2. 2
Introduction to XSLT
Agenda
The X* Ecosystem (XSL, XPath, XQuery...)
XML Refresh/Terminology
XPath: Addresses for XML documents
XSLT Basics: template and value-of
XSLT Loops: apply-templates and for-each
XSLT Decisions: if and choose
XSLT Variables: variable and param
XSLT Extras: sort, copy-of, document…
Why/When XSLT?
3. 3
The X* Ecosystem
• XSL(T): Extensible Stylesheet Language
(Transforms)
– Like a giant function with a domain of XML and a
range of XML, HTML and Text
– XSL-FO (Formatting Objects) is a related,
graphics/print-oriented language
• XPath: XML Path (addressing)
– Like a filesystem path in an XML document
– Also like RegEx
– a language for navigating in XML documents
4. 4
XML Refresh/Terminology
XML Nodes
Processing instruction: <?pi ... ?>
Element: <element /> or
<element></element>
Attribute: <element attribute="value" />
Comment: <!-- comment -->
Entity: &
Text node (just plain text)
5. 5
XML Refresh/Terminology
XML Syntax Rules
Escape < > & (turn them into entities <
> &)
Every document has exactly one root element
Attribute values are always quoted
Elements are case-sensitive (and are typically
lowercase)
You don't have to put an xml processing
instruction at the top of your document
6. 6
XPath: Addresses for XML
XPath Expressions
/library
/library/book
/library/book/@name
/library/book[@name="The
Fourth Civilization"]
/library/book[1]
//book[2]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
7. 7
XPath: Addresses for XML
XPath Expressions
/library
/library/book
/library/book/@name
/library/book[@name="The
Fourth Civilization"]
/library/book[1]
//book[2]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
8. 8
XPath: Addresses for XML
XPath Expressions
/library
/library/book
/library/book/@name
/library/book[@name="The
Fourth Civilization"]
/library/book[1]
//book[2]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
9. 9
XPath: Addresses for XML
XPath Expressions
/library
/library/book
/library/book/@name
/library/book
[@name="The Fourth
Civilization"]
/library/book[1]
//book[2]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
10. 10
XPath: Addresses for XML
XPath Expressions
/library
/library/book
/library/book/@name
/library/book[@name="The
Fourth Civilization"]
/library/book[1]
//book[2]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
11. 11
XPath: Addresses for XML
XPath Expressions
/library
/library/book
/library/book/@name
/library/book[@name="The
Fourth Civilization"]
/library/book[1]
//book[2]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
12. 12
XPath: Addresses for XML
XPath Node
selectors
/library/*
/library/book[1]/text()
/library/node()[1]
.
<library>
<!-- comment -->
<book>The Principles of
Computer
Hardware</book>
<book name="The Fourth
Civilization"
/>
</library>
13. 13
XPath: Addresses for XML
XPath Node
selectors
/library/*
/library/book[1]/text()
/library/node()[1]
.
<library>
<!-- comment -->
<book>The Principles of
Computer
Hardware</book>
<book name="The Fourth
Civilization"
/>
</library>
14. 14
XPath: Addresses for XML
XPath Node
selectors
/library/*
/library/book[1]/text()
/library/node()[1]
.
<library>
<!-- comment -->
<book>The Principles of
Computer
Hardware</book>
<book name="The Fourth
Civilization"
/>
</library>
15. 15
XPath: Addresses for XML
XPath Node
selectors
/library/*
/library/book[1]/text()
/library/node()[1]
.
<library>
<!-- comment -->
<book>The Principles of
Computer
Hardware</book>
<book name="The Fourth
Civilization"
/>
</library>
16. 16
XPath: Addresses for XML
XPath Axes
/library/child::book
(or /library/book for short)
/descendent-or-self::book
(or //book for short)
//book[1]/parent::*
(or //book[1]/.. for short)
//book[2]/preceding-
sibling::book
//book[1]/attribute::name
(or //book[1]/@name for
short)
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
17. 17
XPath: Addresses for XML
XPath Axes
/library/child::book
(or /library/book for short)
/descendent-or-
self::book
(or //book for short)
//book[1]/parent::*
(or //book[1]/.. for short)
//book[2]/preceding-
sibling::book
//book[1]/attribute::name
(or //book[1]/@name for
short)
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
18. 18
XPath: Addresses for XML
XPath Axes
/library/child::book
(or /library/book for short)
/descendent-or-self::book
(or //book for short)
//book[1]/parent::*
(or //book[1]/.. for short)
//book[2]/preceding-
sibling::book
//book[1]/attribute::name
(or //book[1]/@name for
short)
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
19. 19
XPath: Addresses for XML
XPath Axes
/library/child::book
(or /library/book for short)
/descendent-or-self::book
(or //book for short)
//book[1]/parent::*
(or //book[1]/.. for short)
//book[2]/preceding-
sibling::book
//book[1]/attribute::name
(or //book[1]/@name for
short)
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
20. 20
XPath: Addresses for XML
XPath Axes
/library/child::book
(or /library/book for short)
/descendent-or-self::book
(or //book for short)
//book[1]/parent::*
(or //book[1]/.. for short)
//book[2]/preceding-
sibling::book
//book[1]/
attribute::name
(or //book[1]/@name for
short)
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
21. 21
XPath: Addresses for XML
XPath Functions
//book[last()]
count(//book)
name(/*)
//book[contains(@name,
"C++")]
//book[not(contains(
@name, "C++"))]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
22. 22
XPath: Addresses for XML
XPath Functions
//book[last()]
count(//book)
name(/*)
//book[contains(@name,
"C++")]
//book[not(contains(
@name, "C++"))]
Returns: 2
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
23. 23
XPath: Addresses for XML
XPath Functions
//book[last()]
count(//book)
name(/*)
//book[contains(@name,
"C++")]
//book[not(contains(
@name, "C++"))]
Returns: "library"
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
24. 24
XPath: Addresses for XML
XPath Functions
//book[last()]
count(//book)
name(/*)
//book[contains(
@name, "C++")]
//book[not(contains(
@name, "C++"))]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
25. 25
XPath: Addresses for XML
XPath Functions
//book[last()]
count(//book)
name(/*)
//book[contains(@name,
"C++")]
//book[not(contains(
@name, "C++"))]
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
26. 26
XPath: Addresses for XML
Other Useful XPath Functions
position() – provides the position in a list (nice for numbering a
sequence of nodes)
sum(xpath) – takes a sequence of nodes and adds up their
numerical values – see also avg(), min(), max()
concat(string, string, …) – exactly what you think
string-length(string) – returns the number of characters
substring(string, start[, length]) – the first char is at 1
translate(source-string, find-string, replace-string) – looks
for individual chars and replaces them
Example: translate("ABCD", "BD", "bd") "AbCd"
27. 27
XPath: Addresses for XML
XPath Operators
//book[last() or
contains(@name,
"C++")]
//book[string-
length(@name) > 5]
//book[1] | //book[2]
Other operators:
and, =, >, <=, >=, !=, +,
-, *, div, mod
Don't forget to escape < >!
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
28. 28
XPath: Addresses for XML
XPath Operators
//book[last() or
contains(@name,
"C++")]
//book[string-
length(@name) > 5]
//book[1] | //book[2]
Other operators:
and, =, >, <=, >=, !=, +,
-, *, div, mod
Don't forget to escape < >!
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
29. 29
XPath: Addresses for XML
XPath Operators
//book[last() or
contains(@name,
"C++")]
//book[string-
length(@name) > 5]
//book[1] | //book[2]
Other operators:
and, =, >, <=, >=, !=, +,
-, *, div, mod
Don't forget to escape < >!
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
30. 30
XPath: Addresses for XML
XPath Operators
//book[last() or
contains(@name,
"C++")]
//book[string-
length(@name) > 5]
//book[1] | //book[2]
Other operators:
and, =, >, <=, >=, !=,
+, -, *, div, mod
Don't forget to escape < >!
<library>
<book name="C++ How to
Program" />
<book name="The Fourth
Civilization"
/>
</library>
31. 31
XSLT Basics
• Try it out
– Make an XML file
– Make an XSLT file
– Run the transform and
view the transformed
data
<xml />
input
<xsl:… />
transform
<xml />
(or text)
output
32. • <xsl:template> Element
– It details what items from the source document it should
handle and uses its content to specify what should be added to
the output when it is executed
– match attribute is used to associate a template with an XML
element and define a template for the entire document. E.g
emptyXSL.xsl
• <xsl:value-of> Element
– used to evaluate an expression and add the result to the
output
– used to extract the value of an XML element and add it to the
output stream of the transformation
• <xsl:for-each> Element
– used to select every XML element of a specified node-set
• <xsl:apply-templates> Element
– responsible for deciding which items in the source document
should be processed; they are then handled by the appropriate
template
33. 33
XSLT Basics
Sample XML
<person>
<name first="Neil"
last="Armstrong" />
<quote>...one giant leap for
mankind.</quote>
</person>
Sample Output
Neil Armstrong said "...one giant
leap for mankind."
Sample XSLT
<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/person">
<xsl:value-of select="concat(
name/@first, ' ', name/@last)"
/>
said "
<xsl:value-of select="quote" />"
</xsl:template>
</xsl:stylesheet>
34. 34
XSLT Basics
Sample XML
<person>
<name first="Neil"
last="Armstrong" />
<quote>...one giant leap for
mankind.</quote>
</person>
Sample Output
Neil Armstrong said "...one giant
leap for mankind."
Sample XSLT
<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/person">
<xsl:value-of select="concat(
name/@first, ' ', name/@last)"
/>
said "
<xsl:value-of select="quote" />"
</xsl:template>
</xsl:stylesheet>
35. 35
XSLT Basics
Sample XML
<person>
<name first="Neil"
last="Armstrong" />
<quote>...one giant leap for
mankind.</quote>
</person>
Sample Output
Neil Armstrong said "...one giant
leap for mankind."
Sample XSLT
<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/person">
<xsl:value-of select="concat(
name/@first, ' ', name/@last)"
/>
said "
<xsl:value-of select="quote" />"
</xsl:template>
</xsl:stylesheet>
51. 51
XSLT Variables: variable
Source XML
<zoo>
<birds>
<albatross pop="4" />
<buzzard pop="2" />
<chickadee pop="12" />
</birds>
<mammals>
<aardvark pop="5" />
<bat pop="200" />
<cheetah pop="2" />
</mammals>
</zoo>
Result HTML
<html><head><title>Zoo</title></head><body>
<ul>
<li>albatross (22% of all birds)</li>
<li>buzzard (11% of all birds)</li>
<li>chickadee (67% of all birds)</li>
</ul>
<ul>
<li>aardvark (2% of all mammals)</li>
<li>bat (97% of all mammals)</li>
<li>cheetah (1% of all mammals)</li>
</ul>
</body></html>
59. 59
XSLT Extras: more elements
• xsl:text – writes literal text to the output
(useful for controlling whitespace or forcing
exact, unescaped output)
• xsl:processing-instruction – writes a PI like
<?php… ?> or <?xml-stylesheet… ?> to the
output
• xsl:import and xsl:include – used to combine
stylesheets (useful for XSLT libraries)
60. 60
XSLT Extras: more functions
• document(url) – opens an XML document at the
given location, returning its nodes as data that
can be used by the template
Example:
document('zoo.xml')/zoo//*[@pop = 2]
• current() – similar to "." except that it always
refers to the current node, even when used inside
predicates
Example:
//*[@pop = current()/@pop]
61. 61
XSLT Extras: xml-stylesheet
To make an XML document show itself in a
transformed format, add this to the top:
<?xml-stylesheet type="text/xsl"
href="path/to/transform.xslt" ?>
This is particularly useful for XML like RSS
62. 62
Why/When XSLT?
Web Standard
XSLT v 1.0 Recommended by W3C almost 10
years ago (it's stable and well understood)
Dozens of implementations and host
languages (Java, .NET, PHP, C++…)
Wide tool support (Firefox, IE, Visual Studio,
many text editors, full IDEs…)
No vendor lock-in
63. 63
Why/When XSLT?
Very good at converting XML to XML,
XHTML/HTML, or plain text
Makes it easy to keep things well-formed
Uses XML syntax, so the only real new syntax
is XPath (which is also used elsewhere)
XPath is far more compact than similar DOM
code in JS/Java/C#/etc.
Can be interpreted (for quick development)
or compiled (for maximum performance)