Get first node using E4X with mixed namespaces?
Consider an XML snippet with a handful of namespaces:
<meal xmlns="urn:hl7-org:v开发者_StackOverflow社区3">
<veg id="7" />
<lunch id="123">
<veg id="990" />
</lunch>
<dinner id="324">
<veg id="111" />
</dinner>
</meal>
Using JavaScript E4X, how can you explicitly select the first veg
node's id property?
It's important to explicitly include all namespaces as well. This is the reason for my ..*::
syntax below. I realize I'm using the wrong operator here.
I've tried this, which unfortunately gets ALL veg node id values:
var veg = meal..*::veg.@id.toString()
//currently gets 7990111
How can I get the value of 7
?
var veg = meal..*::veg[0].@id
.
var meal = // xml;
var veg = meal..*::veg.@id[0];
Try this :
var veg = meal..*::veg[position()=1].@id.toString()
veg[position()=1]
tells XPath you want the veg node at the first position.
I just complete your initial xpath expression.
精彩评论