- Forward


eXtensible Stylesheet Language Transforms (XSLT)
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

A Brief History of XSL
Back SMYC Forward
  • The developers of XSL were interested in creating a method of defining the formatting/presentation of XML documents
  • The original XSL proposal was submitted to the W3C in August of 1997
  • In early 1999 it became apparent that it would be better to think of XSL as two distinct components, XSL Transformations (XSLT) and XSL Formatting Objects (XSL-FO)
    • XSL-FO is similar in spirit to other types of stylesheets (including CSS and DSSSL)
    • XSLT is a language for selecting and processing the nodes in a document
Overview
Back SMYC Forward
  • XSLT is a Data-Driven (as Opposed to Control-Driven) Language:
    • A program consists of a set of template rules, each of which describes how a particular element type should be processed
    • Rules can appear in any order
  • Syntax:
    • Rules in an XSLT program are described using XML
    • All XSL tags are in the xsl namespace
Overview (cont.)
Back SMYC Forward
  • Side-Effects:
    • XSLT is free of side-effects
  • Input:
    • Any XML document
  • Output:
    • The most common output is HTML
    • XML
    • Text
A Simple Example
Back SMYC Forward

The Input

xsltexamples/ilovejmu/jmu.xml
 
A Simple Example (cont.)
Back SMYC Forward

When the conceptual (as opposed to actual) root element is encountered, we want to build the skeleton of the HTML document.

xsltexamples/ilovejmu/simple.xsl (Fragment: 0)
 
A Simple Example (cont.)
Back SMYC Forward

When a text (i.e., actual root) element is encountered, we want to insert a P element into the HTML skeleton, and insert the contents of the text element in the P.

xsltexamples/ilovejmu/simple.xsl (Fragment: 1)
 
A Simple Example (cont.)
Back SMYC Forward

The Complete Program Click here for a demonstration.

xsltexamples/ilovejmu/simple.xsl
 
The xsl:stylesheet Element
Back SMYC Forward
  • Purpose:
    • The actual root element of all XSLT programs
  • For Some Versions of IE:
    •         <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"
                                 result-ns="html">
              
  • In General:
    •         <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                                 version="1.0">
      
              <xsl:output method="html" indent="yes"/>
      	
The xsl:template Element
Back SMYC Forward
  • Purpose:
    • Defines a template (or rule) for producing output
  • Attributes:
    match The pattern that determines which nodes will be processed by this template.
The xsl:apply-templates Element
Back SMYC Forward
  • Purpose:
    • Indicates that the processor should search for and apply matching templates
  • Attributes:
    select The pattern that determines which nodes will be processed. If this attribute is omitted, all nodes will be processed.
The xsl:value-of Element
Back SMYC Forward
  • Purpose:
    • Returns an XML node
  • Attributes:
    select The pattern that determines which nodes will be processed. If this attribute is omitted, the current node will be returned. If more than one node is matched, only the first will be returned.
An Example
Back SMYC Forward

The Input

xsltexamples/bibliography/version1/bibliography.xml
 
An Example (cont.)
Back SMYC Forward

The Program Click here for a demonstration.

xsltexamples/bibliography/version1/reference.xsl
 
Path Expressions
Back SMYC Forward
  • Purpose:
    • Describe a sequence of "branches" in a document tree (i.e., a way of "finding" a node in the document tree.
  • Operators:
    / The child operator. It selects elements that are children of the specified node. For example, reference/year refers to the year element that is a child of the reference element.
    . The current node operator. For example, ./year refers to the year element that is a child of the current element.
    @ The attribute operator. For example, train/@number refers to the number attribute of the train element.
    * The wildcard operator. For example, timetable/* refers to all children of the timetable element.
    [] The index operator (1-based).
  • Context Functions:
    • position() returns the index of the current node (1-based)
    • last() returns the index of the last node
Path Expressions: An Example
Back SMYC Forward

The Input

xsltexamples/timetable/v1/amtrak.xml
 
Path Expressions: An Example (cont.)
Back SMYC Forward

The Program Click here for a demonstration.

xsltexamples/timetable/v1/timetable.xsl
 
Filter Operators
Back SMYC Forward
  • Purpose:
    • Allow elements to be selected
  • Some Selection Criteria:
    • The existence of child nodes
    • The value of a node
    • The existence of an attribute
    • The value of an attribute
  • Syntax:
    • [ operator pattern ]
    • where operator denotes a filter operator and pattern denotes a filter pattern
Filter Operators (cont.)
Back SMYC Forward
= The equal value operator. For example, time[@status = 'Ar'] refers to a time element with a status attribute equal to "Ar".
> The greater than value operator.
< The less than value operator.
>= The greater than or equal to value operator.
<= The less than or equal to value operator.
!= The not equal to value operator.
The xsl:if Element
Back SMYC Forward
  • Purpose:
    • Conditional processing
  • Attributes:
    test The pattern that determines the value of the test condition.
The xsl:if Element (cont.)
Back SMYC Forward

An Example

xsltexamples/bibliography/version2/reference.xsl (Fragment: 0)
 
The xsl:choose Element
Back SMYC Forward
  • Purpose:
    • Conditional processing
  • Contains:
    • One or more xsl:when elements
    • At most one xsl:otherwise element
  • Attributes:
    • Like xsl:if
The xsl:choose Element (cont.)
Back SMYC Forward

An Example

xsltexamples/bibliography/version2/reference.xsl (Fragment: 1)
 
Default Rules
Back SMYC Forward
  • Continue Processing:
    • To allow recursive processing to continue in the absence of a match to an explicit rule there is a default rule that is equivalent to:
    • <xsl:template match="*|/">
        <xsl:apply-templates/>
      </xsl:template>
      	  
  • Copy Through:
    • To copy text and attribute nodes there is a default rule that is equivalent to:
    • <xsl:template match="text()|@*">
        <xsl:value-of select="."/>
      </xsl:template>
      	  
A Complete Example
Back SMYC Forward

The Program Click here for a demonstration.

xsltexamples/bibliography/version2/reference.xsl
 
The Timetable Example with CSS Formatting
Back SMYC Forward

For Those Who Are Interested Click here for a demonstration.

xsltexamples/timetable/v2/timetable.xsl
 
There's Always More to Learn
Back -