Nic Miloslav Example 23 | KEYWORDS EXAMPLES AUTHORS |
---|
Boolean function is very useful, if you want to check, if a variable has some defined value. Compare Stylesheet 1 and Stylesheet 2
XML | HOME XSL 1 XSL 2 |
---|
<xslTutorial > |
<animal>cat</animal> |
<animal>dog</animal> |
<animal>cow</animal> |
</xslTutorial> |
XSL 1 | HOME XML HTML 1 OUTPUT 1 |
---|
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > |
<xsl:variable name="color">red</xsl:variable> |
<xsl:template match="//animal"> |
<xsl:choose> |
<xsl:when test='boolean($color)'> |
<P style="color:{$color}"> |
<xsl:value-of select="."/></P> |
</xsl:when> |
<xsl:otherwise> |
<P><xsl:value-of select="."/></P> |
</xsl:otherwise> |
</xsl:choose> |
</xsl:template> |
</xsl:stylesheet> |
HTML 1 | HOME XML XSL 1 OUTPUT 1 |
---|
<HTML> |
<HEAD> </HEAD> |
<BODY> |
<P style="color:red">cat</P> |
<P style="color:red">dog</P> |
<P style="color:red">cow</P> </BODY> </HTML> |
OUTPUT 1 | HOME XML XSL 1 HTML 1 |
---|
cat
dog
cow
XSL 2 | HOME XML HTML 2 OUTPUT 2 |
---|
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > |
<xsl:variable name="color"></xsl:variable> |
<xsl:template match="//animal"> |
<xsl:choose> |
<xsl:when test='boolean($color)'> |
<P style="color:{$color}"> |
<xsl:value-of select="."/></P> |
</xsl:when> |
<xsl:otherwise> |
<P><xsl:value-of select="."/></P> |
</xsl:otherwise> |
</xsl:choose> |
</xsl:template> |
</xsl:stylesheet> |
HTML 2 | HOME XML XSL 2 OUTPUT 2 |
---|
<HTML> |
<HEAD> </HEAD> |
<BODY> |
<P>cat</P> |
<P>dog</P> |
<P>cow</P> </BODY> </HTML> |
OUTPUT 2 | HOME XML XSL 2 HTML 2 |
---|
cat
dog
cow