Introduction to XML

XSLT language

Download a short reference to XSLT and XPath.

Practice 5a: XSLT - transformation examples

Create these two files (below) in the same folder using Notepad or Visual Studio. You can replace the data with your own. Examine the syntax and view the XML file in Internet Explorer and Mozilla or Firefox. Always check that the code is free of errors (I may accidentally leave typos in my examples, sorry..), the quotes are straight, etc.

XML document instance, example 1, name this greeting.xml.

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="message.xsl"?>

<message type="final">
<greeting>So long and thanks for all the fish!</greeting>
</message>

Above you see the XML-file, and below its transformation, name it message.xsl.

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

<xsl:template match="/">

<html>
<xsl:apply-templates/>
</html>

</xsl:template>

<xsl:template match="message">

<head><title>
<xsl:value-of select="@type"/>
message
</title></head>
<body><p><xsl:value-of select="."/></p></body>

</xsl:template>

</xsl:stylesheet>

Above the select="." takes the content of a node set, including all children. This you could test by adding more levels.

Study also the logic of following examples: save them and open in a browser window.

Next example: XML document instance: animals.xml

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="animals.xsl"?>

<creatures>
    <animal>
        <name>Penny</name>
        <species>cat</species>
        <picture>penny.jpg</picture>
    </animal>
<animal>
        <name>Mette</name>
        <species>dog</species>
        <picture>mette.jpg</picture>
    </animal>
</creatures>

Transformation: animals.xsl

<?xml version="1.0" ?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <html>
       <head><title>Animals  - for-each</title></head>
    <body><table border="1">
    <xsl:for-each select="creatures/animal">
    <tr><td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="species"/></td>
        <td><xsl:value-of select="picture"/></td></tr>
</xsl:for-each>
</table></body></html>
</xsl:template>
</xsl:stylesheet>

XML document instance, example 3

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="tiny.xsl"?>

<tiny>
    <line>
        This line is printed as output
     </line>
 </tiny>

Transformation: tiny.xsl that uses the default template for apply-templates.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="line">
    <html>
       <head><title>
          Tiny example of apply-templates
       </title></head>
    <body>
        <xsl:apply-templates/>
     </body></html>
   </xsl:template>
 </xsl:stylesheet>

See also a modified version of these files: tiny2.xml and tiny2.xsl.

5b Example of XML to XML transformation

Copy the files CompanyA.xml, order.xsl and the processor msxsl.exe or xt.exe to your hard disk, in a folder with a simple name like temp. They are in the directory: http://users.metropolia.fi/~jaanah/ElDocCP/material
This example is from the book  "Beginning XML". The transformation takes a product order in XML format  from company A, and transforms it to the XML format of company B's orders.  Study file structures first. Run the transformation in the command window (MS-DOS prompt or equivalent) as follows 

D:\temp>MSXSL  CompanyA.xml order.xsl -o CompanyB.xml

With option -v it will set validateOnParse to True, and it will validate the document against its DTD or schema.

Other processors:

A free source program is xt, that is available from http://www.blnz.com/xt/.

In the Linux environment: use xsltproc that is installed on shell.metropolia.fi. Give the file names in reverse order:

$xsltproc order.xsl CompanyA.xml

Saxon, instruction page for installation and use.

Some more examples about simple transformations by Roger Costello (in PowerPoint format):

XML to HTML

XML to XML

XML to text

5c The following example with a Moomin file could be developed to make a simple cipher. Create the XML file and transformations which encrypt and decrypt the text. (This example is also in the lecture notes)

<?xml version="1.0" encoding="UTF-8"?>
<crew>
<member>Mamma</member>
<member>Pappa</member>
<member>Moomintroll</member>
</crew>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/crew">
<FAMILY>
<xsl:apply-templates/>
</FAMILY>
</xsl:template>
<xsl:template match="member">
<CREATURE>
<xsl:value-of select="translate(current(),
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
</CREATURE>
</xsl:template>
</xsl:stylesheet>

Practice 5 d-f: write your own XSL transformations 

Based on the given examples, start making your own transformations. Select minimum two of the exercises here. If you make more, you get extra points. Add comments to your solution. If your XMl has mixed content elements (text and children), the text content can be specified by select="text()"

5d

Design a transformation which sorts the elements of an XML in alphabetical order. The sort command looks like:
<xsl:sort select="." data-type="text" order="ascending"/>

the sorting program will look something like this:
<xsl:template match="//elements">
<ul>
<xsl:for-each select="element" >
<xsl:sort select="." data-type="text" order="ascending"/>
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</xsl:template>

5e

  Several XML files can be combined into one output document. You see below one example how a "staff" document is made from the "crew" and "gears". Modify it using your own data.

<?xml version="1.0" encoding="UTF-8"?>
<crew>
<member gear="mamman.xml">Mamma</member>
<member gear="papan.xml">Pappa</member>
<member>Moomintroll</member>
</crew>

mamman.xml is very simple
<gear>
handbag
</gear>

The transformation is as follows:

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

<xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>

<xsl:template match="member">
<staff>
<creature>
<xsl:value-of select="."/>
</creature>
<xsl:if test="@gear">
<xsl:apply-templates select="document(@gear)"/>
</xsl:if>
</staff>
</xsl:template>

<xsl:template match="gear">
<gear>
<xsl:value-of select="."/>
</gear>
</xsl:template>

</xsl:stylesheet>

5f.   Make an XSLT stylesheet that transforms the file catalog.xml into HTML. Note that this is a bit tricky compared to other exercises.

 

| HOME | SYLLABUS | CALENDAR AND EXERCISES |

Created by: Jaana Holvikivi
Updated: 4.2.2010