Parsing XML with namespaces using jQuery $().find

前端 未结 3 1267
梦如初夏
梦如初夏 2020-12-06 04:30

I\'m trying to get the contents of a XML document element, but the element has a colon in it\'s name.

This line works for every element but the ones with a colon in

相关标签:
3条回答
  • 2020-12-06 05:12

    That isn't just an ordinary element name. That's a qualified name, meaning that it is a name that specifically refers to an element type within a namespace. The element type name is 'lat', and the namespace prefix is 'geo'.

    Right now, jQuery can't deal with namespaces very well, see bug 155 for details.

    Right now, as a workaround, you should be able to select these elements with just the local name:

    $(this).find("lat").text();
    

    If you have to distinguish between element types with the same local name, then you can use filter():

    var NS = "http://example.com/whatever-the-namespace-is-for-geo";
    $(this).find("lat").filter(function() { return this.namespaceURI == NS; }).text();
    

    Edit: my mistake, I was under the impression that patch had already landed. Use Adam's suggestion for the selector, and filter() if you need the namespacing too:

    var NS = "http://example.com/whatever-the-namespace-is-for-geo";
    $(this).find("geo\\:lat").filter(function() { return this.namespaceURI == NS; }).text();
    
    0 讨论(0)
  • 2020-12-06 05:23

    Use a backslash, which itself should be escaped so JavaScript doesn't eat it:

    $(this).find("geo\\:lat").text();
    
    0 讨论(0)
  • 2020-12-06 05:23

    if you have a jquery selector problem with chrome or webkit not selecting it try

    $(this).find('[nodeName=geo:lat]').text();
    

    this way it works in all browsers

    0 讨论(0)
提交回复
热议问题