Altova Mailing List Archives


Re: Complex named template calling

From: Jeni Tennison <mail@---------------->
To:
Date: 11/23/2000 10:06:00 AM
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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.