iRamesh
iRamesh

Reputation: 383

XSLT boolean of an empty string prints true

Here is my xslt. Any reason why boolean($x) prints true when boolean($y) prints false when they both have the same value. the only difference is that x gets its empty string by calling a template.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <xsl:variable name="x">
      <xsl:call-template name="tmplate"></xsl:call-template>
    </xsl:variable>

    ###x-bool:[<xsl:value-of select="boolean($x)"/>]
    ###x:[<xsl:value-of select="$x"/>]

    <xsl:variable name="y" select="''"/>
    ###y-bool:[<xsl:value-of select="boolean($y)"/>]
    ###y:[<xsl:value-of select="$y"/>]

  </xsl:template>

  <xsl:template name="tmplate">
    <xsl:value-of select="''"/>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 756

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

the only difference is that x gets its empty string by calling a template.

No, that's not the only difference.When you define a variable as:

<xsl:variable name="y" select="''"/>

the data type of the variable is string. But when you define it as:

<xsl:variable name="x">
    <xsl:value-of select="''"/>
</xsl:variable>

the data type is result-tree-fragment. It contains a text node that contains an empty string. Therefore it is not empty and will be evaluated as true() when converted to boolean.

Upvotes: 1

Related Questions