Let's say I have this:
<foo>
<bar>CCC</bar>
<baz>sometexthere</baz>
</foo>
<foo>
<bar>AAA</bar>
<baz>sometext</baz>
</foo>
<foo>
<bar>DDD</bar>
<baz>something</baz>
</foo>
Now, I want to get the baz value, which is coming right after bar with the value AAA (!but only with the value AAA!). I don't know how many "foo"s I have, so I can't exactly write something like:
$element->item(0) // I don't know the exact number
So how can I get the value of the baz, which follows after bar with a particular value?
(For the example above, I would like to get sometext because it comes after AAA)
A basic query would be to find the bar
s that contain AAA
, then navigate to the corresponding baz
element.
//foo/bar[.="AAA"]/../baz
Or, find all baz
and filter based on the associated bar
.
//foo/baz[../bar = "AAA"]
Or, baz
s within foo
s containing bar
having AAA
.
//foo[bar="AAA"]/baz
If you're not familiar with XPath expressions, bookmark XML Path Language (XPath) for later.