[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: Complex named template calling


Subject: Re: Complex named template calling
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 23 Nov 2000 10:05:31 +0000

Stephen,

> Now, I want these templates to be called from various places over xml with
> wide differences.
>
> <xsl:for-each select="AUTHOR">
>   <xsl:call-template name="TABLEDEF">
>      <TR>
>       <TD>NAME</TD>
>       <TD>
>          <xsl:for-each select="BOOK">
>             <xsl:call-template name="DATADEF"/>
>             <br/>
>          </xsl:for-each>
>        </TD>
>      </TR>
>    </xsl:call-template>
> </xsl:for-each>
>
> I know this is non-functional as call-templates cannot handle subelements 
> besides params.  Is there a way to simulate this functionality somehow?

Well, you can place the content that you want to pass to the template
within a parameter:

<xsl:for-each select="AUTHOR">
  <xsl:call-template name="TABLEDEF">
     <xsl:with-param name="content">
        <TR>
         <TD>NAME</TD>
         <TD>
            <xsl:for-each select="BOOK">
               <xsl:call-template name="DATADEF"/>
               <br/>
            </xsl:for-each>
          </TD>
        </TR>
     </xsl:with-param>
  </xsl:call-template>
</xsl:for-each>

and then rather than applying templates within the named template,
copy the value of the parameter as the content:

<xsl:template name="TABLEDEF">
  <xsl:param name="content" />
  <table>
     <xsl:copy-of select="$content" />
  </table>
</xsl:template>

I guess the reason that you want the TABLEDEF named template is that
you want to keep the definition of the table element in one place.
There are a couple of other methods that you might want to think
about. The first is to use an attribute set to centralise the
attribute definitions for the table element:

<xsl:attribute-set name="table">
  <xsl:attribute name="class">...</xsl:attribute>
  ...
</xsl:attribute-set>

then you create the table element locally and add that set of
attributes:

<xsl:for-each select="AUTHOR">
  <table xsl:use-attribute-sets="table">
     <TR>
      <TD>NAME</TD>
      <TD>
         <xsl:for-each select="BOOK">
            <xsl:call-template name="DATADEF"/>
            <br/>
         </xsl:for-each>
       </TD>
     </TR>
  </table>
</xsl:for-each>

This is quite good because it allows you to create exceptions quite
easily: if you discover that actually one table needs to have slightly
different attributes than are given by the central definition, you can
always add those attributes directly to override it.

The second thing you might think of doing is using moded templates,
which explicitly have information about what the current node is.  For
example:

<xsl:template match="AUTHOR">
  <xsl:apply-templates select="." mode="TABLEDEF" />
</xsl:template>

<!-- produces a table for *any* element -->
<xsl:template match="*" mode="TABLEDEF">
  <table>
    <xsl:apply-templates select="." mode="table-content" />
  </table>
</xsl:template>

<!-- produces table content specifically for AUTHOR elements -->
<xsl:template match="AUTHOR" mode="table-content">
   <TR>
    <TD>NAME</TD>
    <TD>
       <xsl:for-each select="BOOK">
          <xsl:call-template name="DATADEF"/>
          <br/>
       </xsl:for-each>
     </TD>
   </TR>
</xsl:template>

I hope this gives you some ideas,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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



Current Thread
Keywords
xml