ilinsky / jquery-xpath

jQuery XPath plugin (with full XPath 2.0 language support)
180 stars 64 forks source link

Is it possible to evaluate XPaths on a given string, or only the DOM? #21

Closed bbarker closed 6 years ago

bbarker commented 6 years ago

For instance, if you've retrieved an XML document using an XHR and want use XPath to retrieve specific elements, is this possible?

ilinsky commented 6 years ago

Yes, indeed. You first create an XML DOM document, and then run a query over it:

var doc = new DOMParser().parseFromString('<my:test xmlns:my="myns"/>', 'text/xml');
alert($(doc).xpath('//my:test', function(prefix){ if (prefix === "my") return "myns"; return null;}))
bbarker commented 6 years ago

Awesome, thanks!