Tuesday, 5 March 2019

Tunneling Parameters in XSLT

Tunnel Parameters

[Definition: A parameter passed to a template may be defined as a tunnel parameter. Tunnel parameters have the property that they are automatically passed on by the called template to any further templates that it calls, and so on recursively.] Tunnel parameters thus allow values to be set that are accessible during an entire phase of stylesheet processing, without the need for each template that is used during that phase to be aware of the parameter.

Example

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
   
<xsl:output omit-xml-declaration="yes"/>
   
   
<xsl:template match="/">
       
<xsl:call-template name="template1">
           
<xsl:with-param name="p1" tunnel="yes">pink</xsl:with-param>
       
</xsl:call-template>
   
</xsl:template>
   
   
<xsl:template name="template1">
       
<xsl:param name="p1">orange</xsl:param>
        1. p1 in template1:
<xsl:value-of select="$p1"/>
       
<xsl:call-template name="template2"/>
   
</xsl:template>
   
   
<xsl:template name="template2">
       
<xsl:call-template name="template3"/>
   
</xsl:template>
   
   
<xsl:template name="template3">
       
<xsl:param name="p1" tunnel="yes">blue</xsl:param>
        2. p1 in template3:
<xsl:value-of select="$p1"/>
   
</xsl:template>
   
</xsl:stylesheet>

Example
        1. p1 in template1: orange
        2. p1 in template3: pink

No comments: