XPath Get first element of subset
44,546
Solution 1
This one should work for you:
(//*/CCC)[1]
Solution 2
I want to get first element. But with XPath expression
//*/CCC[1]
I have got two elements. Each of them is the first elemet in<BBB></BBB>
context. How to get first element in subset?
This is a FAQ:
The []
operator has a higher precedence (binds stronger) than the //
abbreviation.
Use:
(//CCC)[1]
This selects the first (in document order) CCC
element in the XML document.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on January 11, 2020Comments
-
Admin over 4 years
I have XML like this:
<AAA> <BBB aaa="111" bbb="222"> <CCC/> <CCC xxx="555" yyy="666" zzz="777"/> </BBB> <BBB aaa="999"> <CCC xxx="qq"/> <DDD xxx="ww"/> <EEE xxx="oo"/> </BBB> <BBB> <DDD xxx="oo"/> </BBB> </AAA>
I want to get first
<CCC>
element. But with XPath expression//*/CCC[1]
I have got two<CCC>
elements. Each of them is the first elemet in<BBB></BBB>
context. How to get first element in subset?