2
votes

The question(s): Can a XPath 1.0 expression return only the names of xml nodes (either qualified or not) and not the entire node? Is XPath 2.0 capable of same?


The detail:

I am profiling a series of well-formed xml documents for xml element node usage (ie. obtain aggregate counts of xml nodes across several xml documents). I have a perl script that retrieves a desired node-set of each document, then iterates over each node to obtain counts of the node names using the XML::XPath::Node::Element->getName. I have an alternate implementation that profiles xml node attributes and text values, but use an XPath statement to retrieve the "atomic values" (as opposed to using XML::XPath::Node::Element->getAttribute() or similar in perl).

[Note that I realize the "atomic values" are actually sets of attribute/ text nodes.]

I experience a 50-fold improvement in performance when allowing the XPath expression to return the desired "atomic values" as opposed to looping over results in perl, so I would like to retrieve xml node names in a manner similar to attribute (ie 'attribute::' or '@')and text (ie 'text()') nodes. I've looked over the XPath and XML specs, but have not been able to identify a solution.

edit: To be more specific, I'm looking for something like 'local-name(//*)', but something that returns a set of names for all matching nodes (as opposed to only the first node found).

3

3 Answers

2
votes

In the XPath 1.0 data model there is no such thing as a set of strings, therefore no way of returning the set of names of a set of nodes as a result.

In XPath 2.0 it's straightforward:

//*/local-name()
0
votes

You can call the local-name function:

//*[local-name()='nodename']
0
votes

XPath 1.0 is a filter; it can't generate content. If it's not a node in the DOM (element node, attribute node, text node, etc), it can't be returned. That means that strings can't be returned by an XPath query.

That's why //*/local-name() can't possibly work.