XPath - 获取没有特定类型子节点的节点 [英] XPath - Get node with no child of specific type
问题描述
XML:/A/B
或 /A
我想获得所有没有任何 B
子节点的 A
节点.
I want to get all A
nodes that do not have any B
children.
我试过了
/A[not(B)]
/A[not(exists(B))]
没有成功
如果可能,我更喜欢使用语法 /*[local-name()="A" and .... ]
的解决方案.有什么可行的想法吗?
I prefer a solution with the syntax /*[local-name()="A" and .... ]
, if possible. Any ideas that works?
澄清.xml 看起来像:
Clarification. The xml looks like:
<WhatEver>
<A>
<B></B>
</A>
</WhatEver>
或
<WhatEver>
<A></A>
</WhatEver>
推荐答案
也许*[local-name() = 'A' and not(descendant::*[local-name() = 'B'])]
?
另外,应该只有一个根元素,所以对于 /A[...]
你要么取回所有的 XML,要么没有.也许 //A[not(B)]
或 /*/A[not(B)]
?
Also, there should be only one root element, so for /A[...]
you're either getting all your XML back or none. Maybe //A[not(B)]
or /*/A[not(B)]
?
我真的不明白为什么 /A[not(B)]
对你不起作用.
I don't really understand why /A[not(B)]
doesn't work for you.
~/xml% xmllint ab.xml
<?xml version="1.0"?>
<root>
<A id="1">
<B/>
</A>
<A id="2">
</A>
<A id="3">
<B/>
<B/>
</A>
<A id="4"/>
</root>
~/xml% xpath ab.xml '/root/A[not(B)]'
Found 2 nodes:
-- NODE --
<A id="2">
</A>
-- NODE --
<A id="4" />
这篇关于XPath - 获取没有特定类型子节点的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!