I have a XML document opened in Chrome and I would like to test some XPath expressions. The XML document involves different namespaces. How do I define the prefix URIs?
For testing XPath expressions in Chrome I have used $x("...")
so far.
There is no method to connect a namespace prefix to a namespace in XPath. The hosting library can provide these services. It is strongly advised that we take advantage of those features and create namespace prefixes that may be used to qualify XML element and attribute names as needed.
The Default NamespaceXPath treats the empty prefix as the null namespace. In other words, only prefixes mapped to namespaces can be used in XPath queries. This means that if you want to query against a namespace in an XML document, even if it is the default namespace, you need to define a prefix for it.
XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document. XPath is a syntax for defining parts of an XML document. XPath uses path expressions to navigate in XML documents. XPath contains a library of standard functions.
as taken from developer.mozilla's Introduction_to_using_XPath_in_JavaScript
Implementing a User Defined Namespace Resolver
function nsResolver(prefix) {
var ns = {
'xhtml' : 'http://www.w3.org/1999/xhtml',
'mathml': 'http://www.w3.org/1998/Math/MathML'
};
return ns[prefix] || null;
}
If you searched for an element "description" anywhere inside the XML document, you could use the local-name() function. Example:
$x("//*[local-name()='description']")
Inspired by this answer on SO.
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