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>
The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.
The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a "select" attribute to the <xsl:apply-templates> element, it will process only the child elements that matches the value of the attribute.
XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation . The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology perspective.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With