Why do I get parent elements when I call the SimpleXMLElement::xpath()
method on a child element?
Example:
$xml = '<root>
<node>
<tag v="foo"/>
<tag v="bar"/>
</node>
<node>
<tag v="foo"/>
</node>
<node>
<tag v="foo"/>
<tag v="bar"/>
</node>
</root>';
$simpleXML = new \SimpleXMLElement($xml);
$nodeList = $simpleXML->xpath('//node');
foreach ($nodeList as $node) {
$node->xpath('//tag');
}
Here $node->xpath('//tag')
returns all the <tag>
xml tags of the document, at each iteration, instead of returning only the child <tag>
elements of the <node>
tag.