xsl-list
[Top] [All Lists]

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

2017-03-02 09:00:35
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 nothing…

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>
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>