Title: XSL
1XSL XML Stylesheet Language
2XSL
- XSL XML Stylesheet Language
- XSL cosists of
- XPath (navigation in documents)
- XSLT (T for transformations)
- XSLFO (FO for formatting objects)
- This is a rather complex language for typesetting
(i.e., preparing text for printing) - It will not be taught
3XPath
- A Language for Locating Nodes
- in XML Documents
4XPath
- XPath expressions are written in a syntax that
resembles paths in file systems - The list of nodes located by an XPath expression
is called a Nodelist - XPath is used in XSL and in XQuery (a query
language for XML) - W3Schools has an XPath tutorial
- XPath includes
- Axis navigation
- Conditions
- Functions
5lt?xml version"1.0" encoding"ISO-8859-1"?gt ltcatal
oggt ltcd country"UK"gt lttitlegtDark
Side of the Moonlt/titlegt ltartistgtPink
Floydlt/artistgt ltpricegt10.90lt/pricegt
lt/cdgt ltcd country"UK"gt
lttitlegtSpace Odditylt/titlegt
ltartistgtDavid Bowielt/artistgt
ltpricegt9.90lt/pricegt lt/cdgt ltcd
country"USA"gt lttitlegtAretha Lady
Soullt/titlegt ltartistgtAretha
Franklinlt/artistgt ltpricegt9.90lt/pricegt
lt/cdgt lt/cataloggt
An XML document
6catalog.xml
The XML document as a DOM tree
catalog
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
7The Main Idea in the Syntax of XPath Expressoins
- / at the beginning of an XPath expression
represents the root of the document - / between element names represents a
parent-child relationship - // represents an ancestor-descendent
relationship - _at_ marks an attribute
- condition specifies a condition
8catalog.xml
catalog
/catalog
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
Getting the root element of the document
9catalog.xml
catalog
/catalog/cd
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
Finding child nodes
10catalog.xml
catalog
/catalog/cd/price
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
Finding descendent nodes
11catalog.xml
catalog
/catalog/cdpricelt10
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
Condition on elements
12catalog.xml
/catalog//title
catalog
//title
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
// represents any directed path in the document
13catalog.xml
catalog
/catalog/cd/
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
represents any element name in the document
14//
catalog.xml
What will the following expressions return?
//
catalog
//price9.90
//price9.90/
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
represents any element name in the document
15catalog.xml
/catalog/cd1
catalog
/catalog/cdlast()
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
Position based condition
16catalog.xml
/catalog/cd_at_countryUK
catalog
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
_at_ marks attributes
17catalog.xml
catalog
/catalog/cd/_at_country
cd
cd
country
cd
country
country
USA
UK
UK
title
artist
price
title
artist
price
title
artist
price
Space Oddity
Aretha Lady Soul
Dark Side of the Moon
David Bowie
Aretha Franklin
Pink Floyd
9.90
9.90
10.90
_at_ marks attributes
18Relative Navigation Using Axes
- Starts with the current node and not with the
root (/) - A . marks the current node (e.g., ./title)
- A .. marks the parent node (e.g., title/../)
- There are also other axes, e.g., child,
descendent, ancestor, parent, following-sibling,
etc.
19Functions
- Many functions that are included in XPath
- Some examples
- count() returns the number of nodes in a
nodelist - last() returns the last node in a nodelist
- name() returns the name of a node
- position() returns the position of the node in
the nodelist
20Additional Examples ofXPath Expressions
- These examples use element names that are not
necessarily from the XML document that was shown
previously
21Examples of XPath Expressions
- para
- Selects the para children elements of the context
node -
- Selects all element children of the context node
- text()
- Selects all text node children of the context
node - _at_name
- Selects the name attribute of the context node
22More Examples ofXPath Expressions
- _at_
- Selects all the attributes of the context node
- para1
- Selects the first para child of the context node
- paralast()
- Selects the last para child of the context node
- /para
- Selects all para grandchilren of the context node
23More Examples ofXPath Expressions
- /doc/chapter5/section2
- Selects the second section of the fifth chapter
of the doc - chapter//para
- Selects the para element descendants of the
chapter element children of the context node - //para
- Selects all the para descendants of the document
root and thus selects all para elements in the
same document as the context node
24More Examples ofXPath Expressions
- //olist/item
- Selects all the item elements that have an olist
parent and are in the same document as the
context node - .
- Selects the context node
- .//para
- Selects the para descendants of the context node
- ..
- Selects the parent of the context node
25More Examples ofXPath Expressions
- ../_at_lang
- Selects the lang attribute of the parent of the
context node - para_at_typewarning
- Selects the para children of the context node
that have a type attribute with value warning - chaptertitle
- Selects the chapter children of the context node
that have one or more title children
26More Examples ofXPath Expressions
- para_at_typewarning5
- Selects the fifth para child among the children
of the context node that have a type attribute
with value warning - para5_at_typewarning
- Selects the fifth para child of the context node
if that child has a type attribute with value
warning
27More Examples ofXPath Expressions
- chaptertitleIntroduction
- Selects the chapter children of the context node
that have one or more title children with
string-value equal to Introduction - employee_at_secretary and _at_assistant
- Selects employee children of the context node
that have both a secretary attribute and an
assistant attribute
28More Examples of Xpath Expressions
- /university/department/course
- This Xpath expression matches any path that
starts at the root, which is a university
element, passes through a department element and
ends in a course element - ./department/course_at_year2002
- This Xpath expression matches any path that
starts at the current element, continues to a
child which is a department element and ends at
a course element with a year attribute that is
equal to 2002
29Location Paths
- The previous examples are abbreviations of
location paths - See XPath tutorial in W3Schools or Costellos
slides on long names - For example, // is short for /descendant-or-self
node()/. - //para is short for
/descendant-or-selfnode()/childpara
30XSLT
- Transforming XML documents into other XML
documents
31XSLT Stylesheet
- An XSLT stylesheet is a program that transforms
an XML document into another XML document - For example
- Transforming XML to XHTML (HTML that conforms to
XML syntax) - Transforming an XML document to WML (a format of
XML that cellular phones can display)
32A Few Things About XSL
- XSL is a high-level, functional language
- The syntax is a bit peculiar and possibly
confusing - An XSL style sheet is a valid XML document
- Valid with respect to the XSL namespace
- Therefore, commands in XSL are XSL elements
33Applying XSLT Stylesheets toXML Documents
- There are three ways of applying an XSLT
stylesheet to an XML document - Directly applying an XSLT processor to the XML
document and the XSLT stylesheet - Calling an XSLT processor from within a (Java)
program - Adding to the XML document a link to the XSL
stylesheet and letting the browser do the
transformation
34Using an XSL Processor
XSL stylesheet
XML document
Result is either an XML, HTML or text document
XSL Processor
35Letting a Browser Perform the Transformation
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt?xml-
stylesheet type"text/xsl" hrefcatalog.xsl"?gt
ltcataloggt ltcd country"UK"gt
lttitlegtDark Side of the Moonlt/titlegt
ltartistgtPink Floydlt/artistgt
ltpricegt10.90lt/pricegt lt/cdgt lt/cataloggt
36The Root of the XSL Document
- The Root of the XSL document should be one of the
following lines
ltxslstylesheet version"1.0" xmlnsxsl"http//w
ww.w3.org/1999/XSL/Transform"gt
ltxsltransform version"1.0" xmlnsxsl"http//ww
w.w3.org/1999/XSL/Transform"gt
The namespace allows the XSL processor to
distinguish between XSL tags and tags of the
result document
37How Does XSLT Work?
- An XSL stylesheet is a collection of templates
that are applied to source nodes (i.e., nodes of
the given XML document) - Each template has a match attribute that
specifies to which source nodes the template can
be applied - The current source node is processed by applying
a template that matches this node - Processing always starts at the root (/)
38Templates
- A template has the form
- ltxsltemplate match"pattern"gt
- ...
- lt/xsltemplategt
- The content of a template consists of
- XML elements and text that are copied to the
result - XSL elements that are actually instructions
- The pattern syntax is a subset of XPath
39lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth1gtHello
Worldlt/h1gt lt/bodygt lt/htmlgt lt/xsltemplategt lt/xs
lstylesheetgt
40lthtmlgt ltbodygt lth1gtHello Worldlt/h1gt lt/bodygt lt/htmlgt
Applying a browser to catalog.xml (catalog.xml
has a link to catalog.xsl)
41The Elementltxslapply-templatesgt
- Processing starts by applying a template that
matches the root (/) - If the given XSL stylesheet does not have a
template that matches the root, then one is
inserted by default (see the slide on Default
Templates) - The XSL stylesheet must specify explicitly
whether templates should be applied to
descendants of the root - It is done by putting inside a template the
instruction - ltxslapply-templates select"xpath"/gt
- Without the select attribute, this instruction
processes all the children of the current node
42lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt ltxslapply-templates
select"catalog/cd"/gt lt/bodygt
lt/htmlgt lt/xsltemplategt ltxsltemplate
match"cd"gt lth2gtA CD!lt/h2gt lt/xsltemplategt lt/
xslstylesheetgt
lthtmlgt ltbodygt lth2gtA CD!lt/h2gt lth2gtA CD!lt/h2gt
lth2gtA CD!lt/h2gt lt/bodygt lt/htmlgt
43Default Templates
- XSL provides implicit built-in templates that
match every element and text nodes - Templates we write always override these built-in
templates (when they match)
ltxsltemplate match/ gt ltxslapply-templates
/gt lt/xsltemplategt ltxsltemplate
matchtext()gt ltxslvalue-of select./gt lt/xsl
templategt
44lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
matchcdtitleSpace Oddity"gt lthtmlgt
ltbodygt lth1gtHello Worldlt/h1gt lt/bodygt
lt/htmlgt lt/xsltemplategt lt/xslstylesheetgt
What is generated when this xsl is applied to
catalog.xml?
45 Dark Side of the Moon Pink Floyd
10.90 lthtmlgtltbodygtlth1gtHello
Worldlt/h1gtlt/bodygtlt/htmlgt Aretha Lady
Soul Aretha Franklin 9.90
46The Most Frequently Used Elements of XSL
- ltxslvalue-of selectxpath-expression/gt
- This element extracts the value of a node from
the nodelist located by xpath-expression - ltxslfor-each selectxpath-expression/gt
- This element loops over all the nodes in the
nodelist located by xpath-expression - ltxslif testxpath-expression/gt,
ltxslif testxpath-expressionvalue/gt, etc. - This element is for conditional processing
47The ltxslvalue-ofgt Element
ltxslvalue-of selectxpath-expression/gt
- The XSL element ltxslvalue-ofgt can be used to
extract the value of an element that is selected
from the source XML document - The extracted value is added to the output stream
- The selected element is located by an XPath
expression that appears as the value of the
select attribute
48Selected values
49lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth2gtA CD
Cataloglt/h2gt lttable border"1"gt lttr
bgcoloryellow"gt ltthgtTitlelt/thgt
ltthgtArtistlt/thgt lt/trgt
50lttrgt lttdgtltxslvalue-of select"catalog/cd
/title"/gt lt/tdgt lttdgtltxslvalue-of
select"catalog/cd/artist"/gt lt/tdgt
lt/trgt lt/tablegt lt/bodygt lt/htmlgt lt/xsltemplate
gt lt/xslstylesheetgt
Note that only the first matched element is
retrieved for each ltxslvalue ofgt
51The ltxslfor-eachgt Element
ltxslfor-each selectxpath-expression/gt
- The ltxslfor-eachgt element loops over all the
nodes in the nodelist of the XPath expression
that appears as the value of the select attribute - The value of each node can be extracted by an
ltxslvalue-ofgt element
52All the values are selected
53lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth2gtA CD
Cataloglt/h2gt lttable border"1"gt lttr
bgcoloryellow"gt ltthgtTitlelt/thgt
ltthgtArtistlt/thgt lt/trgt
As in the previous example
54 ltxslfor-each select"catalog/cd"gt lttrgt
lttdgtltxslvalue-of select"title"/gt
lt/tdgt lttdgtltxslvalue-of
select"artist"/gt lt/tdgt lt/trgt
lt/xslfor-eachgt lt/tablegt lt/bodygt
lt/htmlgt lt/xsltemplategt lt/xslstylesheetgt
Note that all the /catalog/cd elements are
retrieved
55Consider the following change in the select
attribute
ltxslfor-each select"catalog/cdpricelt10
"gt lttrgt lttdgtltxslvalue-of
select"title"/gt lt/tdgt
lttdgtltxslvalue-of select"artist"/gt lt/tdgt
lt/trgt lt/xslfor-eachgt lt/tablegt
lt/bodygt lt/htmlgt lt/xsltemplategt lt/xslstylesheet
gt
Only elements that satisfy /catalog/cdpricelt10
are retrieved
56(No Transcript)
57The ltxslsortgt Element
- The ltxslsortgt element is used to sort the list
of nodes that are looped over by the
ltxslfor-eachgt element - Thus, the ltxslsortgt must appear inside the
ltxslfor-eachgt element - The looping is done in sorted order
58Sorted by the name of the artist
59 ltxslfor-each select"catalog/cd"gt ltxslsort
select"artist"/gt lttrgt
lttdgtltxslvalue-of select"title"/gt lt/tdgt
lttdgtltxslvalue-of select"artist"/gt lt/tdgt
lt/trgt lt/xslfor-eachgt lt/tablegt
lt/bodygt lt/htmlgt lt/xsltemplategt lt/xslstylesheet
gt
The /catalog/cd elements are sorted according to
the value of the artist element
60The ltxslifgt Element
- The ltxslifgt element is used for conditional
processing - The condition appears as the value of the test
attribute, for example - ltxslif test"price gt 10"gt some output
...lt/xslifgt - The elements inside the ltxslifgt element are
processed if the condition is true
61Note
- Processing the inside elements means
- Copying them into the output stream if they are
not XSL elements, and - Evaluating them if they are XSL elements
- If the value of the test attribute is just an
XPath expression (i.e., without any comparison),
then the test is satisfied if the nodelist of
this XPath expression is not empty
62lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth2gtA CD
Cataloglt/h2gt lttable border"1"gt lttr
bgcoloryellow"gt ltthgtTitlelt/thgt
ltthgtArtistlt/thgt lt/trgt
As in the previous examples
63 ltxslfor-each select"catalog/cd"gt ltxslif
test"price gt 10"gt lttrgt
lttdgtltxslvalue-of select"title"/gt lt/tdgt
lttdgtltxslvalue-of select"artist"/gt lt/tdgt
lt/trgt lt/xslifgt lt/xslfor-eachgt
lt/tablegt lt/bodygt lt/htmlgt lt/xsltemplategt lt/xs
lstylesheetgt
Only /catalog/cd with pricegt10 are retrieved
64(No Transcript)
65Question
- In the previous example, what should be the
condition if we want to extract the information
for all cds that have a price (i.e., the
information about the price is not missing)?
66The ltxslchoosegt Element
- The ltxslchoosegt element is used in conjunction
with ltxslwhengt and ltxslotherwisegt to express
test with multiple conditions - There can be many ltxslwhengt inside an
ltxslchoosegt element, but there should be a
single ltxslotherwisegt inside an ltxslchoosegt
element
67Using ltxslchoosegt
- To insert a conditional choose against the
content of the XML file, simply add the
ltxslchoosegt, ltxslwhengt, and ltxslotherwisegt
elements to your XSL document like this - ltxslchoosegt ltxslwhen test"price gt
10"gt ... some code ... lt/xslwhengt
ltxslotherwisegt ... some code ....
lt/xslotherwisegtlt/xslchoosegt
68ltxslfor-each select"catalog/cd"gtlttrgt
lttdgtltxslvalue-of select"title"/gtlt/tdgt
ltxslchoosegt ltxslwhen test"price gt 10"gt
lttd bgcolor"red"gt ltxslvalue-of
select"artist"/gtlt/tdgt lt/xslwhengt
ltxslwhen test"pricegt9 and pricelt10"gt
lttd bgcolor"gray"gt
ltxslvalue-of select"artist"/gtlt/tdgt
lt/xslwhengt ltxslotherwisegt
lttdgtltxslvalue-of select"artist"/gtlt/tdgt
lt/xslotherwisegt lt/xslchoosegtlt/trgt lt/xslfor-ea
chgt
69(No Transcript)
70Applying Templates Recursively
- The following example shows how to apply
templates recursively - Generally, it is possible (but not in this
example) that more than one template matches the
current source node - The specification (www.w3.org/TR/xslt) describes
(Section 5.5) which template should be chosen for
application
71lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth2gtA CD
Cataloglt/h2gt ltxslapply-templates/gt
lt/bodygt lt/htmlgt lt/xsltemplategt
72 ltxsltemplate match"cd"gt ltpgt
ltxslapply-templates select"title"/gt
ltxslapply-templates select"artist"/gt
lt/pgt lt/xsltemplategt ltxsltemplate
match"title"gt Title ltspan style"colorred"gt
ltxslvalue-of select"."/gtlt/spangt ltbr
/gt lt/xsltemplategt
73ltxsltemplate match"artist"gt Artist ltspan
style"colorgreen"gt ltxslvalue-of
select"."/gtlt/spangt ltbr /gt lt/xsltemplategt lt/xs
lstylesheetgt
74(No Transcript)
75Is Recursive Application of Templates Really
Needed?
- The output of the previous example can also be
generated by an XSL stylesheet that uses only one
template that matches the root (and does not use
the element ltxslapply-templatesgt) - However, some tasks can only be done by applying
templates recursively - This typically happens when the structure of the
source XML document is not known
76For example
- Suppose that we want to write an XSL stylesheet
that generates an exact copy of the source XML
document - It is rather easy to do it when the structure of
the source XML document is known - Can we write an XSL stylesheet that does it for
every possible XML document? - Yes! (see next slide)
77lt?xml version"1.0"?gt ltxslstylesheet
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
version"1.0"gt ltxsloutput
method"xml"/gt ltxsltemplate match""gt
ltxslelement name"name(.)"gt
ltxslfor-each select"_at_"gt
ltxslattribute name"name(.)"gt
ltxslvalue-of select"."/gt
lt/xslattributegt lt/xslfor-eachgt
ltxslapply-templates/gt
lt/xslelementgt lt/xsltemplategt lt/xslstyleshe
etgt
Identity Transformation Stylesheet
78The ltxsloutputgt Element
lt?xml version"1.0" encoding"ISO-8859-1"?gt
ltxslstylesheet version"1.0" xmlnsxsl"http//
www.w3.org/1999/XSL/ Transform"gt ltxsloutput
method"xml" version"1.0" encoding"iso-8859-1"
indent"yes"/gt ...... lt/xslstylesheetgt
Tells in what format the output should be
xml/html/text
79Some Other XSL Elements
- The ltxsltextgt element allows to insert free text
in the output - The ltxslcopy-ofgt element creates a copy of the
current node - The ltxslcommentgt element is used to create a
comment node in the result tree - There are more elements and functions look in
the specification! (www.w3.org/TR/xslt)
80Costellos Tutorial(See DBI Lecture Schedule for
a reference)
- Costello has an excellent and detailed tutorial
- First part (xsl01.ppt) has many examples that do
not use recursion - Second part (xsl02.ppt) explains how to use
recursion and why it is needed (the identity
transformation shown earlier is from Costellos
tutorial)
81W3Schools Tutorial on XSLT
- The W3Schools XSLT Tutorial has (among other
things) tables that list all the elements and
functions of XSLT - It also has some details about implementations
- Some browsers may not implement all features or
may implement some features differently from the
specifications
82How to Use the Examples
- Since XSL is a high-level language, XSLT
stylesheets are relatively short and easy to
understand - It is easy to modify an existing style- sheet
into another stylesheet with a similar
functionality - The many examples of Costello and W3Schools are
very useful as a starting point for writing your
own stylesheets
83Summary
- XSLT is a high-level transformation language
- Create core output once in XML format (using
Servlets, JSP, etc.) - Use XSLT to transform the core output as needed
84For Example
- Suppose that you have produced an XML catalog of
products - Now you decide to lower all prices by 10
- It is easy to do it in XSLT
- Modify the identity transformation to do that