xml 从 XPath 中的选择中排除某些元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1068636/
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 12:35:17  来源:igfitidea点击:

Exclude certain elements from selection in XPath

xmlxpath

提问by user123444555621

I know this is a simple question, but I can't figure it out. Consider the following simple XML document:

我知道这是一个简单的问题,但我无法弄清楚。考虑以下简单的 XML 文档:

<root>
  <a></a>
  <b></b>
  <c></c>
  <a></a>
  <d></d>
  <e></e>
  <a></a>
  <a></a>
</root>

What's the best way to select the nodes <b>through <e>using XPath?

<b>通过<e>使用 XPath选择节点的最佳方法是什么?

I'm looking for something like

我正在寻找类似的东西

/root/*[not(a)]

(which does not do the trick)

(这不起作用)

回答by Tomalak

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

回答by Louis

Answering to add that in XPath 2.0, you can use except:

回答在 XPath 2.0 中添加它,您可以使用except

/root/(* except a)

For XPath 1.0, Tomalak pointed out, this is the standard way to do it:

Tomalak 指出,对于 XPath 1.0,这是执行此操作的标准方法:

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

By the way, if someone lands here trying to use this in XSLT 2.0 in a xsl:template/@matchattribute it won't work because @matchtakes patterns which although looklike XPath expressions, are notXPath expressions. The solution for XPath 1.0 would work in this case.

顺便说一下,如果有人试图在 XSLT 2.0 中的xsl:template/@match属性中使用它,它将不起作用,因为@match采用的模式虽然看起来像 XPath 表达式,但不是XPath 表达式。XPath 1.0 的解决方案适用于这种情况。

回答by BVH

I realize this is an old question, but I recently ran into a similar problem and used the following xpath to solve it:

我意识到这是一个老问题,但我最近遇到了类似的问题并使用以下 xpath 来解决它:

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

回答by chinna

Have you tried:

你有没有尝试过:

/root/b|/root/c|root/d|/root/e

/root/b|/root/c|root/d|/root/e