0% found this document useful (0 votes)
92 views9 pages

Lecture 6-XSL and XPath Continued

The document discusses key elements of the XSLT language including using xsl:value-of to select values, xsl:template to create templates for nodes, and xsl:apply-templates to apply templates to nodes. It also covers using xsl:for-each to loop through nodes and add dynamic content, filtering nodes with predicates, and sorting output with xsl:sort. The document provides examples of how to use these elements to transform XML documents with XSLT stylesheets.

Uploaded by

f_atencia
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views9 pages

Lecture 6-XSL and XPath Continued

The document discusses key elements of the XSLT language including using xsl:value-of to select values, xsl:template to create templates for nodes, and xsl:apply-templates to apply templates to nodes. It also covers using xsl:for-each to loop through nodes and add dynamic content, filtering nodes with predicates, and sorting output with xsl:sort. The document provides examples of how to use these elements to transform XML documents with XSLT stylesheets.

Uploaded by

f_atencia
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lecture 6 Last lecture…

 <xsl:value-of select=“Xpath expression”/>


– Selects the value of the element/attribute referenced using an
XPath expression and adds this value to the result document
 <xsl:template match=“Xpath expression”>
XSL: Extensible </template>
– Creates a ‘template’ with rules to be applied to the nodes
matched by the XPath expression
Stylesheet Language  <xsl:apply-templates select=“Xpath expression”/>
– Applies existing template for every node that matches the node
Cont…. named in the Xpath expression. If no template has been created
for the node, the default will apply which includes displaying all
textual content of the node.
– When used without ‘select’ attribute the processor will apply
whatever templates exist for the context node and all
descendants of the context node.

1 2
DWAX 2010.1 DWAX 2010.1

XSLT Loops XSLT Loops contd..


 In the last example in lecture last week we
started creating a table. To enable a dynamic
table showing all elements, we can use an – <xsl: for-each > example - result
xsl:for-each loop
<xsl:template match="/">
<html><body> • Loops through
<h2>Course Information</h2> each node in a
<table border="1"> specified node set
<tr bgcolor="yellow">
<th>Unit</th> <th>Lecturer</th>
</tr>
<xsl:for-each select="course/unit">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="lecturer"/></td>
</tr>
Attribute Value Description
</xsl:for-each>
select expression Required. The node set to be processed
</table></body></html>
</xsl:template>
3 4
DWAX 2010.1 DWAX 2010.1

1
XSLT Loops cont.. XSLT Loops cont..
• <xsl:for-each> Filtering information
• <xsl:for-each> Filtering information – result
<xsl:template match="/"> • Filter the output
from an XML file by
<html><body>
adding a predicate to
<h2>Course Information</h2> the node set created
<table border="1"> by the select
attribute in the
<tr bgcolor="yellow"> <th>Unit</th> <th>Lecturer</th> <xsl:for-each>
</tr> element

<xsl:for-each select="course/unit [practicals &lt;'2']">


<tr> •Legal filter operators
are:
<td><xsl:value-of select="name"/></td>
= (equal)
<td><xsl:value-of select="lecturer"/></td>
!= (not equal)
</tr>
&lt; less than
</xsl:for-each>
&gt; greater than
</table></body></html>
</xsl:template>

5 6
DWAX 2010.1 DWAX 2010.1

XSLT explained more XSLT explained more


 Inserting text: <xsl:text></xsl:text>
 XSLT Language consist of xsl:elements • used to write literal text to the output. This
element can contain literal text, entity
and xsl:attributes that allows many things; references, and #PCDATA
• Insert new elements and attributes that were not in
• Syntax:
the source document
<xsl:text disable-output-escaping="yes|no">
– Inserting Literal Result elements
<!-- Content:#PCDATA -->
– xsl:element and xsl:attribute
</xsl:text>
• Insert Text
• Numbering • disable-output-escaping=“yes|no”
• Conditionals – Optional. "yes" indicates that special characters (like
"<") should be output as is. "no" indicates that special
• Loops characters (like "<") should be output as "&lt;".
• Sorting Default is "no"

7 8
DWAX 2010.1 DWAX 2010.1

2
XSLT sorting XSLT Sorting cont…
– xsl:sort example
<xsl:template match="/">
 <xsl:sort> example - result
<html><body> <h2>Course Information</h2> • The <xsl:sort>
element is used to
<table border="1"> <tr bgcolor="yellow">
sort the output
<th>Code</th><th>Unit</th> <th>Lecturer</th> </tr>
<xsl:for-each select="course/unit">
•Note: <xsl:sort> is
<xsl:sort select=“code" order="descending"/>
always within
<tr> <xsl:for-each> or
<td><xsl:value-of select="code"/></td> <xsl:apply-
<td><xsl:value-of select="name"/></td>
templates>

<td><xsl:value-of select="lecturer"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
9 10
DWAX 2010.1 DWAX 2010.1

XSLT Sorting cont… XSL conditionals


Provides conditional processing via xsl:if and
xsl:choose with operators
 Sorting contd… = (equal) or != (not equal) or &lt; less than or &gt; greater than

• <xsl:sort > attributes (http://w3chools.com ) <xsl:template match="/">


<html><body> <h2>Course Information</h2>
<table border="1"> <tr bgcolor="yellow">
<th>Code</th><th>Unit</th>
Attribute Value Description <th>Lecturer</th></tr>
<xsl:for-each select="course/unit">
select expression Optional. An XPath expression that specifies the nodes to be sorted
<xsl:sort select=“code"/>
lang language- Optional. Specifies which language is to be used by the sort <xsl:if test="code &gt; 300125">
code
data-type text Optional. Specifies the data-type of the data to be sorted. Default is <tr>
number "text"
qname
<td><xsl:value-of select="code"/></td>
<td><xsl:value-of select="name"/></td>
order ascending Optional. Specifies the sort order. Default is "ascending" <td><xsl:value-of select="lecturer"/></td>
descending </tr>
Attribute Value Description
case-order upper-first Optional. Specifies whether upper- or lowercase letters are to be </xsl:if>
lower-first ordered first </xsl:for-each>
test expres Required. Specifies the
</table> </body></html> sion condition to be tested

11 xsl:choose explained in another example 12


DWAX 2010.1 DWAX 2010.1

3
Looping and conditional EXAMPLE:
XSL conditionals cont <xsl:template match="/">
<html>
<body>
 xsl:choose – example on later slides, also see: <h2>My CD Collection</h2>
http://www.w3schools.com/xsl/xsl_choose.asp <p>Titles:
<xsl:choose> <xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl:when test="expression"> <xsl:if test="position() < last()-1">
<xsl:text>, </xsl:text>
... some output ... </xsl:if>
<xsl:if test="position()=last()-1">
</xsl:when> <xsl:text>, and </xsl:text>
</xsl:if>
<xsl:otherwise> <xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if> An example including looping
... some output .... structure, literal output using
</xsl:for-each>
xsl:text and conditional
</xsl:otherwise> </p>
statements. Each are
</body> discussed/explained on other
</xsl:choose> </html> slides.
13 </xsl:template> 14
DWAX 2010.1 DWAX 2010.1

XSLT Numbering
Syntax: <?xml version="1.0" encoding="ISO-8859-1"?>
The <xsl:number> element is used to <xsl:number count="expression"
level="single|multiple|any" <xsl:stylesheet version="1.0"
determine the integer position of the current from="expression"
node in the source. It is also used to format a value="expression"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
number. format="formatstring" <html>
lang="languagecode“
letter-value="alphabetic|traditional" <body>
grouping-separator="character"
grouping-size="number"/> <p>
<xsl:for-each select="catalog/cd">
<xsl:template match="course/unit">
<xsl:number value="position()" format="1" />
<html><body>
<xsl:value-of select="title" /><br />
<xsl:number/>
xsl:number/>
</xsl:for-each>
<xsl:text> ....</xsl:text>
<xsl:apply-templates select ="name"/><br/> </p>
</body></html> </body>
</xsl:template> </html>
</xsl:template></xsl:stylesheet>
15 16
DWAX 2010.1 DWAX 2010.1

4
XSLT explained more XSLT explained more
 Inserting new elements and attributes – xsl:elements
and xsl:attributes
 XSL elements (W3C Recommendation XSLT
<?xml version="1.0" encoding="ISO-8859-1"?>
Version 1.0) : <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
– http://www.w3schools.com/xsl/xsl_w3celementref.asp <xsl:template match="/">
<html><body><h2>My CD Collection</h2>
 XSL functions <table border="1"><th align="left">Title</th><th align="left">Artist</th>
– Inherited Xpath functions </tr>

• Node set functions <xsl:element name=“singer">


<xsl:attribute name=“dob">
• String functions
21 April 1978
• Number functions
</xsl:attribute>
• Boolean functions </xsl:element>
– http://www.w3schools.com/xsl/xsl_functions.asp </table> </body> </html>
</xsl:template>
</xsl:stylesheet>

17 18
DWAX 2010.1 DWAX 2010.1

XSLT example XSLT example


<xsl:choose>
 This example <xsl:when test="picture =''">
– More XSLT elements in action <xsl:element name="img">
<xsl:attribute name="src">
– Shows how to add new XML document <xsl:value-of select="document('picture.xml')/pictures/picture"/>
element with an attribute to the created output </xsl:attribute>
</xsl:element>
– Use of functions to get content from a </xsl:when>
second XML document <xsl:otherwise>
<xsl:element name="img">
– Shows one way of displaying an Image file in <xsl:attribute name="src">
the output <xsl:value-of select="picture"/>
</xsl:attribute>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
19 20
DWAX 2010.1 DWAX 2010.1

5
Using document() function Using document()
 The document() function can be used to  Second XML file – picture.xml
get data from an external file <?xml version="1.0" encoding="ISO-8859-1" ?>

document('picture.xml')/pictures/picture <pictures>
<picture>robyn.jpg</picture>
– Loads the picture.xml file into memory </pictures>
(creates a DOM structure)
– Uses Xpath expression to navigate through  <xsl:value-of select="document('picture.xml')/pictures/picture"/>
the document nodes – Adds the content of the picture element to the output stream

21 22
DWAX 2010.1 DWAX 2010.1

Adding in a new Element w/attribute Using xsl:choose


<xsl:element name="img"> <xsl:choose>
<xsl:attribute name="src"> <xsl:when test="picture =''">
<xsl:value-of select="document('picture.xml')/pictures/picture"/> ......
</xsl:attribute> </xsl:when>
</xsl:element> </xsl:choose>

 Adds a new empty element (img) with an  Tests condition specified in the ‘test’ attribute
attribute (src) to the output stream.
 Here checks if the picture element is empty
 The content of the src attribute comes from the
 If test is true, the statements contained in the
external picture.xml document
xsl:when element (……) are executed.

23 24
DWAX 2010.1 DWAX 2010.1

6
Using xsl:otherwise  example-XSL.xml
<?xml-stylesheet type="text/xsl" href="example-XSL2.xsl"?>
<course>
<xsl:choose>
<unit>
<xsl:when test="picture =''"> <name>Developing Web Applications with XML</name>
...... <code>300111</code>
</xsl:when> <campus>Campbelltown</campus>
<xsl:otherwise> <location>LT 06</location>
<lecturer>Heidi Stratti</lecturer>
...... <practicals>4</practicals>
</xsl:otherwise> <picture>heidi.jpg</picture>
</xsl:choose> </unit>
<unit>
<name>E-business Technologies and Applications</name>
 Statements contained in xsl:otherwise will <code>300126</code>
<campus>Paramatta</campus>
be applied if test condition is false <location>ECG 22</location>
<lecturer>Robyn Lawson</lecturer>
 It is optional to use xsl:otherwise <practicals>1</practicals>
<picture/>
</unit>
25
</course> 26
DWAX 2010.1 DWAX 2010.1

XSLT final example XSLT final example


 XSL file - example-XSL2.xsl  XSL file - example-XSL2.xsl …contd…
<td>
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:choose>
<xsl:stylesheet version=“1.0” <xsl:when test="picture =''">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:element name="img">
<xsl:template match="/"> <xsl:attribute name="src">
<html> <xsl:value-of
<body> select="document('picture.xml')/pictures/picture"/>
<h2>Course Information</h2> </xsl:attribute>
<table border="1"> </xsl:element>
<tr bgcolor="yellow"> </xsl:when>
<th>Code</th> <xsl:otherwise>
<th>Unit</th> <xsl:element name="img">
<th>Lecturer</th> <xsl:attribute name="src">
<th>Image</th> <xsl:value-of select="picture"/>
</tr> </xsl:attribute>
<xsl:for-each select="course/unit"> </xsl:element>
<tr> </xsl:otherwise>
<td><xsl:value-of select="code"/></td> </xsl:choose>
<td><xsl:value-of select="name"/></td> </td>
<td><xsl:value-of select="lecturer"/></td> </tr>
</xsl:for-each>
27 </table></body></html></xsl:template></xsl:stylesheet> 28
DWAX 2010.1 DWAX 2010.1

7
XSLT final example XSLT final example
 XSL explained – use of xsl elements
 Final example - result choose, when, otherwise

<xsl:choose>
<xsl:when test="picture =''">
<!– Load other file, get picture name; DISPLAY THE PICTURE TAKEN
FROM THE OTHER XML FILE---- >
</xsl:when>
<xsl:otherwise>
<!-- DISPLAY THE PICTURE---- >
</xsl:otherwise>
</xsl:choose>

29 30
DWAX 2010.1 DWAX 2010.1

Some more functions


 count() function:  Variables:
– count(//cd) – User defined name to store a particular value or
object:
• Will return the number of cd elements
• Number
 sum() function • Text string
• Node set
– Sum(//price) • Boolen value
• Will return the total sum of all the price elements • Result tree fragment
 Mathematical operators: – <xsl:variable name=‘name’ select=‘value’/>
+, -, *, div, mod – Value can only be set once. Need to consider lifespan
(ie, inside a template, the variable only ‘lives’ for the
duration of the template)
31
– See p 399 32
DWAX 2010.1 DWAX 2010.1

8
XSLT exercise
XSLT exercise <?xml version="1.0" encoding="ISO-8859-
1"?>
<?xml-stylesheet type="text/xsl"
<book year="">
<title>Brave New World</title>
 Use the given XML source (in the next slide) code and write XSLT file href="NOVELS-1.xsl"?>
<author><name>Aldous</name>
(NOVELS-1.xsl) to display the Authors of books as links with a similar <surname>Huxley</surname></author>
<bestnovels> </book>
output given in figure 1 below. When clicked on these links it should <book year=""> <book year="">
submit a search to google.com to search the web like in figure 2 below <title>Ulysses</title> <title>The Sound and the Fury</title>
<author><name>James</name>
 A few hints are given in the last slide <author><name>William</name>
<surname>Joyce</surname></author> <surname>Faulkner</surname></author>
</book> </book>
<book year=""> <book year="">
<title>The Great Gatsby</title> <title>Catch-22</title>
<author><name>F. Scott</name> <author><name>Joseph</name>
<surname>Fitzgerald</surname></author> <surname>Heller</surname></author>
</book> </book>
<book year=""> <book year="">
<title>A Portrait of the Artist as a <title>Darkness at Noon</title>
Young Man</title>
<author><name>Arthur</name>
<author><name>James</name>
<surname>Koestler</surname></author>
<surname>Joyce</surname></author>
</book>
</book>
<book year="">
<book year="">
<title>Sons and Lovers</title>
<title>Lolita</title>
<author><name>D. H.</name>
<author><name>Vladimir</name>
<surname>Lawrence</surname></author>
<surname>Nabokov</surname></author>
</book>
</book>
Figure 1 Figure 2 33 </bestnovels> 34
DWAX 2010.1 DWAX 2010.1

XSLT exercise - hints


 First you need to display the Title stuff
 You need to add a HTML link tag to the
output
– Remember HTML link tag syntax
• <a href=“link”> Display Data </a>
 You need to make the “link” to submit a
query to google.com
– For example if you want to search the name
James Joyce in google here is how the “link”
should look like
• http://google.com/search?q=James+Joyce
35
DWAX 2010.1

You might also like