考虑以下示例:
<root>
<instruments>
<flute>
<baz>bazik</baz>
</flute>
<guitar>
<deep>
<baz>more bazik</baz>
</deep>
</guitar>
<drum>
<foo>fooled</foo>
</drum>
</instruments>
</root>
我想选择flute
和guitar
,因为它们都包含baz
作为后代节点。我该怎么做?
最佳答案
关键是使用谓词[descendant::baz]
所以表达式可以是
/root/instruments/*[descendant::baz]
关于xml - 如何使用 xpath 选择具有 X 作为后代的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2322014/