xml XPath 获取除具有特定名称的子元素之外的所有子元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4121539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:23:13  来源:igfitidea点击:

XPath to get all child elements except one with specific name?

xmlxpathelements

提问by John K

How do I target all elements in a document excepta particular element name?

如何定位文档中特定元素名称之外的所有元素?

For example I want to exclude the terminateelements. They can occur throughout the document.

例如我想排除terminate元素。它们可以在整个文档中出现。

 <root>
     <terminate attr="1" />
     <other>
         The brown fox jumps over the fence. 
         <terminate>
            <b>stuff</b>
         </terminate>
     </other>
 </root>

I've tried using the not(..)operator without success likely because I'm using it wrong.

我尝试使用该not(..)运算符但没有成功,可能是因为我使用错误。

And frankly trying to Google 'not' is tough!

坦率地说,试图谷歌“不”是艰难的!

回答by Jagmag

The following xpath should work

以下 xpath 应该可以工作

/root/*[not(self::terminate)] 

Also, i think you can do it with this as well

另外,我认为你也可以这样做

/root/*[not(name()='terminate')]

回答by Rudi Kershaw

If you are using XPath 2.0 you can use

如果您使用的是 XPath 2.0,则可以使用

/root/* except /root/terminate

Which does what you would expect. It matches for nodes that exist in the first sequence but not the second.

哪个做你所期望的。它匹配存在于第一个序列中但不存在于第二个序列中的节点。

XPath 2.0 Specification - except operator

XPath 2.0 规范 - 运算符除外