当节点文本包含时,xPath获取兄弟节点

时间:2015-02-06 23:45:25

标签: xml xpath

拥有如下的文本xml代码:

<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>

我想得到节点'n'包含文本“one”的所有兄弟节点文本。

尝试:

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

Ť // N [含有(。, “一个”)] /

我希望它返回类似这样的内容

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

1 个答案:

答案 0 :(得分:2)

它被称为following-sibling,而不是follow-sibling。虽然你走在正确的轨道上:

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

演示(使用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>