mozilla

Compare Revisions

Introduction to using XPath in JavaScript

Change Revisions

Revision 110866:

Revision 110866 by Sebuls on

Revision 110867:

Revision 110867 by Broofa on

Title:
Introduction to using XPath in JavaScript
Introduction to using XPath in JavaScript
Slug:
Introduction_to_using_XPath_in_JavaScript
Introduction_to_using_XPath_in_JavaScript
Tags:
DOM, XML, XSLT, Extensions, Add-ons, Transforming_XML_with_XSLT, XPath, "Web Development"
DOM, XML, XSLT, Extensions, Add-ons, Transforming_XML_with_XSLT, XPath, "Web Development"
Content:

Revision 110866
Revision 110867
tt309    <h3 name="Within_an_XML_Document">
310      Within an XML Document
311    </h3>
312    <p>
313      When working within an XML document, a namespace resolver i
 >s required. For example with this document:
314    </p>
315    <pre>
316&lt;?xml version="1.0" encoding="UTF-8"?&gt;
317&lt;feed xmlns="http://www.w3.org/2005/Atom"&gt;
318    &lt;entry /&gt;
319    &lt;entry /&gt;
320    &lt;entry /&gt;
321&lt;/feed&gt;
322</pre>
323    <p>
324      <code>doc.evaluate('//entry', doc, null, XPathResult.ANY_TY
 >PE, null)</code> will return an empty set. To properly resolve th
 >is namespace, you'll need a resolver that, at a minimum, identifi
 >es the default namespace. Like thus:
325    </p>
326    <pre>
327function resolver() {
328    return 'http://www.w3.org/2005/Atom';
329}
330doc.evaluate('//entry', doc, resolver, XPathResult.ANY_TYPE, null
 >)
331</pre>
332    <p>
333      More complex resolvers can be used for documents that use m
 >ultiple namespaces. For example, the Google Data APIs might use t
 >he following resolver:
334    </p>
335    <pre>
336var prefixes = {
337    atom: 'http://www.w3.org/2005/Atom',
338    gCal: 'http://schemas.google.com/gCal/2005',
339    gd: 'http://schemas.google.com/g/2005',
340    html: 'http://www.w3.org/1999/xhtml',
341    openSearch: 'http://a9.com/-/spec/opensearchrss/1.0/',
342};
343var resolver = function(prefix) {
344    return prefixes[name] ||
345        prefixes.atom;  // Default to ATOM namespace
346}
347</pre>

Back to History