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

Re: Ann: Generic Templates -- min(), max(), binSearch(

Subject: Re: Ann: Generic Templates -- min(), max(), binSearch(), sort()
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 25 May 2001 09:25:09 +0100
binsearch
Dimitre Novatchev wrote:
> I have submitted to the EXSL list a number of generic templates
> implementing generic functions as specified in the subject.

One of the neat ideas in these implementations is a possible answer to
the frequently asked question "how do I call templates dynamically?" -
how to choose the name of the template that you want to call based on
a variable.

One answer is to add a match pattern to the template that matches a
(single) node that acts as a proxy or UID for the template.  Dimitre's
examples use a specially-defined node at the top level of the
stylesheet.  For named templates, another approach would be to use the
xsl:template elements themselves, e.g.:

<xsl:template name="my:named-template"
              match="xsl:template[@name = 'my:namedTemplate']">
   ...
</xsl:template>

You can then apply templates to the relevant xsl:template element to
have the same kind of effect as calling the template.  I would
probably set up a global variable to hold the template elements:

<xsl:variable name="templates" select="document('')/xsl:template" />

[Note: this only gets the templates in the current physical
stylesheet, not any that have been imported/included, which could be a
big drawback - is this something that could be addressed with the
closure() function that Christian Nentwich suggested?]

And then filter that to get the relevant one:

  <xsl:apply-templates select="$templates[@name = $template-name]" />

[Note: you could use the template elements as nodes with Dimitre's
generic templates, but they'd be passed as a parameter rather than
have templates applied to them.]
  
The big difference between applying templates in this way and calling
the template is that the current node and position() within the
template body will be the xsl:template element and 1 respectively.
You can get around this problem by passing in parameters holding these
values in the context in which the template is called/applied:

  <xsl:apply-templates select="$templates[@name = $template-name]">
     <xsl:with-param name="current" select="." />
     <xsl:with-param name="position" select="position()" />
  </xsl:apply-templates>
  
I'm not sure if this is new, but it's not something that we've been
giving as an answer to the 'dynamically calling templates' FAQ, so I
thought it would be worth describing.

Cheers,

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.