开发者

get attribute from selectNodes in JavaScript and XML?

I 开发者_Go百科am trying to get an attribute from an XML node from JavaScript.

item.selectNodes("enclosure[@url]")

That is not working like I thought it would :(

Any hints ?

thanks!


[@url]

is a predicate, which does not select the attribute but filters the "enclosure" nodes that do have a url attribute.

In XPath,

enclosure/@url

would select the attribute.


This:

item.selectNodes("enclosure[@url]")

will give you a collection of enclosure nodes that have a url attribute.

To get a collection of url attribute nodes that are on enclosure nodes, do this:

item.selectNodes("enclosure/@url")

Which you must then loop over to get the values of each one. Remember this gives you attribute nodes, not attribute values. You can use attributeNode.nodeValue to get the value from the node.

If you are expecting just one such node, then use selectSingleNode instead of selectNodes. This will give you the first matching node, instead of a collection of all matching nodes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜