How to do a second transform on the output of an XSLT template
I have only basic XSLT skills so apologies if this is either basic or impossible.
I have a paginator template which is used everywhere on the site I'm looking at. There's a bug where one particular search needs to have a categoryId parameter appended to the href of the page links. I can't alter the paginator stylesheet or else i would just add a param to it. What I'd like to do is apply the template as is then do a second transform based on its output. Is this possible? How do others normally go about extending library templates?
So far I've thought about doing a recursive copy of the output and applying a template to the hrefs as they are processed. The syntax for that escapes me somewhat, particularly as I'm not even sure it's possible.
Edit - Between Dabbler's answer and Michael Kay's comment we got there. Here is my complete test.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<!-- note we require the extensions for this transform -->
<!--We call the开发者_开发技巧 template to be extended here and store the result in a variable-->
<xsl:variable name="output1">
<xsl:call-template name="pass1"/>
</xsl:variable>
<!--The template to be extended-->
<xsl:template name="pass1">
<a href="url?param1=junk">foo</a>
</xsl:template>
<!--the second pass. we lock this down to a mode so we can control when it is applied-->
<xsl:template match="a" mode="pass2">
<xsl:variable name="href" select="concat(@href, '&', 'catid', '=', 'stuff')"/>
<a href="{$href}"><xsl:value-of select="."/></a>
</xsl:template>
<xsl:template match="/">
<html><head></head><body>
<!--the node-set extension function turns the first pass back into a node set-->
<xsl:apply-templates select="ext:node-set($output1)" mode="pass2"/>
</body></html>
</xsl:template>
</xsl:stylesheet>
Here is a complete example how multi-pass processing can be done with XSLT 1.0:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*" mode="mPass2">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="mPass2"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="vrtfPass1Result">
<xsl:apply-templates/>
</xsl:variable>
<xsl:apply-templates mode="mPass2"
select="ext:node-set($vrtfPass1Result)/*"/>
</xsl:template>
<xsl:template match="num/text()">
<xsl:value-of select="2*."/>
</xsl:template>
<xsl:template match="/*" mode="mPass2">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="mPass2"/>
</xsl:copy>
</xsl:template>
<xsl:template match="num/text()" mode="mPass2">
<xsl:value-of select="3 + ."/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<nums>
<num>01</num>
<num>02</num>
<num>03</num>
<num>04</num>
<num>05</num>
<num>06</num>
<num>07</num>
<num>08</num>
<num>09</num>
<num>10</num>
</nums>
the wanted result (each num
is multiplied by 2
and in the next pass 3
is added to each num
) is produced:
<nums>
<num>5</num>
<num>7</num>
<num>9</num>
<num>11</num>
<num>13</num>
<num>15</num>
<num>17</num>
<num>19</num>
<num>21</num>
<num>23</num>
</nums>
It's possible in XSLT 2; you can store data in a variable and call apply-templates on that.
Basic example:
<xsl:variable name="MyVar">
<xsl:element name="Elem"/> <!-- Or anything that creates some output -->
</xsl:variable>
<xsl:apply-templates select="$MyVar"/>
And somewhere in your stylesheet have a template that matches Elem. You can also use a separate mode to keep a clear distinction between the two phases (building the variable and processing it), especially when both phases use templates that match the same nodes.
精彩评论