I have an xml document full of nested item
nodes. In most cases, each item
has a name
element. I want to check if an item
has a name
element, and return a default name if one doesn't exist.
<item>
<name>Item 1</name>
</item>
<item>
<items>
<item>
<name>Child Item 1</name>
</item>
<item>
<name>Child Item 2</name>
</item>
</items>
</item>
When I ask node.at('name')
for the node with no name
element, it picks the next one from the children further down the tree. In the case above, if I ask at('name')
on the second item
, I get "Child Item 1"
.
The problem is you're using
at()
, which can accept either a CSS selector or an XPath expression, and tries to guess which you gave it. In this case it thinks thatname
is a CSS selector, which is a descendant selector, selectingname
elements anywhere below the current node.Instead, you want to use an XPath expression to find only child
<name>
elements. You can do this either by making it clearly an XPath expression:or you can do it by using the
at_xpath
method to be clear:Here's a simple working example: