Why is there no XPath syntax for namespace-qualified nodes?

非 Y 不嫁゛ 提交于 2019-11-30 11:31:44

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);
Ciprian Dobra

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!