当节点文本包含时,xPath 获取兄弟节点
xPath get sibling nodes when node text contains
文本 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::*/
T
//n[包含(.,"one")]/
我想要它 return 像这样
<k>two</k>
<m>three</m>
<k>four</k>
<k>five</k>
<m>six</m>
<k>seven</k>"
叫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>
文本 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::*/
T //n[包含(.,"one")]/
我想要它 return 像这样
<k>two</k>
<m>three</m>
<k>four</k>
<k>five</k>
<m>six</m>
<k>seven</k>"
叫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>