开发者

XPath Get first element of subset

开发者 https://www.devze.com 2023-02-23 23:51 出处:网络
I have XML like this: <AAA> <BBB aaa=\"111\" bbb=\"222\"> <CCC/> <CCC xxx=\"555\" yyy=\"666\" zzz=\"777\"/>

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?


This one should work for you:

(//*/CCC)[1]


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.

0

精彩评论

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