[xsl] When/How to use templates, and when to use if/choose

Subject: [xsl] When/How to use templates, and when to use if/choose
From: "Ian Jolly" <Ian.Jolly@xxxxxxxxxxxxx>
Date: Tue, 19 Jun 2007 22:56:53 +0100
Hello,
I'm relatively new to XSLT programming but have quite a background in Visual Basic. I've been writing some XSLT that works perfectly well, but I believe it not to be of a high standard, but I struggle to see the reasons why. Using templates sometimes seems to be more of a pain than not!


For example:

<xsl:template match="doc:Para">
<p>
<xsl:choose>
<xsl:when test="ancestor::doc:Body">
<xsl:number count="doc:Para" from="doc:Body" level="any"/>
<xsl:text>. </xsl:text>
<xsl:call-template name="prefix"/>
<xsl:apply-templates/>
</xsl:when>
<xsl:when test="ancestor::doc:appendix">
<xsl:element name="font" use-attribute-sets="attSetAppendixBody">
<xsl:call-template name="prefix"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="prefix"/>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</p>
</xsl:template>


should it seems, not use the <xsl:choose>, but what benefit does this give?

Also,

<xsl:template match="doc:title">
<xsl:choose>
<xsl:when test="parent::doc:Table">
<xsl:apply-templates/>
</xsl:when>
<xsl:when test="ancestor::doc:Summary">
<xsl:choose>
<xsl:when test="../../../../doc:Section">
<xsl:choose>
<xsl:when test="../../../../../../../doc:Section">
<p>
<xsl:element name="font" use-attribute-sets="level3attrtitle>
<xsl:apply-templates/>
</
</p>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:element name="font" use-attribute-sets="level2attrtitle>
<xsl:apply-templates/>
</
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:element name="font" use-attribute-sets="level1attrtitle>
<xsl:apply-templates/>
</
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="../../../../doc:Section">
<xsl:choose>
<xsl:when test="../../../../../../../doc:Section">
<p>
<xsl:element name="font" use-attribute-sets="level3attrtitle>
<xsl:apply-templates/>
</
</p>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:element name="font" use-attribute-sets="level2attrtitle>
<xsl:apply-templates/>
</
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:element name="font" use-attribute-sets="level1attrtitle>
<xsl:apply-templates/>
</
</p>
</xsl:otherwise>
</xsl:choose>


etc
etc

So the question is essentially - this code doesn't use templates as much as it should (right?), but what are the benefits of using templates in what to a novice seems a gratuitous way?

Thanks,

Ian

Current Thread