JQuery Selector in XML

2019-08-29 10:42发布

问题:

I am attempting to get a set of elements from an XSD document. I have downloaded and I am using the latest version of jQuery (1.7.2). The xsd referenced is a local copy of http://www.w3.org/2001/XMLSchema.xsd the code I am using is as follows:

var xml;
$(function(){
    $.ajax({
        type:"GET",
        url:"http://www.w3.org/2001/XMLSchema.xsd",//"xml/XMLSchema.xsd",
        dataType: 'xml',
        success:function(result){
            xml = $(result);
        }
    });
});

This enables me to load the xsd into the "xml" variable as expected however when I go to query it I end up with some confusing results. Using:

$('complexType[name=simpleType]', xml).attr("name")
$('complexType[name="simpleType"]', xml).attr("name")

return "undefined" however the starts with, ends with and start and ends with return the correct result:

$('complexType[name^="simpleType"]', xml).attr("name")
$('complexType[name$="simpleType"]', xml).attr("name")
$('complexType[name$="simpleType"][name^="simpleType"]', xml).attr("name")

Which is the name "simpleType". Is there a reason why the ='s does not work?

Thanks in advance

回答1:

I think that you have problem with the usage of namespace. You can try to use

$('xs\\:complexType[name=simpleType]', xml).attr("name")

(see about escaping of meta-characters here) instead of

$('complexType[name=simpleType]', xml).attr("name")