Given the following XML:
<root>
Pacman <format bold="1" italic="1">rules</format>!
</root>
What is a better implementation than the following, which results in 2n-1 possible conditional statements?
<xsl:template match="format">
<xsl:choose>
<xsl:when test="@bold='1' and @italic='1'">
<b><i><xsl:value-of-select="."/></i></b>
</xsl:when>
<xsl:when test="@bold='1'">
<b><xsl:value-of-select="."/></b>
</xsl:when>
<xsl:when test="@italic='1'">
<i><xsl:value-of-select="."/></i>
</xsl:when>
</xsl:choose>
</xsl:template>
You can see there is a huge problem if I want to add a new possible attribute such as underline="1", which will result in 4 new conditionals here.
Edit: Also assume that I cannot use CSS classes and must use HTML tags for styling.
My XSLT is so rusty, the hinges won’t budge, but I think you can use
<xsl:call-template … />to process one attribute at a time using one template per attribute.The following probably has some very obvious mistakes, but hopefully it gives you the vibe.