user502808
user502808

Reputation: 95

In XSLT, what is the difference between true() and true?

I wanted to understand the difference between true() and true and the usage of both.

For example:

I am declaring a variable var1 as follows:

<xsl:variable name="var1"
    select="/root/name ='' and exists(/root/name[@as:firstname])"></xsl:variable>

<!--and now I wan to use it as a condition, say:-->

<xsl:if test="$var1=true() "> <!-- Now would I use true() or true here ?-->
    <xsl:text>Hello World</xsl:text>
</xsl:if>

Upvotes: 2

Views: 2833

Answers (2)

Michael Kay
Michael Kay

Reputation: 163625

Writing something like <xsl:with-param name="married" select="true"/> is a common mistake (I see it more often in XQuery than in XSLT). Here the expression true means child::true, and unless you actually have elements named true in your source document, it's likely to select an empty sequence, which will be treated as false() in a boolean context.

Recent releases of Saxon give you a warning if you use the name "true" like this, inviting you to write it as child::true or ./true if you really want to access an element with this name.

Upvotes: 2

michael.hor257k
michael.hor257k

Reputation: 117165

Since you have defined the variable using a boolean expression, both:

<xsl:if test="$var1=true()"> 

and:

<xsl:if test="$var1"> 

will work the same way. Not sure what you mean by true; it could be a node or it could be a string "true". In the latter case, the test:

<xsl:if test="$var1='true'">

would work in XSLT 1.0 (and it would work just as well with any non-empty string), but not in XSLT 2.0.

Note also that:

string(var1)

will return either "true" or "false", so the test:

<xsl:if test="string($var1)='true'">

will work the way you would expect, in both XSLT 1.0 and 2.0

Upvotes: 5

Related Questions