xml 如何在 XSL 中为每个测试引用当前节点值?

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

How to refer to current node value in XSL for-each test?

xmlxsltxpath

提问by Ben Blank

Let's say I have an XML doc like this:

假设我有一个这样的 XML 文档:

<books>
    <book>1110</book>
    <book>1111</book>
    <book>1112</book>
    <book>1113</book>
</books>

I'm trying to setup a condition that tests the value of the current node in the for-each, but I'm doing something wrong:

我正在尝试设置一个条件来测试 中当前节点的值for-each,但我做错了:

<xsl:for-each select="/books/book">
    <xsl:if test=".[='1112']">
        Success
    </xsl:if>
</xsl:for-each>

What am I doing incorrectly?

我做错了什么?

回答by Ben Blank

Using .can, indeed, refer to the current (or "context") node, but not the way you're using it here. In XPath, .[foo]is not valid syntax — you need to use self::node()[foo]instead. Also, the =operator needs something to match against, in this case the text()selector to access the element's text contents:

.实际上,使用can 指的是当前(或“上下文”)节点,但不是您在此处使用它的方式。在 XPath 中,.[foo]是无效的语法 — 您需要self::node()[foo]改用。此外,=操作符需要匹配一些东西,在这种情况下,text()选择器访问元素的文本内容:

<xsl:for-each select="/books/book">
    <xsl:if test="self::node()[text()='1112']">
        Success
    </xsl:if>
</xsl:for-each>

As stated in the other answers, however, unless your for-eachis performing other operations as well, you don't need to iterate at all and can use just ifto accomplish the same task:

但是,如其他答案中所述,除非您for-each还执行其他操作,否则您根本不需要迭代,只需使用if即可完成相同的任务:

<xsl:if test="/books/book[. = 1112]">
    Success
</xsl:if>

回答by Dimitre Novatchev

I'm trying to setup a condition that tests the value of the current node in the for-each, but I'm doing something wrong:

我正在尝试设置一个条件来测试 for-each 中当前节点的值,但我做错了:

The first thing that is incorrect is the syntax:

首先不正确的是语法

   .[='1112']

There are two things wrong here:

这里有两件事不对

  1. Within [ and ] there is no predicate: the "=" operator needs two arguments but only one is provided.

  2. .[x = y]is still invalid syntax, although the predicate is OK. This has to be specified as:

    self::node()[condition]

  1. 在 [ 和 ] 中没有谓词:“=”运算符需要两个参数,但只提供了一个参数。

  2. .[x = y]虽然谓词是 OK ,但仍然是无效的语法。这必须指定为:

    self::node()[条件]

The second thingin the provided code that can be improvedis the <xsl:for-each>instruction, which isn't necessary at all; A single XPath expression will be sufficient.

提供的代码中可以改进的第二件事<xsl:for-each>指令,这根本不是必需的;单个 XPath 表达式就足够了。

To summarize, one possible XPath expression that evaluates to the required boolean value is:

总而言之,计算为所需布尔值的一种可能的 XPath 表达式是:

   /books/book[. = '1112']

If it is really necessary that the condition be tested inside the <xsl:for-each>instruction, then one correct XPath expression I would use is:

如果确实有必要在<xsl:for-each>指令中测试条件,那么我将使用的一种正确的 XPath 表达式是:

   . = '1112'

The aboveis a string comparison and may not evaluate to true()if there are spaces around. Therefore, a numerical comparison may be better:

以上是字符串比较,如果周围有空格,可能无法评估true()。因此,数值比较可能更好

  . = 1112

回答by Dominic Cronin

XSLT has a function specially for this problem.

XSLT 有一个专门针对这个问题的功能。

http://www.w3.org/TR/xslt#function-current

http://www.w3.org/TR/xslt#function-current

回答by tcurdt

While Ben has answered your question correctly, using for-each is most definitely the wrong general approach. After all this is XSLT. So you are probably more looking for something like this:

虽然 Ben 正确回答了您的问题,但使用 for-each 绝对是错误的一般方法。毕竟这是XSLT。所以你可能更喜欢这样的东西:

<xsl:if test="/books/book[text()='1112']">
  Success
</xsl:if>

回答by Shaiful Islam

Try with this

试试这个

<xsl:for-each select="/books/book">
    <xsl:if test="current() = '1112'">
        Success
    </xsl:if> 
</xsl:for-each>