Introduction to XSLT - Part 2

Jean-François Perrot

  1. Precedence, import, include
    1. Priority of rules
    2. Implicit and explicit priority
    3. xsl:import
    4. xsl:include
    5. Use for DocBook processing
  2. xsl:variable, xsl:param, calling templates
    1. Variables
    2. Parameters
    3. Named templates
    4. Example : a simple loop


  1. Precedence, import, include

    1. Priority of rules

      When several rules are applicable to the same node, only one will be applied.
      In principle, the rule with higher priority will be chosen, and it is an error if more than one are found.

      However, it is common practice to choose the last rule (having the highest priority) in the stylesheet (rather than signalling an error).
      Some people consider that relying on this implementation-dependent feature is wrong, others would promote it to a standard technique...

    2. Implicit and explicit priority

      • Explicit with the integer-valued priority attribute :
         <xsl:template match="xyz" priority="2">...  has higher priority than
         <xsl:template match="xyz" priority="1">...

      • Implicit priority (default priority) follows counter-intuitive rules. It is safer not to rely on them.
        The only valuable information is that default rules have lower priority than written templates...
    3. xsl:import

      <xsl:import href = "path-to-/myStylesheet.xsl" /> must be the first child of <xsl:stylesheet> (the root element)

      • imports the set of rules contained in myStylesheet.xsl into the stylesheet;
      • these rules have a lower priority than the indigenous rules.

      • If an imported rule and an indigenous rule are both applicable, the indigenous one is chosen, the imported one is overridden.
      • However, it is still possible to activate an imported rule thanks to <xsl:apply-imports/>.

      A simple example (source)
      1. XML document
      2. Base stylesheet, result
      3. Imported stylesheet (extract from the base), importing stylesheet, result
      4. importing stylesheet with reactivation, result
    4. xsl:include

      <xsl:include href = "path-to-/myStylesheet.xsl" />  can appear anywhere at the top-level of the stylesheet (i.e. as a direct child of the root).

      • Contrary to the import case, included rules have the same priority as indigenous ones.
      • If an included rule cr and an indigenous rule dr are both applicable:
        • in principle, that is an error;
        • in practice, the last found one will be chosen.
          • If the inclusion is located before the indigenous rule dr, then cr will be overriden,
            with the same logic as in the the import case. See here for an example.
          • If the inclusion is located after the indigenous rule dr, then cr will be activated,
            and dr will be neutralized. See here for an example.


    5. Use for DocBook processing

      Combining Stylesheets with Include and Import
  2. xsl:variable, xsl:param, calling templates

    (source)

    1. Variables...

      ... in XSLT are not really variables, as their values cannot be changed.
      They resemble constants from conventional programming languages.

      Declaration : <xsl:variable name="myVar" select="'myValue'" />
      Usage : <xsl:value-of select="$myVar"/>

      The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.
      Top-level variables (variables that are direct child nodes of the <xsl:stylesheet> element) can never be changed.

    2. Parameters

      are assigned a value either from the process that invoked the stylesheet (top-level parameter),
      or from a <xsl:with-param> or from a default value (in which case it behaves as if it was a variable).

    3. Named templates

      behave very much like functions in conventional programming languages.
      Recursive calls are allowed.
      The <xsl:with-param> construct implements parmeter-passing.

    4. Example : a simple loop

      Styleheet Loop.xsl

      <?xml version='1.0'?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
      <xsl:output  method="text" indent="yes" />

      <xsl:variable name="dash" select="'---'" />

      <xsl:template match="/">
          <xsl:variable name="Max" select="value" />

          <xsl:call-template name="Base">
              <xsl:with-param name="x" select="0"/>
              <xsl:with-param name="y" select="$Max"/>
          </xsl:call-template>
      </xsl:template>

      <xsl:template name="Base">
          <xsl:param name="x"/>
          <xsl:param name="y"/>
          <xsl:if test="$x &lt;= $y">
              <xsl:value-of select="concat($x, $dash)"/><xsl:text>&#10;</xsl:text>
              <xsl:call-template name="Base">
                  <xsl:with-param name="x" select="$x+1"/>
                  <xsl:with-param name="y" select="$y"/>
              </xsl:call-template>
          </xsl:if>
      </xsl:template>

      </xsl:stylesheet>



      Demo
      jfp$ more value.xml
      <?xml version='1.0'?>
      <value>5</value>
      jfp$ xsltproc Loop.xsl value.xml
      0---
      1---
      2---
      3---
      4---
      5---
      jfp$