Lecture 5-XSL and XPath
Lecture 5-XSL and XPath
3 4
DWAX 2010.1 DWAX 2010.1
1
First Stylesheet example First Stylesheet example
XSL file
Linking the XSL to the XML
5 6
DWAX 2010.1 DWAX 2010.1
7 8
DWAX 2010.1 DWAX 2010.1
2
Transformation explained Stylesheet Declaration
The root element
XSLT – <xsl:stylesheet> or <xsl:transform>
Processor
– How to use it Version is important
MSXML – IE6 <?xml version = "1.0"?>
Source: <xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
Any XML document Result:
<!– rest of the code goes here -->
Usually (but not XSLT namespace
necessarily) XML </xsl:stylesheet>
This is essential to
OR distinct XSLT
elements from non-
<?xml version = "1.0"?>
XSLT elements
<xsl:transform version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
XSLT:
<!– rest of the code goes here -->
XML conformant with the XSLT spec
</xsl:transform>
9 10
DWAX 2010.1 DWAX 2010.1
instructions </xsl:template>
</xsl:stylesheet>
– XPath will allow us to access specific pieces
of information within an xml document
11 12
DWAX 2010.1 DWAX 2010.1
3
Creating an XSLT cont. XML Path Language (XPath)
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" Xpath provides a syntax for locating
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
information in XML document
<xsl:template match="/">
<html> Not a structural language like XML
<body>
<h2>Unit Number:</h2>
A string based language of expressions
<xsl:value-of select="course/unit/code"/> used by other XML technologies such as
</body>
XSLT and Xpointer
</html>
</xsl:template> Defines a library of standard functions
</xsl:stylesheet>
A W3C standard
Adding in HTML tags
Selecting nodes from the source xml document 13 14
DWAX 2010.1 DWAX 2010.1
4
XPath cont. Example from W3schools: Catalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
Xpath uses a pattern expression to identify <catalog>
nodes in an XML document. <cd country="USA">
<title>Empire Burlesque</title>
An Xpath pattern is a slash-separated list of <artist>Bob Dylan</artist>
child element names that describe a path <price>10.90</price>
</cd>
through the XML document. The pattern selects <cd country="UK">
elements that match the path. <title>Hide your heart</title>
19 20
DWAX 2010.1 DWAX 2010.1
5
XPath cont XPath
/catalog/cd[price>12.00] Selecting Branches
– selects all the cd elements that have a price /catalog/cd[1]
element with a value larger than 10.80 – selects the first cd child element of the catalog
//cd element
– selects all the cd elements in the document /catalog/cd[last()]
/catalog/cd/* – selects the last cd child element of the catalog
– selects all the child elements of all the cd element
elements of the catalog element
21 22
DWAX 2010.1 DWAX 2010.1
XPath XPath
Selecting Attributes: Location Paths
Attributes are specified by the @ prefix A location path is the expression that specifies how to
navigate an XPath tree from one node to another.
//@country A location path can be absolute or relative:
– selects all attributes named country An absolute location path starts with a slash ( / ), a
//cd[@country] relative location path starts without the slash, i.e.:
– selects all cd elements which have an attribute
named country /step/step/…
//cd[@*] /catalog/cd/title (absolute path)
– selects all cd elements which have any attributes
//cd[@country=’UK’] step/…
– selects all cd elements which have an attribute cd/title (relative path)
named country with a value of ‘UK’
23 24
DWAX 2010.1 DWAX 2010.1
6
XPath XPath
The location path consists of one or more A location path is composed of location steps.
Location steps consists of:
steps, each separated by a slash. an axis (specifies the tree relationship between the
Absolute path: the current node-set nodes selected by the location step and the current
node)
consists of the root node. a node test (specifies the node type and the expanded-
Relative path: the current node-set name of the nodes selected by the location step). The
expanded name is the name of the element or attribute
consists of the node where the expression including the namespace prefix if any.
is being used zero or more predicates (use expressions to further
refine the set of nodes selected by the location step)
25 26
DWAX 2010.1 DWAX 2010.1
XPath XPath
Syntax for a location step: Axes
Axisname::nodetest[predicate] An Axis selects a set of nodes from the
Child::price[price=9.90] document tree. It indicates which nodes,
To locate a specific node in an XML relative to the context node, should be
document, we put together multiple included in the search. The axis also
location steps, each of which refines the dictates the ordering of the nodes in the
search. set. (forward/backward axes)
27 28
DWAX 2010.1 DWAX 2010.1
7
XPath XPath
Axis Name Ordering Description Node tests
Self None The context node A node test is used to identify a
itself node within an axis. Node tests can
Parent Reverse The context node’s be performed by name or by type
parent, if one exists.
Node test Description
Child Forward The context node’s * Selects all nodes of the same principal node type.
children, if they node() Selects all nodes, regardless of their type.
exist. text() Selects all text nodes.
Attribute Forward The attribute nodes node name Selects all nodes with the specified node name.
8
XPath XPath
Predicates
Some abbreviations that can be used for A predicate filters a node-set into a new node-set. A
location path: predicate is placed inside square brackets ( [ ] ).
Examples
Example Result
child::price[price=9.90] Selects all price elements that are children of the current node with a
Abbr Meaning Example price element that equals 9.90
@ attribute:: cd[@type="classic"] is short for child::cd[position()=last()] Selects the last cd child of the current node
child::cd[attribute::type="classic"]
child::cd[position()=last()-1] Selects the last but one cd child of the current node
. self::node() .//cd is short for
self::node()/descendant-or-self::node()/child::cd child::cd[position()<6] Selects the first five cd children of the current node
.. parent::node() ../cd is short for
parent::node()/child::cd /descendant::cd[position()=7] Selects the seventh cd element in the document
// /descendant-or- //cd is short for
self::node()/ /descendant-or-self::node()/child::cd child::cd[attribute::type="classic"] Selects all cd children of the current node that have a type attribute
with value classic
33 34
DWAX 2010.1 DWAX 2010.1
35 36
DWAX 2010.1 DWAX 2010.1
9
Basic XSLT elements and attributes Basic XSLT elements and attributes
<xsl:template> example <xsl:template> example - result
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Course Information</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
37 38
DWAX 2010.1 DWAX 2010.1
Basic XSLT elements and attributes Basic XSLT elements and attributes
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Course Information</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="unit">
<p>
<xsl:apply-templates select="name"/> <br/>
<xsl:apply-templates select="code"/> <br/>
<xsl:apply-templates select="lecturer"/>
</p>
</xsl:template>
</xsl:stylesheet> 39 40
DWAX 2010.1 DWAX 2010.1
10
Basic XSLT elements and attributes Basic XSLT elements and attributes
<xsl:template> example expanded more -
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
result
<xsl:template match="/"><html><body> <h2>Course Information</h2> <xsl:apply-templates/>
</body></html></xsl:template>
<xsl:template match="unit"> <p> <xsl:apply-templates select="name"/> <xsl:apply-templates select="code"/>
<xsl:apply-templates select="lecturer"/> </p></xsl:template>
<xsl:template match="name">
Name of the Unit: <span style="color:red">
<xsl:value-of select="."/></span> <br />
</xsl:template>
<xsl:template match="code">
Code: <span style="color:green">
<xsl:value-of select="."/></span> <br />
</xsl:template>
<xsl:template match="lecturer">
Lecturer: <span style="color:blue">
<xsl:value-of select="."/></span> <br />
</xsl:template> 41 42
</xsl:stylesheet> DWAX 2010.1 DWAX 2010.1
Basic XSLT elements and attributes Basic XSLT elements and attributes
<xsl:template> attributes <xsl:apply-templates> example
(http://w3chools.com ) <?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
Attribute Value Description
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
name name Optional. Specifies a name for the template. <xsl:template match="course/unit">
Note: If this attribute is omitted there must be a match attribute
<html>
<body>
<h2>Course Information</h2>
match pattern Optional. The match pattern for the template.
<xsl:apply-templates select="name" /> <hr/>
Note: If this attribute is omitted there must be a name attribute
</body>
</html>
</xsl:template>
mode mode Optional. Specifies a mode for this template
</xsl:stylesheet>
priority number Optional. A number which indicates the numeric priority of the
template
43
– applies a template to the current element or to 44
DWAX 2010.1 the current element's child nodes
DWAX 2010.1
11
Basic XSLT elements and attributes Basic XSLT elements and attributes
<xsl:apply-templates> example result <xsl:apply-templates> another example
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Course Information</h2>
<xsl:apply-templates select="course//name" /> <hr/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
45 46
DWAX 2010.1 DWAX 2010.1
Basic XSLT elements and attributes Basic XSLT elements and attributes
<xsl:apply-templates> another example - <xsl: apply-templates > attributes
(http://w3chools.com )
result
Attribute Value Description
select expression Optional. Specifies the nodes to be processed. An asterisk selects the entire node-
set. If this attribute is omitted, all child nodes of the current node will be selected
mode name Optional. If there are multiple ways of processing defined for the same element,
distinguishes among them
47 48
DWAX 2010.1 DWAX 2010.1
12
Basic XSLT elements and attributes Basic XSLT elements and attributes
<xsl:value-of> example <xsl: value-of > example - result
<?xml version="1.0" encoding="ISO-8859-1"?>
• Used to select the
<xsl:stylesheet version="1.0" value of an XML
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> element and add it
<xsl:template match="/"> to the output
<html> <body> stream of the
<h2>Course Information </h2> transformation
<table border="1">
<tr bgcolor="yellow"> <th>Unit Name</th>
<th>Lecturer</th>
</tr>
<tr>
<td><xsl:value-of select="course/unit/name"/></td>
<td><xsl:value-of select="course/unit/lecturer"/></td>
</tr>
</table>
</body> </html>
</xsl:template></xsl:stylesheet> 49 50
DWAX 2010.1 DWAX 2010.1
51
DWAX 2010.1
13