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

Subject: Re: [xsl] calling template with name passed in a variable
From: Piotr Dobrogost <pd@xxxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 18 Dec 2009 21:53:39 +0100
Date: Fri, 18 Dec 2009 02:14:20 GMT
From: David Carlisle <davidc@xxxxxxxxx>
Subject: Re: [xsl] calling template with name passed in a variable
Message-Id: <200912180214.nBI2EKee001410@xxxxxxxxxxxxxxxxxxx>

<xsl:template match="xsd:element[@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[@type='MyTypeX']">
  code for THIS type
</xsl:template>
....
<xsl:template match="xsd:element[@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

Current Thread