ilinsky / jquery-xpath

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

Use default namespace from context #19

Closed ChiSamurai closed 8 years ago

ChiSamurai commented 8 years ago

Hi,

I got an svg embedded in an html page. If I use the jquery selector to get one of the svg-elements, i.e. $('svg') and take this as context for $.xpath(), the context's default namespace is "http://www.w3.org/2000/svg".

Addressing an subelement like the following does not return any element: $('svg').xpath("//rect")

But using the namespace wildcard works: : $('svg').xpath("//*:rect")

How can I address an element when the default/null namespace is different than html?

Best, Matthias

ilinsky commented 8 years ago

To change default namespace in XPath query you can pass resolver function like this:

$('svg').xpath("//rect", function() {return "http://www.w3.org/2000/svg"});

Or, introducing a prefix in the query:

$('svg').xpath("//s:rect", function(prefix) {
    if (prefix == "s") 
        return "http://www.w3.org/2000/svg";
});