Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test XPath expressions with namespaces in Chrome

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.

like image 414
Mahoni Avatar asked Nov 11 '12 23:11

Mahoni


People also ask

How do I find the XPath namespace?

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.

What is namespace in XPath?

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.

What is XPath with examples?

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.


2 Answers

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;
}
like image 197
Wim Ombelets Avatar answered Oct 02 '22 19:10

Wim Ombelets


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.

like image 36
knb Avatar answered Oct 02 '22 19:10

knb