How can I use XPath to select a node without retrieving all of its child nodes? For instance, in the following XML document:
<parentnode>
<node1 a="b" b="c">
<child1/>
<child2/>
... many many child nodes
<childN/>
</node1>
<node2/>
</parentnode>
I'd like to be able to select the 'node1' element in order to inspect its attributes, but without selecting the child nodes, which I don't need to parse and could be thousands of elements, thus impacting the performance of the query (the output of which is used to build a sort of DOM tree with arrays and dictionaries in a 3rd party library).
Update: just to be clearer, the 3rd party library I mentioned is actually just an Objective-C wrapper around the libxml2 parser that builds a DOM tree made of Foundation classes with the result of any XPath query. The queries themselves are executed over an already parsed document (xmlDocPtr) that is reused for all queries, so yes, as many answers say, the document is already DOM'ed up at C level, but the Objective-C wrapper implementation produces the performance hit in this particular scenario. I could modify this library to optionally not fetch the selected node's children, but I thought there would probably be a simple way to retrieve just the node's attributes with the query.
An XPath expression like /a/b/c will select the c elements: it does not select their children. The reason many people imagine that it also selects the children is that many tools will show the result of the XPath expression by showing you the entire subtree rooted at the c element. One can understand why they do that - it shows you visually what you've selected - but the XPath expression itself is just returning a pointer to the selected element, and where you go from there is entirely up to you. (Some tools, rather than showing you the subtree rooted at the element, show the path to the node with all its ancestors - that's equally valid.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With