xsl-list
[Top] [All Lists]

Re: [xsl] calling template with name passed in a variable

2009-12-18 15:54:20
Date: Fri, 18 Dec 2009 02:14:20 GMT
From: David Carlisle <davidc(_at_)nag(_dot_)co(_dot_)uk>
Subject: Re: [xsl] calling template with name passed in a variable
Message-Id: 
<200912180214(_dot_)nBI2EKee001410(_at_)edinburgh(_dot_)nag(_dot_)co(_dot_)uk>

<xsl:template match="xsd:element[(_at_)type=$InPlaceTypesList]">
   <xsl:call-template name="@type"/> // <-- error line
</xsl:template>

why have a named template there?

To be able to call by template by name with template's name being equal to 
current value of @type.
See description of the goal below.

it would be more natural to do

<xsl:template match="xsd:element">
   <xsl:apply-templates select="@type"/>
</xsl:template>

<xsl;template match="xsd:element/@type">
 the normal code
</xsl:template>

<xsl:template match="xsd:element/@type[.=$InPlaceTypesList]" priority="2">
  code for these types.
</xsl:template>

The above makes the same template matched for any element of $InPlaceTypesList 
list.
The goal is to execute different code (have different template matched) for all 
xsd:elements with given @type's value from the $InPlaceTypesList list. But I 
would like to avoid writing by hand a separate match for each element of 
$InPlaceTypesList list like this;

<xsl:template match="xsd:element[(_at_)type='MyTypeX']">
  code for THIS type
</xsl:template>
....
<xsl:template match="xsd:element[(_at_)type='MyTypeY']">
  code for THIS type
</xsl:template>


I would like be able to extend xsl easily by adding new types to 
InPlaceTypesList param and to add new templates handling these new types with 
templates' names being the same as the names of new types like this;

<xsl:param name="InPlaceTypesList" 
select="('MyTypeX','MyTypeY','MyNewType1','MyNewType2')"/>

<xsl:template name="MyNewType1">
  code for THIS type
</xsl:template>

<xsl:template name="MyNewType2">
  code for THIS type
</xsl:template>


To put this another way; I want to isolate adding templates (with purpose of 
handling new types) from code needed to invoke (match) them.

Incidetally if that's the only use for InPlaceTypesList it is easier and
more efficent to have a list of strings rather than elements.

<xsl:param name="InPlaceTypesList" select="('MyTypeX','MyTypeY')"/>

Ok.


Regards
Piotr Dobrogost

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--