More On XSLT (Templates)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Week 7 – More XSLT

Internet Technologies and Web


Services
Aims of the lecture
• At the end of today’s lecture students should
be able to
– Use XSLT to format an XML file
Classwork - book.xml
<?xml version="1.0" <book>
encoding="ISO- <title
8859-1"?> lang="eng">Learning
<bookstore> XML</title>
<price>39.95</price>
<book> </book>
<title <book>
lang="eng">Harry <title
Potter</title> lang="fr">Exploitation
<price>29.99</price> Linux</title>
</book> <price>40.00</price>
</book>
</bookstore>
Classwork

Write the XSLT to display the following


information in various tables:
1. All the information in the XML file with
corresponding headings
2. Only books which have lang=“fr”
3. All books above with the books costing
more than 35.00 highlighted in yellow
The <xsl:apply-templates>
Element

• The <xsl:apply-templates> element applies


a template to the current element or to the
current element's child nodes.
• If we add a select attribute to the <xsl:apply-
templates> element it will process only the
child element that matches the value of the
attribute. We can use the select attribute to
specify the order in which the child nodes
are processed.
The select attribute

• If a select attribute is added to the


<xsl:apply-templates> element it will
process only the child element that
matches the value of the attribute.
• The select attribute can be used to
specify the order in which the child
nodes are processed.
feedback.xslt(part 1)

<xsl:template match="/">
<html> <body> <h2>Feedback provided</h2>
<xsl:apply-templates/>
</body> </html>
</xsl:template>
<xsl:template match="module"><p>
<xsl:apply-templates select="studentemail"/>
<xsl:apply-templates
select="modulename"/></p>
</xsl:template>
feedback.xslt(part 2)
<xsl:template match="studentemail">
Student Email: <span style="color:#ff0000">
<xsl:value-of select="."/></span> <br />
</xsl:template>
<xsl:template match="modulename">
Module Name: <span style="color:#00ff00">
<xsl:value-of select="."/></span> <br />
</xsl:template>
</xsl:stylesheet>
Viewing feedback.xml in firefox
People.xml
<People>
<Person>
<Name>Winston Churchill</Name>
<Description>Winston Churchill was a..</Description>
</Person>
<Person>
<Name>Indira Gandhi</Name>
<Description>Indira Gandhi was India’s..</Description>
</Person>
<Person>
<Name>John F. Kennedy</Name>
<Description>JFK was a US president..</Description>
</Person> </People>
People.xslt – using apply-
templates
<xsl:stylesheet xmlns:xsl= <br />
"http://www.w3.org/1999/XSL/Transform" <xsl:apply-templates
version="1.0" > select="/People/Person" />
</body>
<xsl:template match="/"> </html>
<html> </xsl:template>
<head>
<title>Information about <xsl:template match="Person">
<xsl:value-of <h3>
select="count(/People/Person)"/> <xsl:value-of select="Name" /></h3>
people.</title> <p>
</head> <xsl:value-of
<body> select="Description"/></p>
<h3>Information about <br />
<xsl:value-of </xsl:template>
select="count(/People/Person)"/> </xsl:stylesheet>
people.</h3>
Viewing feedback.xml in firefox
Cdcatalog.xml
• <?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>
Cdcatalog.xsl example
• <?xml version="1.0" encoding="UTF-8"?> • <xsl:template match="title">
<xsl:stylesheet version="1.0" Title: <span style="color:#ff0000">
xmlns:xsl="http://www.w3.org/1999/XSL/Tra <xsl:value-of select="."/></span>
nsform"> <br />
</xsl:template>
<xsl:template match="/">
<html> <xsl:template match="artist">
<body> Artist: <span style="color:#00ff00">
<h2>My CD Collection</h2> <xsl:value-of select="."/></span>
<xsl:apply-templates/> <br />
</body> </xsl:template>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>

</xsl:stylesheet>
Example
• You decide to make the following changes to your XSLT: Module code is
displayed at the top of each module with a silver background.
• The values for credit and level are displayed in bold and blue colour.
• Use of templates to promote code reuse and modularise your XSLT file.
• NO SORTING - Present modules in same order as in XML file
• Below is a sample of the output you want
The code
• Source codes are found in the XSLTEx1WT
archive.
• The main template (<xsl:template
match="/">) is broken into smaller templates
where each element/attribute is processed by
its own template.
• The smaller templates usually have a match
attribute which contains the node name of the
element/attribute to process.
Code from the XSLT
• The <xsl:apply-templates/> in the main template will cause the
smaller templates to be applied to elements/attributes found in the
XML tree.
• If the apply-templates instruction has a select attribute, then only
the element/attribute that match the select attribute will be
processed. E.g., only the attribute code will be processed by the
following
• <xsl:apply-templates select="@code"/>
• The elements credit and level will be processed by the template
below which matches their path. The | will match both.
• <xsl:template match="credit|level">
• The code attribute will be processed by the following template
• <xsl:template match="@code">
The <xsl:copy> Element

 The xsl:copy element copies a node to the result


tree, but it does not copy any descendant
nodes; nor, if the context node is an element
node, does it cause any attribute nodes to be
copied.
 This can be useful when, for example, you want
to use an element but change the structure of its
content or add or remove attributes from it.
Persons.xml
<Persons>
<Person><FirstName>Jill</FirstName><LastName>Ha
rper</LastName></Person>
<Person><FirstName>Claire</FirstName><LastName>
Vogue</LastName></Person>
<Person><FirstName>Paul</FirstName><LastName>
Cathedral</LastName></Person>
</Persons>
Persons.xslt
<xsl:stylesheet
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
version=”1.0” >

<xsl:template match=”/”>
<Persons><xsl:apply-templates select=”/Persons/Person”
/></Persons>
</xsl:template>
<xsl:template match=”Person”>
<xsl:copy><xsl:attribute name=”FirstName”><xsl:value-of
select=”FirstName”/></xsl:attribute>
<xsl:attribute name=”LastName”><xsl:value-of
select=”LastName”/></xsl:attribute></xsl:copy></xsl:templ
ate></xsl:stylesheet>
Result – Personsout.xml

<?xml version=”1.0” encoding=”UTF-8”?>


<Persons>
<Person FirstName=”Jill”
LastName=”Harper”/>
<Person FirstName=”Claire”
LastName=”Vogue”/>
<Person FirstName=”Paul”
LastName=”Cathedral”/>
</Persons>
Classwork 2

 Write the XSLT that does the reverse


of the above XSLT, i.e. it converts the
attributes in Personsout.xml into
elements. (<xsl:copy> should be used
for this as well )
Classwork 3 – class.xml

• <?xml version="1.0" encoding="UTF-8"?>


• <?xml-stylesheet type="text/xsl"
• href="class.xsl"?>
• <class>
• <student id="1">Jack</student>
• <student id="2">Harry</student>
• <student id="3">Rebecca</student>
• <teacher id="1">Mr. Bean</teacher>
• </class>
Classwork 3

• Consider the file class.xml above. Write a new


class.xslt such that the id of the student followed by
the name is printed. (1 – Jack)
• <html><body>
• <p>1 - Jack</p>
• <p>2 - Harry</p>
• <p>3 - Rebecca</p>
• </body></html>
Classwork 4 – class2.xsl

• <xsl:stylesheet version="1.0"
• xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
• <xsl:template match="class"> //context is class
• <html><body><h3>List of Students</h3>
• <ol><xsl:for-each select="student">
• <li><xsl:value-of select="."/></li></xsl:for-each>
• </ol>
• </body></html>
• </xsl:template>
• </xsl:stylesheet>
Classwork 4

• Predict what will be obtained when class.xml is


styled using the above XSLT.
• Rewrite the above XSL such that <xsl:apply-
templates> is used instead of <xsl:for-each> and the
context is the root element