XPath - 获取没有特定类型子节点的节点 [英] XPath - Get node with no child of specific type

查看:34
本文介绍了XPath - 获取没有特定类型子节点的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆