0% found this document useful (0 votes)
125 views

Lecture 5-XSL and XPath

- XSL is used to transform XML documents and consists of XSLT and XSL formatting objects. XSLT is used to transform XML from one form to another using XPath to match nodes, while XSL formatting objects provide an alternative to CSS. - An XSLT stylesheet is used to transform an XML document into another format like XML, HTML, PDF or an image. XSLT allows adding or removing elements, rearranging elements, and making calculations. - XPath provides a syntax for locating information in an XML document and is used by technologies like XSLT. It defines standard functions and is a W3C standard language for selecting nodes from an XML source document.

Uploaded by

f_atencia
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Lecture 5-XSL and XPath

- XSL is used to transform XML documents and consists of XSLT and XSL formatting objects. XSLT is used to transform XML from one form to another using XPath to match nodes, while XSL formatting objects provide an alternative to CSS. - An XSLT stylesheet is used to transform an XML document into another format like XML, HTML, PDF or an image. XSLT allows adding or removing elements, rearranging elements, and making calculations. - XPath provides a syntax for locating information in an XML document and is used by technologies like XSLT. It defines standard functions and is a W3C standard language for selecting nodes from an XML source document.

Uploaded by

f_atencia
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lecture 5 Introduction

 Stylesheet for HTML is CSS


 Stylesheet for XML is XSL
XSL: Extensible  Extensible Stylesheet Language (XSL)
Stylesheet Language – Consist of two parts
• XSL Transformation Language (XSLT)
– Transform XML document from one form to another
– Use XPath to match nodes – XPath language for defining
XPath parts of an XML document
• XSL formatting objects
– Alternative to CSS

 XSL is a W3C recommendation


1 2
DWAX 2010.1 DWAX 2010.1

What is XSLT First Stylesheet example


– XSLT document is used to transform an XML  XML file (prologue not included)
document into another document. Ex:
» Another XML doc
» XHTML/HTML
» PDF
» Graphic
– Some XSLT uses:
• Add or remove elements in the resulting document
or output
• Rearrange and sort elements using conditions
• Make calculations

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

First Stylesheet example Transformation explained


 Result (using IE6)
Result
Source
XSLT aa
X
Processor bb
Z
cc
W

Any XML document Usually (but not


necessarily) XML
XSLT Stylesheet
X  aa
Z bb
W  cc

XML conformant with the XSLT spec

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

Creating an XSLT Creating an XSLT cont.


 Define which part of the XML document <?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
we will use xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
– <xsl:template match=“/”> <xsl:template match="/">

• Whole xml document, including processing ...

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

XPath cont. XPath nodes


 In XPath, an XML document is viewed  An Xpath tree has a single root node, which contains all
other nodes in the tree.
conceptually as a tree structure with
 The root node and element nodes contain ordered lists
nodes. of child nodes.
 7 XPath Node types:  All nodes except the root node has a parent node
– Root  Parent nodes can have any number of child
(descendant) nodes
– Element  Attributes and namespaces are not children of their
– Attribute parent node, they describe their parent node
– Text  Relationship between a parent node and its child nodes
is containment
– Comment
– Processing instruction
– Namespace 15 16
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>

 The expressions in Xpath looks similar to <artist>Bonnie Tyler</artist>


<price>9.90</price>
traditional file paths when you use your </cd>
computer, i.e.: <cd country="USA">
<title>Greatest Hits</title>
– C:\XML\Lectures\Lecture Week 03\Xpath.doc
<artist>Dolly Parton</artist>
<price>12.90</price>
</cd>
</catalog>
17 18
DWAX 2010.1 DWAX 2010.1

XPath XPath cont.


 XPath patterns:  If the path starts with a slash ( / ) it represents an
absolute path to an element.
– /catalog  If the path starts with two slashes ( // ) then all
• selects the root element catalog elements in the document that fulfil the criteria
– /catalog/cd/price will be selected, even if they are at different
levels in the XML tree.
• selects all the price elements of all the cd elements
 Wildcards ( * ) can be used to select unknown
of the catalog element XML elements
– Creates an ordered list of price elements
– Will need to use and xsl looping structure to display all
 XPath defines a library of standard functions for
price elements that have been selected working with strings, numbers and Boolean
expressions

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.

of the context node.


29 30
DWAX 2010.1 DWAX 2010.1

XPath: Examples XPath


 child::cd  Child::text()
– Selects all cd elements that are children of the current – Selects the text node children of the current node
node (if the current node has no cd children, it will  Child::node()
select an empty node-set) – Selects all the children of the current node
 attribute::src  descendant::cd
– Selects the src attribute of the current node (if the – Selects all the cd element descendants of the current
current node has no src attribute, it will select an node
empty node-set)  ancestor::cd
 child::* – Selects all cd ancestors of the current node
– Selects all child elements of the current node  /
 attribute::* – Selects the document root
– Selects all attributes of the current node
31 32
DWAX 2010.1 DWAX 2010.1

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

none child:: cd is short for child::cd


child::cd[position()=1] Selects the first cd child of the current node

@ 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

Transformation explained Transformation explained


 Visualizing the Transformation flow
Result
Source
XSLT aa – The process start by traversing the nodes in
X
Z
Processor bb the source tree, trying to find a matching rule
W
cc for each visited node
Any XML document Usually (but not
necessarily) XML
XSLT Stylesheet
– Once the rule is found, the body of the rule is
X  aa instantiated. The XSLT processing
Z bb instructions in the body of the rule use the
W  cc matching node as their context
XML conformant with the XSLT spec

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>

– Template contains rules to apply when a specified


node is matched

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

 XSLT continues next week…

51
DWAX 2010.1

13

You might also like