Professional Documents
Culture Documents
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.
<?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.
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.
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: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.
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.