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

Sunday 24 February 2019

Token a string with spaces into Nested-List


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vis="vishFunction"
    exclude-result-prefixes="xs vis"
    version="2.0" >
   
<xsl:variable name="assets" select="'123 234 456 789 0001'"/>
   
<xsl:output method="html"></xsl:output>
   
<xsl:template match="/">
       
<xsl:sequence select="vis:nestedList($assets)"/>
   
</xsl:template>
   
   
<xsl:function name="vis:nestedList">
       
<xsl:param name="assetID"/>
       
<ul>
           
<li>
               
<xsl:variable name="firstToken" select="tokenize(normalize-space($assetID),' ')[1]"/>
               
<xsl:value-of select="$firstToken"/>
               
<xsl:variable name="restAssets" select="normalize-space(substring-after($assetID,$firstToken))"/>
               
<xsl:if test="string-length(normalize-space($restAssets)) != 0">
                   
<xsl:message><xsl:value-of select="$restAssets"/></xsl:message>
               
<xsl:sequence select="vis:nestedList($restAssets)"/>
               
</xsl:if>
           
</li>
       
</ul>
   
</xsl:function>
</xsl:stylesheet>