I'm new to jQuery and would like to parse an XML document.
I'm able to parse regular XML with the default namespaces but with XML such as:
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"> <s:Schema id="RowsetSchema"> <s:ElementType name="row" content="eltOnly" rs:CommandTimeout="30"> <s:AttributeType name="ows_ID" rs:name="ID" rs:number="1"> <s:datatype dt:type="i4" dt:maxLength="4" /> </s:AttributeType> <s:AttributeType name="ows_DocIcon" rs:name="Type" rs:number="2"> <s:datatype dt:type="string" dt:maxLength="512" /> </s:AttributeType> <s:AttributeType name="ows_LinkTitle" rs:name="Title" rs:number="3"> <s:datatype dt:type="string" dt:maxLength="512" /> </s:AttributeType> <s:AttributeType name="ows_ServiceCategory" rs:name="Service Category" rs:number="4"> <s:datatype dt:type="string" dt:maxLength="512" /> </s:AttributeType> </s:ElementType> </s:Schema> <rs:data> <z:row ows_ID="2" ows_LinkTitle="Sample Data 1" /> <z:row ows_ID="3" ows_LinkTitle="Sample Data 2" /> <z:row ows_ID="4" ows_LinkTitle="Sample Data 3" /> </rs:data> </xml>
All I really want are the <z:row>
.
So far, I've been using:
$.get(xmlPath, {}, function(xml) { $("rs:data", xml).find("z:row").each(function(i) { alert("found zrow"); }); }, "xml");
with really no luck. Any ideas?
XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".
An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.
When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.
Figure 1: Elements and attributes in XML Schema namespace are used to write an XML Schema document, which generates elements and attributes as defined by user and puts them in {target namespace}. This {target namespace} is then used to validate the XML instance.
I got it.
Turns out that it requires \\
to escape the colon.
$.get(xmlPath, {}, function(xml) { $("rs\\:data", xml).find("z\\:row").each(function(i) { alert("found zrow"); }); }, "xml");
As Rich pointed out:
The better solution does not require escaping and works on all "modern" browsers:
.find("[nodeName=z:row]")
I have spent several hours on this reading about plugins and all sorts of solutions with no luck.
ArnisAndy posted a link to a jQuery discussion, where this answer is offered and I can confirm that this works for me in Chrome(v18.0), FireFox(v11.0), IE(v9.08) and Safari (v5.1.5) using jQuery (v1.7.2).
I am trying to scrape a WordPress feed where content is named <content:encoded> and this is what worked for me:
content: $this.find("content\\:encoded, encoded").text()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With