How to refer to current node value in XSL for-each test?
Solution 1
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>
Solution 2
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:
Within [ and ] there is no predicate: the "=" operator needs two arguments but only one is provided.
.[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
Solution 3
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>
Solution 4
XSLT has a function specially for this problem.
http://www.w3.org/TR/xslt#function-current
Solution 5
Try with this
<xsl:for-each select="/books/book">
<xsl:if test="current() = '1112'">
Success
</xsl:if>
</xsl:for-each>
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on April 16, 2020Comments
-
Admin about 4 years
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?