As far as I know, you can not do that with a single xpath expression but you would need to loop over the result. An example of such a loop:
$base = '';
foreach($xp->query('//@baseurl|//*[@baseurl]/x/@u') as $element) {
$value = $element->value;
if (substr($value, -1,1) === '/') {
$base = $value;
} else {
echo $base, $value, "
";
}
}
With your example XML document:
male/86644/lol.png
male/86644/haha.png
male/86644/name.png
male/27827/page.png
male/27827/examp.png
male/27827/bottom.png
This example is using the union operator |
to obtain all wanted nodes at once.
I was originally looking for doing the following within xpath but is not possible AFAIK. However PHP can take care of this: Run an Xpath expression in context to a previous xpath query nodes:
$array = array_map(function($context) use($xp) {
return $xp->evaluate('concat(../../@baseurl, .)', $context);
}, iterator_to_array($xp->query('//x/@u')));
Give $array
then:
Array
(
[0] => male/86644/lol.png
[1] => male/86644/haha.png
[2] => male/86644/name.png
[3] => male/27827/page.png
[4] => male/27827/examp.png
[5] => male/27827/bottom.png
)
That is probably more straight forward.