David
David

Reputation:

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

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

<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:

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

What am I doing incorrectly?

Upvotes: 29

Views: 76942

Answers (5)

Shaiful Islam
Shaiful Islam

Reputation: 45

Try with this

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

Upvotes: 2

Dominic Cronin
Dominic Cronin

Reputation: 6191

XSLT has a function specially for this problem.

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

Upvotes: 2

Ben Blank
Ben Blank

Reputation: 56572

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:

<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-each is performing other operations as well, you don't need to iterate at all and can use just if to accomplish the same task:

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

Upvotes: 44

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

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:

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]

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

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

   /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:

   . = '1112'

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

  . = 1112

Upvotes: 13

tcurdt
tcurdt

Reputation: 15808

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:

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

Upvotes: 3

Related Questions