Variable scope : variable « XSLT stylesheet « XML Tutorial






File: Data.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- testlines.xml -->
<ul>
  <li>A</li>
  <li>B</li>
  <li>C</li>
</ul>

File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:apply-templates select="ul/li"/>
  </xsl:template>

  <xsl:template match="li">
    <xsl:variable name="single-quote">
      <xsl:text>&apos;</xsl:text>
    </xsl:variable>
    <xsl:variable name="two-quotes">
      <xsl:text>&apos;&apos;</xsl:text>
    </xsl:variable>

    <xsl:variable name="sub1">
      <xsl:call-template name="replace-substring">
        <xsl:with-param name="original" select="."/>
        <xsl:with-param name="substring" select="'&amp;'"/>
        <xsl:with-param name="replacement" select="'^&amp;'"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="sub2">
      <xsl:call-template name="replace-substring">
        <xsl:with-param name="original" select="$sub1"/>
        <xsl:with-param name="substring" select="'|'"/>
        <xsl:with-param name="replacement" select="'^|'"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:call-template name="replace-substring">
      <xsl:with-param name="original" select="$sub2"/>
      <xsl:with-param name="substring" select="$single-quote"/>
      <xsl:with-param name="replacement" select="$two-quotes"/>
    </xsl:call-template>
    <xsl:text>&#xA;</xsl:text>
  </xsl:template>

  <xsl:template name="replace-substring">
    <xsl:param name="original" />
    <xsl:param name="substring" />
    <xsl:param name="replacement" />
    <xsl:choose>
      <xsl:when test="contains($original, $substring)">
        <xsl:value-of 
          select="substring-before($original, $substring)" />
        <xsl:value-of select="$replacement" />
        <xsl:call-template name="replace-substring">
          <xsl:with-param name="original" 
            select="substring-after($original, $substring)" />
          <xsl:with-param 
            name="substring" select="$substring" />
          <xsl:with-param 
            name="replacement" select="$replacement" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$original" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>          
  
</xsl:stylesheet>

Output:

A
B
C








5.68.variable
5.68.1.Define variable and use it
5.68.2.Fill position to a variable
5.68.3.Define and use variable
5.68.4.Variable scope
5.68.5.Variable assignment with choose statement
5.68.6.There is an important difference in variable value specification.
5.68.7.if a variable has some defined value
5.68.8.Variable without initialization
5.68.9.demonstrate different ways of setting xsl:variable
5.68.10.Use variable to hold a result tree