开发者

How can I extract part of an XML document using jquery to be transformed (xslt)?

I want to know how can I extract part of an XML document using jquery to an object, which will be transformed via XSLT? So, let's say I have this sample XML document:

<nutrition>

<daily-values>
    <total-fat units="g">65</total-fat>
<saturated-fat units="g">20</saturated-fat>
<cholesterol units="mg">300</cholesterol>
<sodium units="mg">2400</sodium>
<carb units="g">300</carb>
<fiber units="g">25</fiber>
<protein units="g">50</protein>
</daily-values>

<food>
<name>Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100"/>
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
    <a>0</a>
    <c>0</c>
</vitamins>
<minerals>
    <ca>0</ca>
    <fe>0</fe>
</minerals>
</food>

<food>
<name>Bagels, New York Style </name>
<mfr>Thompson</mfr>
<serving units="g">104</serving>
<calories total="300" fat="35"/>
<total-fat>4</total-fat>
<saturated-fat>1</saturated-fat>
<cholesterol>0</cholesterol>
<sodium>510</sodium>
<carb>54</carb>
<fiber>3</fiber>
<protein>11</protein>
<vitamins>
    <a>0</a>
    <c>0</c>
</vitamins>
<minerals>
    <ca>8</ca>
    <fe>20</fe>
</minerals>
</food>
</nutrition>

Let's say I just want the first < food >...< /food > block and I wish to save it as an object or a string. How can this be done using jquery? Once I have the XML block extracted, I can properly combine with a XSL document for transformation (XSLT). Thank you very much for any help!

UPDATE; CLARIFICATION:

I apologize if my question is not clear. Please allow me to clarify my position.

I am well aware that XML can be manipulated by using the browser's DOM and the differences between IE and non-IE implementations. I can use methods like cloneNode and selectNodes on an XML Object. In fact, that's how I have done it. Now if I bring jQuery into the equation, I'd like to replace all those lines of code with a jQuery way of dealing with it. That is, perhaps there's a jQuery plugin that eliminates the need to directly interact with the XML DOM for each browser, taking care of cross-compatibility "behind-the-scenes". I'm looking for a js framework that will take care of everything for me, instead of how I am dealing with it at the moment, which is separate implementations for IE and non-IE browsers.

I hope that clarifies my question.

Thank you very much for any h开发者_JS百科elp.


With jQuery.parseXML(xmlString) you can convert an XML string to a jQuery object, then do whatever you want with that.

OR

If it's an XML file, you can GET it via AJAX

$.ajax({
        type: "GET",
        url: "file.xml",
        dataType: "xml",
        error: function(){},
        success: function (xml) {// you can access the parsed XML just like you do it with HTML }
    });


In compatible browsers you can convert a document object or fragment to XML using an XMLSerializer:

var d = document.createElement('div');
d.id = 'div0'
var p = d.appendChild(document.createElement('p'));
var xmlString = (new XMLSerializer()).serializeToString(d);

alert(xmlString); 
// shows <div xmlns="http://www.w3.org/1999/xhtml" id="div0"><p></p></div>

A little more work in IE, but not much. You need to create an XML object of just the bits you want, then get it's xml property:

alert(xmlObject.xml);


After doing some research, it appears even those within the jQuery community recommend XML manipulation to be done by the browser, as it is usually faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜