How to access attribute and text values from elements in XPathResult?
The following is all in JavaScript.
I have the following XML
<Hotels>
<Hotel category='standard'>Hotel 1</Hotel>
<Hotel category='s开发者_运维问答uperior'>Hotel 2</Hotel>
</Hotels>
My 'results' from my xpath ("//Hotel") returns an array with a length of 2.
What do I do next to get at the category values and the text.
i.e. results[0].???? //will give me category value
and results[0].???? //will give me the text
my 'results' from my xpath ("//Hotel") returns an array with a length of 2.
what do I do next to get at the category values and the text.
You can select both the category
attribute and the text child using a single XPath expression:
/*/Hotel/@category | /*/Hotel/text()
The order of the results may depend on the XPath engine of use, but is usually document-order.
So with the provided XML document:
<Hotels>
<Hotel category='standard'>Hotel 1</Hotel>
<Hotel category='superior'>Hotel 2</Hotel>
</Hotels>
the above XPath expression selects four nodes: the category
attribute of the 1st Hotel
element, the text-node child of the 1st Hotel
element, the category
attribute of the 2nd Hotel
element and the text-node child of the 2nd Hotel
element
精彩评论