[xsl] boolean(string(false())) is true - always?

Subject: [xsl] boolean(string(false())) is true - always?
From: "Michael Müller-Hillebrand mmh@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 2 Mar 2017 15:00:28 -0000
Hi folks,

I was visiting some old code:

<xsl:function name="dy:isFoo" as="xs:boolean">
    <xsl:param name="node" as="node()"/>
    <xsl:choose>
      <xsl:when test="$node[self::bar/@foo = 'x']">
            <xsl:value-of select="true()"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="false()"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:function>

and changed that to the more elegant:

<xsl:function name="dy:isFoo" as="xs:boolean">
    <xsl:param name="node" as="node()"/>
    <xsl:sequence select="exists($node[self::bar/@foo = 'x'])"/>
</xsl:function>

BUT then I looked at my old code and said to myself: This could never have
worked?!

Isn't xsl:value-of creating a text node, so in xsl:otherwise it creates
'false'. And if this is cast to boolean as the function requires, we should
get true(), because the string is not empty?

But to my astonishment, the original function works as originally intended, it
can be used inside a predicate and will be either true() or false().

I searched the 4th Edition, and found nothingb&

In hindsight I am happy that the original function worked as intended, but
today I would like to understand why.

Thanks,

- Michael

PS: XSLT 2.0, I tested this with Saxon 9.2 and 9.6.

Sample input:

<bars>
  <bar foo="x"/>
  <bar/>
  <bar foo="X"/>
  <bar foo=""/>
  <bar/>
  <bar foo="x"/>
</bars>

Full XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:dy="dy" version="2.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="bar">
    <xsl:copy>
      <xsl:if test="dy:isFoo(.)">
        <xsl:attribute name="hohoho" select="'rudolph'"/>
      </xsl:if>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:function name="dy:isFoo" as="xs:boolean">
    <xsl:param name="node" as="node()"/>
    <xsl:choose>
      <xsl:when test="$node[self::bar/@foo = 'x']">
        <xsl:value-of select="true()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="false()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>

  <xsl:function name="dy:isFooX" as="xs:boolean">
    <xsl:param name="node" as="node()"/>
    <xsl:sequence select="exists($node[self::bar/@foo = 'x'])"/>
  </xsl:function>

</xsl:stylesheet>

Current Thread