René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

XSLT

XSLT is a transformation language for XML, that is, it can transform documents of one XML Application into documents of another XML Application. There is also XSLF (F standing for formatting objects) [also called XSL-FO)
XSLT originates from css and DSSSL (= Document Style Semantics and Specification Language) (pronounced: dessel)
XSLT is a declarative language: we don't specify how we want to transform something, but how it must look like when transformed.
Transformation needs those three things: an XML document, a tranformation stylesheet and a software that generates a new (transformed) XML document. The stylesheet is written in XSLT.

A minimal stylesheet

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <!-- further declarations -->

</xsl:stylesheet>
Note: the xsl namespace prefix is needed to distinguish between elements that are part of the stylesheet and those used for the result tree.

XML document as a tree

Seven node types:

Rules

A rule does three things:
  • matches a node
  • specifies the structure of the resulting sub-tree
  • states branches of matched node that need processing

Template rules

In a template rule, the resulting subtree is explicitley stated in a template element.
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0"
>

  <xsl:template match="/">
    <html>
      <head><title>Some Title</title></head>
      <body>Hello world</body>
    </html>
  </xsl:template>

</xsl:stylesheet>
This example produces the same html file regardless of the input file.

The transformation process

The tranformation process works like this:
  1. The input XML file (not the stylesheet) is loaded and internally represented as a DOM tree.
  2. Walks the tree depth first
  3. yet to be finished ....

XSLT specific tags

xsl:template

rules: if the template is not a named templates it states a rule about processing.
named templates: A named template is the equivalent of a function in a programming language. xsl:call-template is used to call a template.
Parameters for named parameters are passed through the xsl:param instruction.
A template becomes a named parameter by setting its name attribute.

xsl:apply-templates

If present, this instruction prccesses all of the children of the current node. The nodes processed can be limited by setting the select attribute.

xsl:apply-imports

xsl:call-template

Used to call a xsl:template

xsl:element

xsl:attribute

xsl:value-of

select is a required attribute. The value of the select's XPath expression will be written into the output stream.

xsl:if

xsl:message

xsl:text

Writes text out. Useful when outputting non nested tags. See here.

xsl:choose

The xsl:choose ... xsl:when .... xsl:when.... xsl:otherwise construct is like a switch ... case statement in c.

xsl:when

<xsl:when test="system-property('xsl:version') >= 1.1">
  <!-- .... !>
</xsl:when>
<xsl:otherwise>

</xsl:otherwise>

xsl:otherwise

See xsl:when

xsl:import

xsl:include

Used to include other stylesheets.

xsl:strip-space

xsl:preserve-space

xsl:output

Attributes:
  • method
  • version
  • encoding
  • omit-xml-declaration
  • standalone
  • doctype-public
  • doctype-system
  • cdata-section-elements
  • indent
  • media-type

xsl:number

The attribute format can be:
  • 1: 1, 2, 3, 4...
  • 0: 0, 1, 2, 3...
  • 7: 7, 8, 9,10...
  • 01:01,02,03,03...
  • I: I,II,III,IV....
  • i: i,ii,iii,iv...
  • A: A, B, C, D...
  • a: a, b, c, d...
  • U: U, V, W, X...
See Enumerating with roman numbers for an example.

xsl:key

xsl:for-each

See Enumerating chapters for an example.

xsl:sort

<xsl:apply-templates select="item">
  <xsl:sort select="name" order="descending" data-type="number"/>
</xsl:apply-templates>

xsl:decimal-format

xsl:namespace-alias

xsl:attribute-set

xsl:variable

xsl:with-param

Used to pass parameters in a xsl:call-template call.

xsl:param

Provides parameters for xsl:template.

xsl:copy-of

xsl:stylesheet

See also xsl:transform.
Must have a version attribute.

xsl:transform

xsl:transform is a synonym for xsl:stylesheet

XSLT specific attributes

match

The value of match is a pattern.

select

The value of select is a XPath expression.

Patterns

A pattern looks like an XPath expression but it is treated differently.
A pattern is used for matching nodes in the tree against a specified criteria.

Referencing a stylesheet within an XML document

A processing instruction can be used to reference a stylesheet from within an XML document:
<?xml-stylesheet type="text/xml" href="the_stylesheet.xsl" ?>

disabling implicit templates

<xsl:template match="@*|text()">
</xsl:template>

Links

XT is a fast, free implementation of XSLT in java.
Xalan Xalan -o outputfile xmlSource stylesheet