MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

XSLT

XSLT is a declarative language, written in XML, that specifies transformation rules for XML fragments. XSLT can convert any arbitrary XML document into XHTML or another XML format (e.g., different metadata formats). For example, the author tag can be converted as follows:

<template match="uct:author">
   <dc:creator>
      <value-of select="."/>
   </dc:creator>
</template>
    

XSLT Templates

Templates of replacement XML are specified, with matching criteria, using XPath expressions. XSLT processors attempt to match the root XML tag with a template. If this fails they descend one level and try to match each of the root's children, and so on. In the previous example, all occurrences of the "uct:author" tag will be replaced by the contents of the template. Special tags in the XSL namespace are used to perform additional customisation, for example, value-of.

XSLT Special Tags

  • value-of, text, element: Create nodes in result document.

  • apply-templates, call-template: Explicitly apply template rules.

  • variable, param, with-param: Local variables and parameter passing.

  • if, choose, for-each: Procedural language constructs.

XSLT Language

  • value-of is replaced with the textual content of the nodes identified by the XPath expression. For example:

    <value-of
    	    select="uct:title"/>

  • text is replaced by the textual content. Plain text is usually sufficient. For example:

     <text>1.0</text>
    	    1.0

  • element is replaced by an XML element with the indicated tag. Usually the actual tag can be used. Example:<element name="dc:publisher">UCT</element> <dc:publisher>UCT</dc:publisher>

  • apply-templates explicitly applies templates to the specified nodes. Example: apply-templates select="uct:version"/>

  • call-template calls a template in a similar way to calling a function. This template may have parameters and must have a name attribute instead of a match. Example:<call-template name="doheader"> <with-param name="lines">5</with-param> </call-template> <template name="doheader"> <param name="lines">2</param> ... </template>

  • variable sets a local variable. In XPath expressions, a $ prefix indicates a variable or parameter instead of a node. Example:<variable name="institution">UCT</variable> <value-of select="$institution"/>

  • Selection and iteration examples:<if test="position()=last()">...</if> <choose> <when test="$val=1">...</when> <otherwise>...</otherwise> </choose> <for-each select="uct:number">...</for-each>

XSLT Example

<stylesheet version='1.0'
 xmlns='http://www.w3.org/1999/XSL/Transform'
 xmlns:oaidc='http://www.openarchives.org/OAI/2.0/oai_dc/'
 xmlns:dc='http://purl.org/dc/elements/1.1/'
 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xmlns:uct='http://www.uct.ac.za'
>

<!--
   UCT to DC transformation
   Hussein Suleman
   v1.0 : 24 July 2003
-->

   <output method="xml"/>

   <variable name="institution"><text>UCT</text></variable>
   <template match="uct:uct">
      <oaidc:dc xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
                http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
         <dc:title><value-of select="uct:title"/></dc:title>
         <apply-templates select="uct:author"/>
         <element name="dc:publisher">
            <value-of select="$institution"/>
         </element>
         <apply-templates select="uct:version"/>
      </oaidc:dc>
   </template>

   <template match="uct:author">
      <dc:creator>
         <value-of select="."/>
      </dc:creator>
   </template>

   <template match="uct:version">
      <dc:identifier>
         <value-of select="uct:number"/>
      </dc:identifier>
   </template>

</stylesheet>

This is not the simplest XSLT that solves the problem. The transformed XML looks like this:

<?xml version="1.0"?>
<oaidc:dc xmlns:oaidc="http://www.openarchives.org/OAI/2.0/oai_dc/"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:dc="http://purl.org/dc/elements/1.1/"
          xmlns:uct="http://www.uct.ac.za"
          xsi:schemaLocation=
             "http://www.openarchives.org/OAI/2.0/oai_dc/
                 http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
  <dc:title>test XML document</dc:title>
  <dc:creator>Pat Pukram</dc:creator>
  <dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">UCT</dc:publisher>
  <dc:identifier>1.0</dc:identifier>
</oaidc:dc>