xsl-list
[Top] [All Lists]

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

2007-06-19 14:57:23
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




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>