[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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
complex xsl for each
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.