开发者

Find element with matching attribute

开发者 https://www.devze.com 2023-03-28 20:59 出处:网络
I have some xml: <myco:results xmlns:myco=\"http://myco.mycollp.com\" resultsTypeID=\"StockChart\">

I have some xml:

<myco:results xmlns:myco="http://myco.mycollp.com" resultsTypeID="StockChart">
    <myco:row xmlns:myco="http://myco.mycollp.com">
        <myco:price title="stock"&开发者_高级运维gt;0.6107013847</myco:price>
        <myco:price title="index">0.61965464</myco:price>
    </myco:row>
</myco:results>

I am looping over it using the following jQuery:

$(xml).find("row").each(function () {

    var stockPrice = $(this).find("price[title='stock']").text();
}

However stockPrice is always coming back as an empty string.

if I inspect:

$(this).find("price[title!='stock']")

it has a length of 2, not 1, and

$(this).find("price[title='stock']")

has a length of 0.

What am I doing wrong?

UPDATE

Have given full xml with namespace info, I had omitted this originally to make the example clearer but this may be the cause of the problem so have give the full XML now.


here it is your selectors were incorrect while using a namespace: http://jsfiddle.net/PvakT/3/


OK, I managed to work it using the jsfiddle.net website that @pimvdb suggested.

var xml = '<myco:results xmlns:myco="http://myco.mycollp.com">\
    <myco:row xmlns:myco="http://myco.mycollp.com">\
        <myco:price title="stock">0.61</myco:price>\
        <myco:price title="index">12.5</myco:price>\
            </myco:row>\
</<myco:results>';

$(xml).find("myco\\:row").each(function () {
    var stockPrice = $(this).find("myco\\:price[title='stock']").text();
    alert(stockPrice);
});

Thanks

0

精彩评论

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