Created
November 5, 2008 22:20
-
-
Save greut/22450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using XPath with namespace | |
// @see https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript#Evaluating_against_an_XML_document_within_an_Extension | |
var ns = "urn:Spam", xml = document.implementation.createDocument(ns, "eggs", null); | |
xml.documentElement.appendChild(xml.createElementNS(ns, "sausage")); | |
xml.documentElement.appendChild(xml.createElementNS(ns, "spam")); | |
xml.documentElement.appendChild(xml.createElementNS(ns, "sausage")); | |
// there is only one namespace used here. | |
var resolver = function(namespace) { return ns; }; | |
var result = xml.evaluate("count(//spam:sausage)", xml, resolver, XPathResult.ANY_TYPE, null); | |
alert(new XMLSerializer().serializeToString(xml) + "\n\n" + | |
result.numberValue + " sausages found"); | |
// OSX: | |
// ---- | |
// Firefox (3.0.3): 2 | |
// Safari (3.1.2): 2 | |
// Opera (9.62): 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment