1
votes

Having a text xml code like the following:

<meters>
    <metric>
        <n>one</n>
        <k>two</k>
        <m>three</m>
        <k>four</k>
    </metric>
    <metric>
        <n>one</n>
        <k>five</k>
        <m>six</m>
        <k>seven</k>
    </metric>
    <metric>
        <n>two</n>
        <k>eight</k>
        <m>nine</m>
        <k>ten</k>
    </metric>
</meters>

I would like to get all the sibling node text where node 'n' contains the text "one".

Tried:

//*[contains(text(),"one")]/follow-sibling::*/
//*[contains(.,"one")]/follow-sibling::*/

T //n[contains(.,"one")]/

I want it to return something like this

<k>two</k>
<m>three</m>
<k>four</k>
<k>five</k>
<m>six</m>
<k>seven</k>"
1

1 Answers

2
votes

It is called following-sibling, not follow-sibling. Though you were on the right track:

//n[contains(., "one")]/following-sibling::*

Demo (using xmllint):

$ xmllint input.xml --xpath '//n[contains(., "one")]/following-sibling::*'
<k>two</k><m>three</m><k>four</k><k>five</k><m>six</m><k>seven</k>