Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What is XSLT
A language for transforming XML:
From a structural vocabulary like DocBook to a presentation vocabulary like HTML Between similarly vocabularies Even within the same vocabulary
Batch Formatting
Transform XML into vocabulary suitable for display Most common targets:
HTML (for legacy display) XSLT Formatting Objects (one day)
Inter-vocabulary Translation
Any input vocabulary is legal Any output vocabulary is legal Even:
Proprietary Non-visual
"Massaging" Data
Querying for particular information Sorting on elements and attributes Filtering elements and attributes Output could be new vocabulary or old one
Reusing Data
For example, in the online rendering of an XML document representing an encyclopedia, the article title might be:
in the page that represents the article itself in the header in the footer in the Table of Contents in a "listing by author" in a "listing by subject" in cross-references
Example: Input
<repairs> <repair> <date>...</date> <location>...</location> <reason>...</reason> </repair> <repair> <date>...</date> <location>...</location> <reason>...<reason> </repair> </repairs>
Example: Output
<table> <headings> <head>Location</head><head>Date</head><head>Reas on</head> </headings> <row><cell>...</cell><cell>...</cell><cell>...</cell></row> <row><cell>...</cell><cell>...</cell><cell>...</cell></row> </table>
10
Transformational Features
Headings row added Location and date switched around Elements renamed
Repairs --> table Date --> cell Location --> cell Reason --> cell
11
Transformation Terminology
XSLT Stylesheet = collection of "template rules" Template Rule = pattern + template Every input element is processed with a single rule The "right rule" is found based on pattern The rule applies the template
12
Formatting Terminology
Templates create result elements Result elements can conform to any vocabulary
13
14
15
16
XSLT Versions
The current version is 1.0. 2.0 is being designed. It is somewhat more complex and sophisticated. It builds on XPath 2.0, which is strongly type-checked. It supports all XML Schema types and can do appropriate conversions between them.
17
What is XPath?
Data must be found before it can be worked with Document components can be located by
name (components with URIs or IDs) position (in document hierarchy or character stream) structural relationship (to some other located component)
XPath is a simple query/addressing language for XML XPath expressions are applied to XML documents to return node sets. Some node sets contain a single element but others contain multiple nodes of arbitrary types (text, attribute, etc.)
19
A Basic XPath
Return the root (document) element if it is of type DOC /DOC It is just like a DOS or Unix path referencing a file in the root directory
20
Path-like XPaths
Return the TITLE child (assuming there is only one) of the DOC document element /DOC/TITLE
21
Query-like XPaths
Return all SECTION children of the DOC document element /DOC/SECTION
22
Multi-level XPaths
Return all TITLE children of all SECTION children of the DOC document element /DOC/SECTION/TITLE
23
Wildcards in XPaths
Return all TITLE children of all children of the DOC document element /DOC/*/TITLE In other words, all TITLE grandchildren The wildcard matches elements of any element type It matches only one level
24
25
26
Matching Parents
.. represents the parent node Return elements directly containing FOOTNOTEs //FOOTNOTE/.. Return footnoes parent elements' TITLE children //FOOTNOTE/../TITLE
27
Matching Attributes
@foo represents an attribute named "foo" Security attributes of SECTION: //SECTION/@security All security attributes in document //*/@security
28
Evaluation Context
Every XPath is evaluated in a context:
For an XPointer used in a link, the context is the root of the document into which it points In XSLT, the context is "the node you are working on" (the current node)
29
Relative XPaths
The XPath ./P (or just P) finds all elements of type P that are children of the context node The XPath ./* (or just *) finds all elements of all types that are children of the context node
30
Understanding Context
<DOC> <SECTION ID="Top-level"> <SECTION ID="Contained.1"/> <SECTION ID="Contained.2"/> </SECTION> </DOC> In the context DOC, XPath: SECTION (or ./SECTION) Returns a single node The other SECTIONs are not children
31
Predicates
Filter result nodesets Predicates are specified with square brackets The most common types of predicates are:
attribute existence subelement existence position
32
Attribute Predicates
Sections with a security value of "TOPSECRET"
//SECTION[@security="TOPSECRET"]
33
Subelement Predicates
Sections containing an image
//SECTION[IMG]
34
Positional Predicates
A numeric predicate tests that a node corresponds to a position Selects third SECTION
//SECTION[3]
35
Predicate Expressions
To refine an address, in a predicate you can use
other location path expressions strings numbers operators, including or and and to express alternates or unions functions (XPath defines a core set)
36
Core Functions
XPath's core function library includes
node set functions, e.g.: id(), last(), position(), count() string functions, e.g: contains(), startswith(), normalize() boolean functions, e.g.: true(), false(), lang() number functions, e.g.: number(), sum(), round()
37
38
Boolean Operators/Functions
or: e.g.
section[@title or @name] (one or other)
and:
section[@title and @name]
(both)
39
Numeric Operators
+ : add - : subtract div : divide * : multiply mod : modulus
40
Conversion Functions
number() string() boolean()
41
Alternates
We can express a logical union Elements of element type a or b:
a|b
42
43
Extending Functionality
Systems that use XPath can add functions and features to suit their needs Most XSLT implementations add their own features.
44
In and Out
Input to an XSLT process is
a transformation or template, and a source document
Output is an XML tree or text file In XSLT, the terms "stylesheet" and "transformation" are interchangable
46
XSLT Process
INPUT
OUTPUT
Source Tree
XSLT PROCESS
Output Template: Stylesheet/ Transformation
47
48
An Input Document
In an XSL-implementing browser: <?xml version="1.0"?> <?xml-stylesheet href="stylesheet-name.xsl" type="text/xsl"?> <book><title>Catch-22</title> <logo url="catch22.jpg"/> <description>...</description> </book> Put the stylesheet declaration after the XML declaration in the XML file
49
50
XSLT Namepace
The real XSLT Namespace is: http://www.w3.org/1999/XSL/Transform Some older stylesheets support an Internet Explorer 5 syntax: "http://www.w3.org/TR/WD-xsl" Run away! These stylesheets depend on behaviors that are slightly different than the standard.
51
Template components
Blindly reproduced to output:
Elements not in the XSLT namespace (literal result elements) Text in the stylesheet (literal text)
52
xsl:value-of
xsl:value-of outputs a computed value select attribute computes value Usually used to fetch data from input tree
e.g. <xsl:value-of selelct=/book/title/>
53
Value of a Node
The value of a text node is its character data The value of an element is the character data of all of the text nodes directly contained or contained by descendants of the element The value of an attribute is the character data in its literal value
54
Full Transformations
Stylesheet constituents
Instructions at the top level
affect the entire transformation process include template rules (<xsl:template>) and other instructions and declarations
56
<xsl:template>
<xsl:template> elements contain most of the code for an XSLT transform <xsl:template> elements define template rules A template rule matches input nodes to an output template E.g. "when you see an EXAMPLE element:"
"Make a horizontal line." "Output the text 'Example'." "Output the example number."
57
58
A Template Rule
This example uses HTML result element types: <xsl:template match="EXAMPLE"> <hr/> <!-- horizontal rule --> <p>Example <xsl:number level="any" from="chapter"/></p> <pre> <!-- PRE element --> <xsl:apply-templates/> </pre> </xsl:template>
59
Rule Dependence
A rule for an element may need to process other elements ("nodes") E.g. 1: A rule for processing a chapter is dependent on rules for processing paragraphs E.g. 2: A rule for processing a cross-reference may be dependent on a rule for processing the target element
60
61
62
Example Document
<doc> <section> <title></title> <p></p> </section> <section> <title></title> <p></p> </section> </doc>
63
Example Transformation
<xsl:stylesheet version="1.0" xmlns:xsl="http://"> <xsl:template match="doc"> <html><body> <xsl:apply-templates/> </body> </html> </xsl:template>
64
Example (contd)
<xsl:template match="section"> <div> <xsl:apply-templates/> </div> </xsl:template>
65
Example (contd)
<xsl:template match="section/title"> <h1><xsl:apply-templates/></h1> </xsl:template> <xsl:template match="p"> <p><xsl:apply-templates/></p> </xsl:template>
66
67
<xsl:apply-templates>
The <xsl:apply-templates> instruction goes within <xsl:template> content The processor processes each node with the applicable template By default, apply-templates processes all subelements and text nodes of the current node.
68
69
Simple Example
Goal: transform a slideshow in XML into something simpler Target DTD is similar to HTML All element type names are from HTML
70
Input
<slideshow><title>My Title</title> <slides> <slide><title>Slide 1</title> <point>Point 1</point> <point>Point 2</point> </slide> <slide><title>Slide 2</title>... </slide> </slides> </slideshow>
71
Output
<html> <title>My Title</title> <h1>My Title</h1> <hr/> <h2>Slide 1</h2> <ul> <li>Point 1</li> <li>Point 2</li></ul> ... </html>
72
73
74
75
76
77
78
79
80
We're done!
XSLT Stylesheet evaluation will start with the root node The root node represents the whole document Evaluation will proceed to the document element node (slideshow) Then to each of its children ...And so on, and so on
81
Final Stylesheet - 1
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform">
82
Final Stylesheet - 2
<xsl:template match="slideshow"> <html> <title> <xsl:apply-templates select="title"/> </title> <h1> <xsl:apply-templates select="title"/> </h1> <xsl:apply-templates select="slides"/> </html> </xsl:template>
83
Final Stylesheet - 4
<xsl:template match="slide"> <hr/> <h2><xsl:apply-templates select="title"/></h2> <ul><xsl:apply-templates select="point"/></ul> </xsl:template> <xsl:template match="point"> <li><xsl:apply-templates/></li> </xsl:template> </xsl:stylesheet>
84
85
Exercise: subpoints
How could we implement subpoints? <slide><title>A slide title</title> <point>A point <subpoints> <point>First subpoint</point> <point>Second subpoint</point> <point>Third subpoint</point> </subpoints> </point> </slide>
86
87
Implementing subpoints
<xsl:template match="subpoints"> <ul> <xsl:apply-templates/> </ul> </xsl:template>
88
89
Template Recursion
RESULT <li>...
subpoints
point ... ...
<ul>
<li>...</li> ... </ul> </li>
90
Looping
xsl:for-each lets us do the same thing for many nodes The body of the xsl:for-each is a full template It is instantiated once for each match Each time a different node is the "current" node
91
Example of Looping
<xsl:template match="repairs"> <TABLE> <xsl:for-each select="/repairs/repair"> <ROW> <TD><xsl:value-of select="date"/></TD> <TD><xsl:value-of select="location"/></TD> </ROW> </xsl:for-each> </TABLE> </xsl:template>
92
Current Node
Relative location paths are relative to the current node The current node is also available through a current() function <xsl:for-each select="/class/instructor"> Teacher: <xsl:value-of select="/teachers/teacher [@name=current()/@name]"/> </xsl:for-each>
93
Conditional Processing
Conditionals: xsl:if
E.g.: Only build a Table of Figures if there are figures <xsl:if test=".//figure"> <h1>Table of Figures</h1> <ul> ... </ul> </xsl:if>
95
Conditionals: <xsl:choose>
xml:choose is necessary when you need multiple options xsl:otherwise is like "else" Only one option is chosen, or else xsl:otherwise is used
96
Using <xsl:choose>
<xsl:choose> <xsl:when test="..[@type='chapter']"> Chapter</xsl:when> <xsl:when test="..[@type='appendix']"> Appendix</xsl:when> <xsl:otherwise> Section</xsl:otherwise> </xsl:choose>
97
99
100
Text Nodes
<xsl:text>This is text</xsl:text> xsl:text is good for inserting whitespace: <xsl:text> </xsl:text>
101
102
Processing Instructions
<xsl:processing-instruction name="xml-stylesheet"> href="book.css" type="text/css" </xsl:processing-instruction> creates the PI: <?xml-stylesheet href="book.css" type="text/css"?>
103
Comments
<xsl:comment>This file is automatically generated. Do not edit!</xsl:comment> creates the comment: <!--This file is automatically generated. Do not edit!-->
104
Copying
xsl:copy-of: copy of a result-tree fragment xsl:copy: easy way of copying the current node
105
xsl:copy-of
<xsl:template ...> <xsl:copy-of select="/foo/bar"/> </xsl:template> Copies selected elements from source to result Children are also copied Can also insert result tree fragments: <xsl:variable name="myvar"> <a><b/></a> </xsl:variable> <xsl:copy-of select="$myvar"/>
106
xsl:copy
<xsl:template match="p"> <xsl:copy/> </xsl:template> Namespace nodes of the current node are automatically copied Attributes and children of the node are not automatically copied
107
xsl:variable
Variable values can be numbers, strings, booleans, node-sets It is also possible to hold "result tree fragments": <xsl:variable name="res-tree"> Hello, my name is: <xsl:value-of select="@name"/> </xsl:variable> name attribute is name of variable Value is specified in select attribute or content Variables cannot be overwritten
108
Using xsl:variable
<xsl:template match="student"> <xsl:variable name="student-name" select="name"/> Congratulations <xsl:value-of select="$name"/>! <xsl:value-of select="$name"/>, you are soon to be the winner of our sweepstakes! Your friends will all say, I wish I were <xsl:value-of select="$name"/>. </xsl:template>
109
Parameters
Parameters are inputs from the "application" Could be specified through:
command line arguments browser GUI environment variables .ini file
110
xsl:param
<xsl:param name="foo"> Hello, my name is <xsl:value-of select="@name"/>. </xsl:param> <xsl:param name="foo" select="/foo/@default"/> A missing default is equivalent to empty string
111
Template Calling
xsl:apply-templates xsl:call-template <xsl:call-template name="foo"/> xsl:call-template is like apply-templates but:
template is chosen by name mode and select attributes are irrelevant does not change current node
112
Passing parameters
xsl:with-param: <xsl:call-template name="foo"> <xsl:with-param name='myparam1'> value </xsl:with-param> </xsl:call-template> <xsl:apply-templates/> <xsl:with-param name='myparam' select='value'/> </xsl:apply-templates>
113
Modes
What if we need the same element to be processed in many different ways?
As a heading As a TOC entry As an index entry For cross references
We need to write four different rules that apply to the same element Each rule will operate in a different mode
114
Using Modes
A template can declare that it only works in a particular mode <xsl:template match="..." mode="..."> ... </xsl:template> An apply-templates instruction can declare that it needs a template that has a certain mode: <xsl:apply-templates select="..." mode="..."/>
115
Modes Example
<xsl:template match="SECTION"> ... </xsl:template> <xsl:template match="SECTION" mode="TOC"> ...TOC handling... </xsl:template> <xsl:template match="BOOK"> <h1>Table of Contents</h1> <xsl:apply-templates mode="TOC"/> <hr/> <xsl:apply-templates/> </xsl:template>
116
Inclusion Instructions
xsl:include: include another stylesheet xsl:import: include ... with lower precedence
118
xsl:include
Includes another stylesheet Absolute: <xsl:include href="http://.../foo.xsl"/> Relative: <xsl:include href="foo.xsl"/> Stylesheets may not include themselves directly or indirectly
119
Import Instructions
Like inclusion except that definitions in the importing stylesheet take precedence over those in the imported stylesheet Absolute: <xsl:import href="http://.../foo.xsl"/> Relative: <xsl:import href="foo.xsl"/>
120
Input Management
Input Descriptions
xsl:key: define searchable index xsl:namespace-alias: equate two namespaces xsl:strip-space: define space stripping element types xsl:preserve-space: define space preserving element types
122
Keys
Keys allow you to look up elements by name:
Customers by phone number Chapters by a unique identifier
123
<xsl:value-of select='key("employees","e98302")/name'/>
<xsl:value-of select='key("employees",@empid)/name'/> </xsl:template>
126
127
xsl:namespace-alias
<xsl:namespace-alias xmlns:axsl="http://something/bogus" stylesheet-prefix="axsl" result-prefix="xsl"/> <xsl:template match="/"> <axsl:stylesheet> ... </axsl:stylesheet> </xsl:template>
128
Space Stripping
In machine to machine applications, whitespace between nodes is usually irrelevant xsl:strip-space strips space from specified elements Strip space from address and date elements: <xsl:strip-space elements="address date"/> Strip space from all elements: <xsl:strip-space elements="*"/>
129
Space Preserving
If certain element types need spacing to be preserved: <xsl:preserve-space elements="foo bar"/>
130
Output Management
Output Descriptions
xsl:output: various output options xsl:decimal-format: printing numbers
132
xsl:output
(Quick overview!) <xsl:output method = "xml" | "html" | "text" | other version = nmtoken encoding = string omit-xml-declaration = "yes" | "no" standalone = "yes" | "no" doctype-public = string doctype-system = string cdata-section-elements = qnames indent = "yes" | "no" media-type = string />
133
Error Management
Messages
xsl:message generates messages Presentation depends on application:
GUI dialog box standard error logfile
<xsl:if test="not( /foo/bar )"> <xsl:message> Sorry, you need a /foo/bar </xsl:message> </xsl:if>
135
Fallback
If you see xsl:foo and don't know how to handle it, you should look for an xsl:fallback element <xsl:foo> <xsl:fallback>Sorry, your XSLT processor doesn't have that feature! </xsl:fallback> </xsl:foo>
136
More Information
Specs:
http://www.w3.org/TR/xslt http://www.w3.org/TR/xpath.html http://www.w3.org/TR/xslt20/ http://www.w3.org/TR/xpath20/ XSLT : Programmer's Reference by Michael Kay (Author) Beginning XSLT by Jeni Tennison XSLT and XPath On The Edge by Jeni Tennison Definitive XSLT and XPath by G. Ken Holman
Mailing list:
http://www.biglist.com/lists/xsl-list/
137