XML Transformations (XSLT) – Part III

XML Foundations [./]
Fall 2009 — INFO 242 (CCN 42575)

Erik Wilde, UC Berkeley School of Information
2009-10-01

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents E. Wilde: XML Transformations (XSLT) – Part III

Contents

E. Wilde: XML Transformations (XSLT) – Part III

(2) 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.



E. Wilde: XML Transformations (XSLT) – Part III

(3) XSLT Core Concepts



How to Iterate

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]
How to Iterate E. Wilde: XML Transformations (XSLT) – Part III

(5) Processing Nodes in XSLT



How to Iterate E. Wilde: XML Transformations (XSLT) – Part III

(6) Homogeneous Processing



How to Iterate E. Wilde: XML Transformations (XSLT) – Part III

(7) Heterogeneous Processing



Calling Templates

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]
Calling Templates E. Wilde: XML Transformations (XSLT) – Part III

(9) Executing Templates



Calling Templates E. Wilde: XML Transformations (XSLT) – Part III

(10) Named Templates



Variables and Parameters

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]
Variables and Parameters E. Wilde: XML Transformations (XSLT) – Part III

(12) Programming Language Basics

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


Variables

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]
Variables E. Wilde: XML Transformations (XSLT) – Part III

(14) Why Variables?

  • Reuse of values in different locations
    • texts required for the transformation
    • facilitates better separation of structure and content
    <xsl:value-of select="$email-prefix"/> <!- - $email-prefix = 'You have ' - ->
    <xsl:value-of select="count(//message)"/>
    <xsl:value-of select="$email-suffix"/> <!- - $email-suffix = ' e-mail messages.' - ->
  • Using the correct context is essential
    • variables cannot be updated
    • if they need to be updated, they have to be re-created
  • Why are they called variables if they are constants?
    • their value varies in different invocations of the context
    • they are computed at runtime (dynamic constants) rather than statically


Variables E. Wilde: XML Transformations (XSLT) – Part III

(15) Scope and Extent

  • Variables can be global or local
    • global variables are visible in all templates
    • local variables are visible in their context (i.e., at following-sibling::*/descendant-or-self::*)
    • local variables are allowed to shadow global (not local) variables
  • Variable values may be assigned using the select attribute
    • The XPath's result is the value of the variable
  • Variables can contain arbitrary XPath code
    • the code is executed in the same way as when constructing the result tree
    • the result tree fragment is the value of the variable
    • it can be used as a string (value-of) or as a tree (copy-of)


Variables E. Wilde: XML Transformations (XSLT) – Part III

(16) 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>


Parameters

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 E. Wilde: XML Transformations (XSLT) – Part III

(18) Parameters vs. Variables

  • Parameters are variables with additional semantics
    • they are passed to their scope from the outside
    • they are available within the scope like a variable (scopes are stylesheets and templates)
    • like variables, they cannot be updated (and only global parameters can be shadowed)
  • XSLT does not check proper parameter passing
    • if a declared parameter is not passed, it gets a default value (specified or '')
    • if a passed parameter is not declared, it is ignored
    • like variables, parameters have no type (any value can be passed)
    • XSLT's robustness makes it hard to spot programming errors


Parameters E. Wilde: XML Transformations (XSLT) – Part III

(19) Stylesheet Parameters

  • Passed to the stylesheet when calling the stylesheet
    • the exact way of specifying the parameters depend on the processor and the environment
    • the passed values are available in the same way as global variables
    • parameter checking has to be done by hand
 <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>


Parameters E. Wilde: XML Transformations (XSLT) – Part III

(20) Template Parameters

  • Parameters can be passed to templates
    • works with apply-templates and call-template
    • with-param elements list the passed parameters
    • parameter matching is done by name (there is no particular order to parameters)
  • Templates can be programmed as parametrized components
    • checking the signature has to be done by hand
    • with-param elements list the passed parameters
    • parameter matching is done by name (there is no particular order to parameters)
  • Parametrized template calls need a lot of markup
    • XSLT's XML syntax makes the code hard to read
main param start = 1 ; param count = 10 ; {
	loop (0) };
loop param counter ; {
	print $start + $counter ;
	if ( $counter < $count - 1) then 
		loop ($counter + 1) ; }


Parameters E. Wilde: XML Transformations (XSLT) – Part III

(21) 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>


Parameters E. Wilde: XML Transformations (XSLT) – Part III

(22) 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>


Conclusions

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]
Conclusions E. Wilde: XML Transformations (XSLT) – Part III

(24) XSLT Programming Environment



2009-10-01 XML Foundations [./]
Fall 2009 — INFO 242 (CCN 42575)