This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

using few or many templates?


I have an XML file that looks like this:

<A>
 <B>B</B>
 <C>C</C>
 <D>
  <E>E1A</E>
  <E>E1B</E>
  <F>
   <G>G1A</G>
   <G>G1B</G>
   <G>G1C</G>
  </F> 
 </D>
 <D>
  <E>E2A</E>
  <E>E2B</E>
  <F>
   <G>G2A</G>
   <G>G2B</G>
   <G>G2C</G>
  </F>
 </D>
</A>

Which I wish to convert to html and display in a table like this:

B|   |
C|   |
 |E1A| 
 |E1B| 
 |   |G1A 
 |   |G1B 
 |   |G1C 
 |E2A| 
 |E2B| 
 |   |G2A 
 |   |G2B 
 |   |G2C 


I can accomplish it in one tightly coded template:

 <xsl:template match="A">
  <table>
  <tr><td><xsl:value-of select="B"/></td></tr>
  <tr><td><xsl:value-of select="C"/></td></tr>
  <xsl:for-each select="D">  
   <xsl:for-each select="E">
    <tr><td></td><td><xsl:value-of select="."/></td></tr>
   </xsl:for-each>
   <xsl:for-each select="F/G">
    <tr><td></td><td></td><td><xsl:value-of select="."/></td></tr>
   </xsl:for-each>
  </xsl:for-each>
  </table>
 </xsl:template>

Or with multiple templates:

 <xsl:template match="A">
  <table>
  <xsl:apply-templates select="B"/>
  <xsl:apply-templates select="C"/>
  <xsl:apply-templates select="D"/>
  </table>
 </xsl:template>
 
 <xsl:template match="B">
  <tr><td><xsl:value-of select="."/></td></tr>
 </xsl:template>
 
 <xsl:template match="C">
  <tr><td><xsl:value-of select="."/></td></tr>
 </xsl:template>
 
 <xsl:template match="D">
  <xsl:apply-templates select="E"/>
  <xsl:apply-templates select="F"/>
 </xsl:template>
 
 <xsl:template match="E">
  <tr><td></td><td><xsl:value-of select="."/></td></tr>
 </xsl:template>
 
 <xsl:template match="F">
  <xsl:apply-templates select="G"/>
 </xsl:template>
 
 <xsl:template match="G">
  <tr><td></td><td></td><td><xsl:value-of select="."/></td></tr>
 </xsl:template>

Note the first xsl fragment used no apply-templates and 
the second fragment used multiple apply-templates.
My question to the list is this: As a matter of style, how
do you trade off these two extremes in coding? From the
simple to most complex situation, the issue of how to
factor your template usage must repeatedly come up.
How do you deal with this?

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]