Mega Code Archive

 
Categories / XML Tutorial / XSLT StyleSheet
 

If a variable has some defined value

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <data>   <animal>cat</animal>   <animal>dog</animal>   <animal>cow</animal> </data> File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <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>           <paragraph>             <xsl:value-of select="."/>           </P>         </xsl:otherwise>       </xsl:choose>     </xsl:template> </xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>   <P style="color:red">cat</P>   <P style="color:red">dog</P>   <P style="color:red">cow</P>