xml Xpath/XSLT:检查以下兄弟节点是否为特定节点

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

Xpath/XSLT : check if following sibling is a particular node

xmlxsltxpath

提问by Richard N

I have seen questions where following-sibling has been applied based on a node's value but my issue is related to the actual node itself.

我见过一些问题,其中根据节点的值应用了以下兄弟姐妹,但我的问题与实际节点本身有关。

This is the type of XML I have have:

这是我拥有的 XML 类型:

<Employee>
    <Summary>
        <A>
        <B>
    </Summary>
    <Elections>
    </Elections>
<Employee>

I need to write a Xpath condition as follows:

我需要写一个 Xpath 条件如下:

if (NOT(the following sibling(first sibling) of /Employee/Summary is Elections)), then do something.

Currently I have:

目前我有:

<xsl:if test="(not(following-sibling::Employee/Summary[1]='Earnings'))
    <xsl:call-template name="EmployeeRecord"/>
</xsl:if>

Please note that I am not checking a node value but the node itself(i.e the node name). Any help in the right direction will be much appreciated.

请注意,我不是在检查节点值,而是在检查节点本身(即节点名称)。任何在正确方向上的帮助将不胜感激。

回答by Mads Hansen

You are on the right track. The following-siblingaxis evaluates from the context node in the previous step.

你走在正确的轨道上。的following-sibling轴从在先前步骤中的上下文节点的计算结果。

  • if you are looking for the first element that is the following-sibling of /Employee/Summary, you would use: /Employee/Summary/following-sibling::*[1].

  • In order to evaluate whether that first following-sibling is an Electionselement, you can use an additional predicate filter [self::Elections].

  • Testing for the negation of that, wrap the whole thing in not()
  • 如果您要查找是 的以下同级的第一个元素,则/Employee/Summary可以使用:/Employee/Summary/following-sibling::*[1]

  • 为了评估第一个后续兄弟姐妹是否是一个Elections元素,您可以使用额外的谓词过滤器[self::Elections]

  • 测试是否定的,把整个事情包起来 not()

Putting it all together, adjusting your example:

把它们放在一起,调整你的例子:

<xsl:if test="not(/Employee/Summary/following-sibling::*[1][self::Elections])">
    <xsl:call-template name="EmployeeRecord"/>
</xsl:if>

回答by Dimitre Novatchev

I need to write a Xpath condition as follows:

if (NOT(the following sibling(first sibling) of /Employee/Summary is Elections)), then do something.

我需要写一个 Xpath 条件如下:

if (NOT(the following sibling(first sibling) of /Employee/Summary is Elections)), then do something.

Use:

使用

not(/Employee/Summary/following-sibling::*[1][self::Elections])

The XPath expression that is specified as argument to the not()function selects any Electionselement that is the first following-sibling element of any Summaryelement that is a child of the top element Employee.

指定为函数参数的 XPath 表达式not()选择任何Elections元素,该Summary元素是作为顶级元素的子元素的任何元素的第一个后续同级元素Employee

If this doesn't select any node, then the XPath expression above evaluates to true().

如果这没有选择任何节点,则上面的 XPath 表达式的计算结果为true()