What I have?
I am have an ASP.NET project in which, I have an XSLT file with many templates defined. Only one template will be used at a time depending on the user selection to display the content on a web page. It looks something like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="TemplateName"></xsl:param>
<xsl:template match="Title_Only">
...template Title_Only body...
</xsl:template>
<xsl:template match="Image_Only">
...template Image_Only body...
</xsl:template>
<xsl:template match="Title_And_Image">
...template Title_And_Image body...
</xsl:template>
</xsl:stylesheet>
What I want?
I want to pass the template name TemplateName
as a parameter and to be able apply the respective template on the data.
Can someone please tell me how can I achieve this?
You cannot use the value of a param or variable in a match pattern in XSLT 1.0. However, you could apply templates conditionally from a single template, like this:
...and then just set-up templates to match each type of node individually. Templates will be applied only to those things that match your
select
expression, which is based on the param.Example of conditionally applying templates
Applied to this input:
Produces:
...based on the value of
$TemplateName
. (Note that this example uses a variable, but the idea is the same.)Separate templates using modes
If you really need an entirely different template in each case, then use modes. This stylesheet:
Applied to this source:
Produces:
The desired template is used, based on the value of the param.
In both XSLT 1.0 and XSLT 2.0 it is illegal to have a construct like:
While XPath 3.0 (XSLT 3.0) introduces function items and HOF (higher-order functions), HOF can be emulated in previous verions of XSLT. For more information, do read the articles on the home page of FXSL.
Here is a simplified example of the idea lying behind FXSL:
Given this sample XML, we have two templates, one which produces the sum of all
num
elements and the other produces their concatenation. We want to pass the desired operation as a parameter.Here is how to do this (note that nothing in the source XML itself tells us which operation to use):
When applied on the XML document above, the wanted, correct result is produced:
when we replace:
with:
and apply the new transformation, again the wanted, correct result is produced:
Do note:
The main (using) template can and typically will be in a separate XSLT stylesheet file and will import stylesheets with templates that implement operations. The main template doesn't know what are the operations implemented (and doesn't use an
<xsl:choose>
with hardcoded names).In fact, if templates are added or removed from the imported files, there is no need to modify the main (using) template. In this style of programming the
<xsl:apply-templates>
instruction often selects for execution templates which were not yet written, when the main template was produced.Get involved with mode attribute in <xsl:apply-templates> element.