Some of the nodes in an XML document have namespaces, specified with a defined prefix.
It is possible to specify local-name() in XPath 1.0 and so ignore namespaces.
However, I want to enable the writer of the XPath to find nodes using their full namespace-qualified name as an identifier.
The recommended way is to add namespace declarations in the invoking code (in my case, Java). But this means that the person writing Xpath does not have the ability to work with namespaces!
How do we find nodes by their fully qualified names using pure XPath?
Not sure what you meant by "as an identifier".
How do we find nodes by their fully qualified names using pure XPath?
In XPath 1.0, by using local-name() and namespace-uri(), e.g.
"*[local-name() = 'foo' and namespace-uri() = 'http://my.org/ns/2.0']"
In XPath 2.0, there is a richer set of functions related to namespaces, e.g. namespace-uri-from-QName()
. But I'm not sure they improve on the above for what you want.
XPath 3.0, which is currently in working draft status, will include a literal expression for URI qualified QNames allowing to directly specify the namespace uri.
Here are some examples of EQNames:
- pi is a lexical QName without a namespace prefix.
- math:pi is a lexical QName with a namespace prefix.
- "http://www.w3.org/2005/xpath-functions/math":pi specifies the namespace URI using a URILiteral; it is not a lexical QName.
I think Saxon 9.3 includes a preview implementation of xpath 3.0 which should be usable through the java api.
You can use namespaces during your XPath queries. In Java, what you need to provide is an implementation of NamespaceContext if you also want to use prefixes in those queries instead of the fully qualified namespace all the time. Just add an instance of NamespaceContext
to your XPath
- I'll assume you use the standard JDK implementation - but the concept is applicable to Jaxen or others as well.
Then you can perform queries such as //customns:Element
.
If you don't or can't use a NamespaceContext
(for whatever reason) then the only solution seems to be using the local-name
and namespace-uri
functions:
Document doc = ...;
XPath xp = XPathFactory.newInstance().newXPath();
String name = "Element";
String ns = "http://www.custom.org/#";
String expr = "//*[local-name() = '"+name+" and namespace-uri() = '"+ns+"']";
Node node = ((NodeList)xp.evaluate(expr, doc, XPathConstants.NODESET)).item(0);
The spec for XPath 3.0 says:
Q{http://www.w3.org/2005/xpath-functions/math}pi
This works at this moment (October 2015) for example in eXist-db.