Variables

<xsl:variable name="" select="" >
  • To save a node-set, string, number, boolean or result tree fragment for later use
  • Variables are single assignment (no side effects) mathematical variables
  • Can be global, or local to a template and are lexically scoped
  • Variable accessed by placing $ in front of name
<xsl:variable name="varempty" /> No selection made, empty
<xsl:variable name="varnumber" select="23" /> Recognised as number
<xsl:variable name="varstring" select="'a string'" />Quotes indicate string
<xsl:variable name="varfragment">
  <cd><artist>Armstrong</artist><title>My New Album</title></cd>
</xsl:variable>

<xsl:if test="position() = last()">
  <xsl:variable name="final">Completed</xsl:variable> Invalid outside if
</xsl:if>

<xsl:variable name="final">  Variable final can be used ouside if
<xsl:if test="position() = last()">Completed</xsl:if>
</xsl:variable>

<xsl:value-of select="$varstring" /> Using a variable
<xsl:copy-of select="$varfragment" />

<variable name="template_result"> Pass value out of template
<apply-templates select="cdlist" /> Result to the variable not Result Tree
</xsl:variable>

Uses of Variables

  • Constant values that you use in many places
  • Keeping context information for later use
  • Parameter passing both into and out of templates
  • Simplify stylesheets in terms of readability
  • Improve stylesheets in terms of efficiency
  • Avoid using intermediate documents

Most uses of variables fall into one of three classes:

  • Convenience: saves recalculation and makes template more readable
  • Saving current nodes
  • Holding result tree fragments to be output later

Finer Control

Various facilities are provided within XSLT to provide more control over which templates are obeyed

  • Modes: certain templates only matched when a specific mode is being matched
  • Priority: changes selection priority of templates
  • Parameters: applying or calling templates can have parameters, similar to variables, same format ($xyz)
  • Calling Templates Directly: calls a specific template with parameters
  • Recursion: invoking a template from within itself

Calling Templates

<xsl:call-template name="" >
  • xsl:call-template must have a name attribute
  • Content of xsl:call-template is a set of xsl:with-param elements
  • There must be a xsl:template defined with that name
  • Results returned via an enclosing variable
  • Result tree fragments can be output
<xsl:template name="abs">
 <xsl:param name="num" /> Must appear first
 <xsl:choose>
  <xsl:when test="0 > $num"><xsl:value-of select="0 - $num"/></xsl:when>
  <xsl:otherwise><xsl:value-of select="$num"/></xsl:otherwise>
 </xsl:choose>
</xsl:template>

<xsl:variable name="abs_sum">  How you pass value out of called template
 <xsl:call-template name="abs">
  <xsl:with-param name="num" select="sum" /> 
 </xsl:call-template>
</xsl:variable>

Parameters

<xsl:with-param name="" select="" />
  • Assigns a value to a parameter to be used by the template invoked
  • Child of either xsl:apply-templates or xsl:call-template
  • name attribute is mandatory, select attribute is optional
  • If no select attribute, content of with-param element defines value
<xsl:apply-templates select="slideset">
  <xsl:with-param name="type" select="'overview'" />
</xsl:apply-templates>

<xsl:param name="" select="" />
  • Appears straight after the xsl:template element before other children
  • select attribute can define a default value (used if no value supplied by the invoking template)
  • Acts like a local variable
<xsl:template match="slideset">
  <xsl:param name="type" />
  <xsl:if test="$type = 'overview'">
  • Parameters accessed by adding $ before the name, act as variables within the template
  • If template called recursively, there is effectively a new parameter with that name at each level of the recursion. This is the way you get real variables in XSLT

Recursion

  • A template may call itself: Recursion
  • Need to ensure that there is a way to exit from the recursion
  • Example: square roots are often found using the Newton Raphson method:
    • To find square root of N:
    • Initial GUESS is 0.5 * N
    • NEXTGUESS = 0.5 * (N / GUESS + GUESS)
    • Continue until difference between N and NEXTGUESS2 is less than error required
<xsl:variable name="sqrt_of_n">
 <xsl:call-template name="sqrt" > 
  <xsl:with-param name="n" select="number(23)" />
  <xsl:with-param name="guess" select="number(11.5)" />
  <xsl:with-param name="eps" select="number(0.000000001)" />
 <xsl:call-template>
</xsl:variable>

Square Root

<xsl:template name="sqrt">
 <xsl:param name="n" />
 <xsl:param name="guess" />
 <xsl:param name="eps" />
 
 <xsl:variable name="error">
  <xsl:call-template name="abs">
   <xsl:with-param name="num" select="$n - $guess * $guess" />
  </xsl:call-template>
 </xsl:variable>
 <xsl:choose> 
  <xsl:when test="$error > $eps">
  
   <xsl:call-template name="sqrt">
    <xsl:with-param name="n" select="$n" />
    <xsl:with-param name="guess" 
          select="number( ( ($n div $guess) + $guess) div 2)" />
    <xsl:with-param name="eps" select="$eps" />
   </xsl:call-template>
   
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="$guess" />
  </xsl:otherwise>
 </xsl:choose> 
</xsl:template>

This finite state machine was coded in XSLT to recognise numbers in SVG path expressions quickly. The finite state machine in XSLT is quite complex so the XSLT for the code of the finite state machine was generated from a description of the finite state machine. We even have a version that can adapt the finite state machine if it encounters regular formats in the numbers it is recognising. This is often necessary when processing SVG path expressions arriving from other applications where the accuracy is much greater than what is required.

digit + or - full stop comma e or E 1. Start of Coordinate 2 3 4 1 Exit 2. Expecting Further Digits 2 Exit 4 Exit Exit 3. Digits for Signed Number 3 Exit 5 Exit Exit 4. Fraction 4 Exit Exit Exit 6 5. Fraction Signed Number 5 Exit Exit Exit 6 6. Exponent 7 8 Exit Exit Exit 7. Exponent Digits 7 Exit Exit Exit Exit 8. Exponent First Digit 7 Exit Exit Exit Exit