Monday, August 4, 2008

XSLT example template that returns boolean

In XSLT you can create templates that act as boolean functions.

<xsl:template match="/">
<!-- create variable named cat to store boolean value -->
<xsl:variable name="cat">
<xsl:call-template name="HasCat"/>
</xsl:variable>

<xsl:when test="$cat= 'true'" >
<!-- do something because it has a child element named cat -->
</xsl:when>
</xsl:template>


<xsl:template name="HasCat">
<xsl:choose>
<xsl:when test="count(descendant-or-self::*[contains(local-name(), 'Cat')]) > 0">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="false()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>