Why do I get parent elements when I call the SimpleXMLElement::xpath method on a child element?

Максим

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.

Nigel Ren

In XPath, when you use //, you are saying a node in any position relative to the current node. With XPath this also includes nodes higher in the document as well as enclosed in this element.

There are several ways to solve it in this particular scenario...

With reference to What's the difference between //node and /descendant::node in xpath?, you can use the descendant axis...

foreach ($nodeList as $node) {
    $node->xpath('descendant::tag');
}

which only uses nodes inside the base node (in this case $node).

Or if the hierarchy in your document is exactly as you have in your document, a simpler way is to use SimpleXML object notation (with a loop to show each one)...

foreach ($nodeList as $node) {
    // For each enclosed tag
    foreach ( $node->tag as $tag )  {
        // Echo the v attribute
        echo $tag['v'].PHP_EOL;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get a BootstrapMethodError when trying to call a super class's protected method using method reference from an inner class?

How do I call an overridden parent class method from a child class?

How do I select child elements of any depth using XPath?

what method do I call if I want to get the parent key if a certain type of child exist?

How do I call a child's method from the parent class?

How to call method from parent when i click on child?

How can I select parent elements that contain empty element with XPath?

Why do I get compilation error when doing flatmap() call?

ReactJS - how do I call a parent method from a child component?

Using LESS, can I reference a parent element in nested child elements

How do I call an event handler or method in a child component from a parent?

How do I call abstract class' child class method when using an array of child objects?

Why do I get a borrow error when returning result of a child structure's method?

Should i call the parent class method with super() in the child class

why is it when I try to create a child class from a parent class(Downcasting), I get a null object

In Ada, why doesn't a child get instantiated with the generic parent and why do I have to make it generic as well?

How do I get only the visible children elements of a parent element?

Why is __get called instead of __call when I call a nonexistent method?

Why do I have to pass the self argument when I call a parent method in Python?

How do I get this child element with overflow auto to show when the parent is overflow hidden?

How can I call method in child component from parent component?

How to find the parent element If I don't know number of elements that is present between the child element and the Parent element

How do I increase parent element as child element is increased?

How do I call a method in the non-specific child component from parent component

Why do I get an error when entering child classes to a TreeMap defined for their parent?

How do I target the correct data attribute and child element when dealing will multiple occurrences of the same parent element?

How do I call a method in one Blazor (parent) component from another Blazor ("child") component?

Why is Typescript calling an overridden child method from within the parent class if I call child.listen()?

How to get a parent element by two child elements using XPATH?