0

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

Meberem
  • 937
  • 7
  • 18

1 Answers1

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")
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • This does work as I you say, thanks! Is there any reasoning behind why the node is matched correctly when I don't include the namespace prefix and when either ^=, $= or *= is used for matching the attribute value as opposed to just = ? – Meberem Jun 12 '12 at 15:53
  • 1
    @Meberem: You are welcome! In tests which I made all the tests with ^=, $= or *= get `undefined` value. I can imagine that it could be important which web browser and in which version do you used. In general jQuery is designed for parsing of DOM and not XML. Parsing of XML documents with jQuery is not the best idea. The usage of `getElementsByTagNameNS`: `result.documentElement.getElementsByTagNameNS("http://www.w3.org/2001/XMLSchema", "simpleType")` and then test attributes of all selected elements in the loop can get better results. – Oleg Jun 12 '12 at 16:40
  • Thanks for the follow up! I thought parsing XML as opposed to pure DOM with jQuery might be ambitious, I will take a look into what you have suggested, thanks again – Meberem Jun 12 '12 at 18:31