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++, Eclipse or Visual Studio. You can replace the data with your own. Examine the syntax and view the XML file in Internet Explorer or Firefox (Chrome has no XSLT processor). Always check that the code is free of errors, the quotes are straight, etc.

First 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 below.

<?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>

As you see, the picture files are not shown. The explanation how to add a picture is given in the next assignment.

XML document instance, example 2, 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. This transformation includes two templates, the main template, and the other that addresses node "message".

<?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.

You may want to check the CD example (in the lecture material) from www.w3schools.com.

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

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.

Some more examples about simple transformations by Roger Costello, from his Xfront.com site, (in PowerPoint format):

XML to HTML

XML to XML

XML to text

5b Example of XML to XML transformation

In the Linux environment: Copy the files CompanyA.xml, order.xsl to your Z:-Drive, 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 Putty command window and use xsltproc that is installed on edunix.metropolia.fi. Give the file names in the following order:

xsltproc -o CompanyB.xml order.xsl CompanyA.xml

Other processors:

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

Saxon, instruction page for installation and use.

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>

Try to open this file in a browser. To fix the problem of not having a well-formed result, modify your stylesheet to produce a document element.

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: 27.11.2013