[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

boolean(string(false())) is true - always?

Subject: 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
 boolean(string(false())) is true - always?
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.