You are on page 1of 8

Lab 6 – XSLT I

Prepared by: Mohammad Bakri Che Haron

Objectives:
At the end of this lab session, students should be able to:
a. Do basic XSLT Transformation (XSLT).
b. Use XSLT template.
c. Use multiple templates.
d. Use select and match expression.

A – Basic XSLT Transformation

Preparing the documents


Create a new XML document and write this code inside it:

<?xml version="1.0"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>

Save the XML document as bookstore.xml. Next, create a XML stylesheet document and write this code:

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

</xsl:stylesheet>

Save the document as bookstore.xsl. As you can see, every XSLT style sheet is actually an XML document
in itself, and therefore should begin with a standard XML declaration. Once that’s done, we define the
W3C namespace for style sheets to enable us to use style sheet tags.

Creating the Root Template


The first thing that the XSLT processor looks for in a style sheet is the root template. This is the template
that defines the set of rules to apply to the root node of the XML document. Specifically, it describes
how to process or transform the content from the root node into some new output.
Modify your XSLT stylesheet to insert the root template.

Transforming (HTML)
Next, we need to define the set of rules for this template; the rules that will apply to the content in the
root node. Typically, in the root template, we will start by creating the structure for the final
transformed document. If our final document is HTML, we’ll want to add HTML basic structure at the
very least.

To have our XSLT processor output HTML, we will need to use the xsl:output processing instruction.
We can set the output method to either html, xml or text. If this instruction is omitted, processors
will output XML by default.

Inside the root template rule (that is, between <xsl:template match=”/”> and its matching
</xsl:template>), add the HTML that we’d like to output when this template is applied.

*because all XSL documents are XML documents, they must be well-formed. Consequently, the HTML you
use in the XSLT document must be well-formed as well.

Transform the bookstore.xml with this style sheet. Examine the output.
Outputting Values
We’ve output HTML using the root template, but out XSLT style sheet still haven’t touched the XML
content. In order to actually output the content of an XML node, we’ll use the <xsl:value-of>
element.

Try to transform bookstore.xml with this updated style sheet. Examine the output. Make sure you g
B – Use XSLT Template
Next we try to create another template other than the root template. Let say, we want to create
template for book element. We can call the template in two ways, which is using <xsl:apply-
templates> or <xsl:call-templates>. The template will output the same thing as before.

Using <xsl:apply-templates>
In <xsl:apply-templates>, the select attribute is optional. If there is no template matching
the current node, built-in template inside XSLT processor is automatically used. The built-in template
will search for a matching template for each child node of current node.

Modify you XSLT stylesheet into:

Try to transform your XML document. Make sure you get the same output as before. In the code above
we use apply-templates by selecting the book element. The XSLT processor will search for book
template, and process it.

Using <xsl:apply-templates> with multiple templates


Next, we try to add more templates to the style sheet. Let say, we want to create template for title
and author element. Then, we will use apply-templates inside the book template. This time, we
will not use the select attribute.

Modify your stylesheet as follow:


Transform your bookstore.xml again. Make sure you get the same output. In the above, what happen is
that:

 First, it will process the root template.


 Inside the room template, we use apply-templates and selecting book element(s)
 It will process the book template. Inside the book template, we use apply-templates without
select parameter.
 What happen is that, it will search for templates of child nodes, which are title, author, year and
price.
 In this case, it found only templates for title and author. It will processed both templates in
order.

Using <xsl:call-templates>
Another way of calling templates is using <xsl:call-templates>. It will call template based on the
template name. The difference with apply-templates is that it doesn’t have current node/element. So
we need to select required data from the root element.

Modify your stylesheet as follow:


Transform bookstore.xml with this new stylesheet. Make sure you get the same output.

Using <xsl:call-templates> with parameters


<xsl:call-templates> allow us to call template while supplying parameters. Parameters can be any value
or nodes. Inside <xsl:call-templates> we will put <xsl:with-param> elements that have attributes name
for parameter name, and select attribute to select which data to be sent as parameter.

Inside the template, we need to put <xsl:param> with name attribute that specify which parameter
name that the template need to receive. To use the parameter’s value inside the template, use the
parameter’s name and add a dollar sign, $, in front of it.

Modify your codes as follow:


Try to transform again your bookstore.xml and make sure you get the same output. In the above
styleshhet, we call the myTemplate template and supplying 2 values as parameters, which are title and
author, retrieved from the xml documents. The myTemplate template will receive these 2 parameters.
To use the value, put a dollar sign in front of the parameter name.

Lab Exercise – Submit as handwritten after lab session


Create a new XSL Stylesheet with XML as the output. Using the bookstore.xml, output the following
XML using XSLT. Use <xsl:apply-templates> in your solution. The format of output XML
document is:
References
XML Visual QuickStart Guide, 2 nd edition

You might also like