Q: Template returns true() when is told to return false Why am I getting true as a return value from this template:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:template name="return-false">
  <xsl:value-of select="false()"/>
 </xsl:template>
 <xsl:template match="/">
  <root>
   <xsl:variable name="call-template">
    <xsl:call-template name="return-false"/>
   </xsl:variable>
   <xsl:if test="$call-template = true()">
    <FALSE/>
   </xsl:if>
  </root>
 </xsl:template>
</xsl:stylesheet>



Guess what I am getting on the output document FALSE element. I am wondering should I cry or should I laugh on my struggle on this simple example. My frustration hit the sky.

A: The xsl:value-of instruction creates a text node.
<xsl:value-of select="false()"/>

returns the string-value of the false() function, which is the string "false". So the content of your $call-template variable is a text node containing the string "false".
http://www.w3.org/TR/xslt/#value-of
Next, the test test="$call-template = true()" returns true(), because: 


*

*you are comparing a text node to a boolean;  

*a node compared to a boolean is first converted to a boolean;  

*a node that exists is evaluated as true() when converted to a boolean. 


http://www.w3.org/TR/xpath/#booleans

A: Note that in XSLT 2.0 and 3.0 a template or function can return a boolean value but not using value-of, instead using sequence e.g.
<xsl:template name="return-false">
    <xsl:sequence select="false()"/>
</xsl:template>

Of course, typically you would not return a boolean constant value but instead simply evaluate a comparison e.g.
<xsl:template name="check">
    <xsl:param name="input"/>
    <xsl:sequence select="matches($input, 'foo')"/>
</xsl:template>

Of course, to use such code in a compact form, you would write a function
<xsl:function name="mf:check">
  <xsl:param name="input"/>
  <xsl:sequence select="matches($input, 'foo')"/>
</xsl:function>

and call it with e.g. <xsl:variable name="check1" select="mf:check('foobar')"/> respectively <xsl:if test="mf:check('foobar')">..</xsl:if>.

A: Test against the value 'true' or 'false' rather than the function and it works.  Why are you doing it this way though?  What are you trying to achieve?  Using a call-template to return a boolean value is more of a procedural thing, and likely not to fit well with the more functional/declarative model of XSLT.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template name="return-false">
        <xsl:value-of select="false()"/>
    </xsl:template>
    <xsl:template match="/">
        <root>
            <xsl:variable name="call-template">
                <xsl:call-template name="return-false"/>
            </xsl:variable>
            <xsl:if test="$call-template = 'true'">
                <FALSE/>
            </xsl:if>
        </root>
    </xsl:template>
</xsl:stylesheet>

