XML Transformations (XSLT) – Part III

XML Foundations (INFO 242)

Erik Wilde, UC Berkeley School of Information
2007-10-02
Creative Commons License

This work is licensed under a CC
Attribution 3.0 Unported License

Abstract

XSLT's template matching mechanism lets the XSLT processor find the best match to process a selected node. XSLT also supports a more traditional way of using templates, where they are called in a way very similar for function calls in most programming languages. Another interesting area of XSLT are variables and parameters, which are used for storing or passing values within XSLT code. One special property of XSLT variables is that they cannot be changed, which is a result of the functional design of the language.

XSLT Core Concepts

Outline (How to Iterate)

  1. How to Iterate [3]
  2. Calling Templates [2]
  3. Variables and Parameters [9]
    1. Variables [3]
    2. Parameters [5]
  4. Conclusions [1]

Processing Nodes in XSLT

Homogeneous Processing

Heterogeneous Processing

Outline (Calling Templates)

  1. How to Iterate [3]
  2. Calling Templates [2]
  3. Variables and Parameters [9]
    1. Variables [3]
    2. Parameters [5]
  4. Conclusions [1]

Executing Templates

Named Templates

Outline (Variables and Parameters)

  1. How to Iterate [3]
  2. Calling Templates [2]
  3. Variables and Parameters [9]
    1. Variables [3]
    2. Parameters [5]
  4. Conclusions [1]

Programming Language Basics

<xsl:variable name="sum" select="$op1 + $op2"/>
<xsl:variable name="result" select="$sum * $factor"

Outline (Variables)

  1. How to Iterate [3]
  2. Calling Templates [2]
  3. Variables and Parameters [9]
    1. Variables [3]
    2. Parameters [5]
  4. Conclusions [1]

Why Variables?

Scope and Extent

Using Variables

 <xsl:variable name="name" select="'value'"/>
 <xsl:template match="/">
  <xsl:choose>
   <xsl:when test="$name = 'value'">
    <xsl:variable name="result" select="'ok'"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:variable name="result" select="'problem'"/>
   </xsl:otherwise>
  </xsl:choose>
  <xsl:value-of select="$result"/>
 </xsl:template>
 <xsl:variable name="name" select="'value'"/>
 <xsl:template match="/">
  <xsl:variable name="result">
   <xsl:choose>
    <xsl:when test="$name = 'value'">
     <xsl:value-of select="'ok'"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="'problem'"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="$result"/>
 </xsl:template>

Outline (Parameters)

  1. How to Iterate [3]
  2. Calling Templates [2]
  3. Variables and Parameters [9]
    1. Variables [3]
    2. Parameters [5]
  4. Conclusions [1]

Parameters vs. Variables

Stylesheet Parameters

 <xsl:param name="number" select="0"/>
 <xsl:variable name="number-param">
  <xsl:if test="string(number($number)) = 'NaN'">
   <xsl:message terminate="yes">"number" must be a proper number!</xsl:message>
  </xsl:if>
  <xsl:value-of select="$number"/>
 </xsl:variable>
 <xsl:template match="/">
  <xsl:value-of select="10 * $number-param"/>
 </xsl:template>

Template Parameters

main param start = 1 ; param count = 10 ; {
	loop (0) };
loop param counter ; {
	print $start + $counter ;
	if ( $counter < $count - 1) then 
		loop ($counter + 1) ; }

Parameter Passing

 <xsl:param name="start" select="1"/>
 <xsl:param name="count" select="10"/>
 <xsl:template match="/">
  <xsl:call-template name="loop">
   <xsl:with-param name="counter" select="0"/>
  </xsl:call-template>
 </xsl:template>
 <xsl:template name="loop">
  <xsl:param name="counter"/>
  <xsl:message>
   <xsl:value-of select="$start + $counter"/>
  </xsl:message>
  <xsl:if test="$counter &lt; $count - 1">
   <xsl:call-template name="loop">
    <xsl:with-param name="counter" select="$counter + 1"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>

Message Facility

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="message" select="'plain'"/>
    <!-- messaging style: 'silent', 'plain', or 'prefixed' -->
    <xsl:template name="message">
        <xsl:param name="level" select="'fatal'"/>
        <!-- message level: 'warning', 'error', or 'fatal' -->
        <xsl:param name="text" select="'no text given'"/>
        <xsl:choose>
            <xsl:when test="$message = 'silent'"/>
            <xsl:when test="$level = 'warning'">
                <xsl:call-template name="print_message">
                    <xsl:with-param name="level" select="'warning'"/>
                    <xsl:with-param name="terminate" select="'no'"/>
                    <xsl:with-param name="text" select="$text"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$level = 'error'">
                <xsl:call-template name="print_message">
                    <xsl:with-param name="level" select="'error'"/>
                    <xsl:with-param name="terminate" select="'no'"/>
                    <xsl:with-param name="text" select="$text"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$level = 'fatal'">
                <xsl:call-template name="print_message">
                    <xsl:with-param name="level" select="'fatal'"/>
                    <xsl:with-param name="terminate" select="'yes'"/>
                    <xsl:with-param name="text" select="$text"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="print_message">
                    <xsl:with-param name="level" select="'fatal'"/>
                    <xsl:with-param name="terminate" select="'no'"/>
                    <xsl:with-param name="text" select="concat('fatal:unknown level for message [', $text, ']')"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="print_message">
        <!-- this template should never be called from outside; for printing messages, call the "message" template. -->
        <xsl:param name="level"/>
        <xsl:param name="terminate"/>
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="$message = 'plain'">
                <xsl:message terminate="{$terminate}">
                    <xsl:value-of select="$text"/>
                </xsl:message>
            </xsl:when>
            <xsl:otherwise>
                <xsl:message terminate="{$terminate}">
                    <xsl:value-of select="concat($level, ':', $text)"/>
                </xsl:message>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Outline (Conclusions)

  1. How to Iterate [3]
  2. Calling Templates [2]
  3. Variables and Parameters [9]
    1. Variables [3]
    2. Parameters [5]
  4. Conclusions [1]

XSLT Programming Environment