The Xpath specification reqires that xpath queries always explicitly specify the namespace of the node they want to match. Namespaces are specified by using prefixes in node name tests. In the absence of a prefix the null namespace is assumed. This is very much like how namespaces for attributes are specified.
Here's an example -
We want to write an xpath to find the <table> node in the following document.
<html xmlns="http://www.w3.org/1999/xhtm"> <head> <title> Title </title> </head> <body> <table> ....
The xpath html/body/table looks for nodes in the null namespace and hence will not work. If we assume the xhtml prefix maps to the namespace http://www.w3.org/1999/xhtm the correct xpath would be xhtml:html/xhtml:body/xhtml:table
The mapping from the prefix to a namespace is done by the XPathContext object, i.e. it is completely independent of the prefix definitions in the document in which the query is applied. The default XPathContext class has the following prefixes hardcoded:
"xoe" | Constants.DEFAULT_NAMESPACE |
"xmlns" | Constants.XMLNS_NAMESPACE |
"xhtml" | Constants.XHTML_NAMESPACE |
"xlink" | XLink.XLINK_NAMESPACE |
To add new prefixes create a new XPathContext object and call definePrefix (prefix, namespace). There are versions of findNodes, findFirstNode, evalToString and evalToBoolean which take XPathContext objects. The same applies to expandUsingXPathAttributes, onLoad and hideElements in DOMUtils and XDocument.
If your app extends XMLDataEditor and you need to use a define prefixes for your xpaths you need to call XMLDataEditor.setXPathContext.