Transcript Slide 1

Extensible Stylesheet
Language (XSL)
1
XHTML
2
XHTML
• XHTML stands for EXtensible HyperText Markup
Language
• XHTML is aimed to replace HTML
• XHTML is almost identical to HTML 4.01
• XHTML is a stricter and cleaner version of HTML
• XHTML is HTML defined as an XML application
• XHTML is a W3C Recommendation
3
HTML Vs. XHTML
• The Most Important Differences:
- XHTML elements must be properly nested
- XHTML documents must be well-formed
- Tag names must be in lowercase
- All XHTML elements must be closed
4
XML Namespaces
5
XML Namespaces
• When an element name appears in two different
XML documents, we would like to know that it
has the same meaning in both documents
- Is the tag <title> used as the <title> XHTML tag in
both documents?
- If two documents about books have the tag
<number>, does it mean that they use the same
system for cataloging books?
6
What XML Namespaces are
and What They are not
• Namespaces merely provide a mechanism for
creating unique names (for elements and
attributes) that can be used in XML documents all
over the Web
- A namespace is just a collection of names that were
created for a specific domain of applications
• Namespaces are not DTDs and they do not
provide a mechanism for validation of XML
documents using multiple DTDs
7
Identifying an XML Namespace
• A name space is identified by a URI
• The URI does not have to point to anything
- It is merely used as a mechanism for creating unique
names
• An element name or attribute name from a
namespace has two parts
prefix identifies
the namespace
prefix:name
name is just a
name from the
namespace
8
Recognizing Namespace
• When an application sees a qualified name, it
may recognize it and act accordingly
- A browser identifies tags that belong to the XHTML
namespace and processes them
- An XSLT processor identifies tags and attributes that
belong to the XSLT namespace and executes them
9
Declaring a Namespace
• An XML namespace is declared in the xmlns
attribute
<foo:book xmlns:foo=“http://www.foo.org/”>
<foo:title> XML Namespaces </foo:title>
<foo:author> John Doe </foo:author>
</foo:book>
Using foo as the prefix, instead of using the
URI, is more convenient
10
The Default Namespace
• The default namespace is declared without a
prefix
<book xmlns=“http://www.foo.org/”>
<title> XML Namespaces </title>
<author> John Doe </author>
</book>
All the above elements belong to the
default namespace
11
DTDs as Namespaces
• The URI of a namespace may point to a DTD
• A DTD defines a namespace comprising all its
element names and attribute names
- But it is just a namespace – not a DTD!
12
This document is
invalid according
to either DTD!
<bib:book
Example
xmlns:bib=“http://www.acm.org/bibliography.dtd”
xmlns:isbn=“http://www.isbn-org.org/def.dtd”>
<bib:title> Proceedings of SIGMOD </bib:title>
<bib:number> 472010 </bib:number>
<isbn:number> 1-58113-332-4 </isbn:number>
</bib:book>
But the document is well formed! (e.g., in the
book element, attribute names are unique)
13
Alternatively, One Namespace can be
Declared as the Default
<book
xmlns=“http://www.acm.org/bibliography.dtd”
xmlns:isbn=“http://www.isbn-org.org/def.dtd”>
<title> Proceedings of SIGMOD </title>
<number> 472010 </number>
<isbn:number> 1-58113-332-4 </isbn:number>
</book>
This document is well formed but
invalid according to either DTD!
14
Scope of Namespaces
• The scope of a namespace declaration is the
element containing the declaration and all
descendant elements
- Must use the prefix anywhere in the scope
• Only the default namespace can be
redeclared
• More than one namespace can be declared in
the same scope
- At most one can be the default namespace
- All others must have unique prefixes
15
What about Attributes?
• Recall that element names and attribute names must
be qualified if they belong to a nondefault namespace
• Unqualified element names belong to the default
namespace (if they are inside the scope)
• However, an unqualified attribute does not belong to
the default namespace
• An unqualified attribute is processed according to the
rules that apply to its element name
16
Extensible Stylesheet
Language (XSL)
17
XSL
XSL is a standard that consists of three parts:
• XPath (navigation in documents)
• XSLT (transformation of documents)
• XSLFO (FO for formatting objects)
- This is a rather complex language for typesetting (i.e.,
preparing text for printing)
- It will not be taught
18
XSL Transformations (XSLT)
An Example
19
XSLT
• XSLT is a language for transforming XML
documents into other XML documents
- For example, XHTML, WML
- Can also transform XML to text documents
• An XSLT program is itself an XML document
(called an XSL stylesheet) that describes the
transformation process for input documents
20
XSLT Processors
DTD
XML
XSL
XHTML
WML
XSLT Processor
Text
21
Web Pages – The Whole Picture
Knowledge
XSL
XHTML
XML
Data
Doc.
Structure
CSS
Style
Layout
Web Page
22
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="UK">
<title>Dark Side of the Moon</title>
<artist>Pink Floyd</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Space Oddity</title>
<artist>David Bowie</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Aretha: Lady Soul</title>
<artist>Aretha Franklin</artist>
<price>9.90</price>
</cd>
catalog.xml
23
</catalog>
Valid XML!
Commands are XML elements
Includes XHTML with the namespace xsl
elements
<?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>
<head><title>cd catalog</title></head>
<body><h1>This is a cd catalog!</h1></body>
</html>
</xsl:template>
</xsl:stylesheet>
catalog.xsl
24
Applying XSL Stylesheets to XML
There are several ways of applying an XSL
stylesheet to an XML document:
- Directly applying an XSLT processor to the XML
document and the XSL stylesheet
- Calling an XSLT processor from within a program
- Adding to the XML document a link to the XSL
stylesheet and letting the browser do the
transformation
• The resulting XHTML document is shown instead
of the original XML
25
How Does XSLT Work?
• An XSL stylesheet is a collection of templates that are
applied to source nodes (i.e., nodes of the given XML)
• Each template has a match attribute that specifies to
which source nodes the template can be applied
• Each source node has a template that matches it
• The current source node is processed by applying a
template that matches this node
• When processing a node, it is possible (but not
necessary) to recursively process other nodes, e.g., the
children of the processed node
• The XSLT processor processes the document root (/)
26
Templates
• A template has the form
<xsl:template match="pattern">
...
</xsl:template>
• The content of a template consists of
- XML elements (e.g., XHTML) and text that are
copied to the result
- XSL elements (<xsl:…>) that are actually
instructions
• The pattern syntax is a subset of XPath
27
<?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>
<head><title>cd catalog</title></head>
<body><h1>This is a cd catalog!</h1></body>
</html>
</xsl:template>
</xsl:stylesheet>
catalog1.xsl
28
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl"
href="catalog1.xsl"?>
<catalog>
<cd country="UK">
<title>Dark Side of the Moon</title>
<artist>Pink Floyd</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Space Oddity</title>
<artist>David Bowie</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Aretha: Lady Soul</title>
<artist>Aretha Franklin</artist>
<price>9.90</price>
</cd>
</catalog>
catalog1.xml
29
The Result
<html>
<head>
<META http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>cd catalog</title>
</head>
<body>
<h1>This is a cd
Automatically
catalog!</h1>
added to <head>
</body>
</html>
30
Examples of Match Attributes
• match="cd",
- All elements with tag name cd
• match="//cd",
match="/catalog/cd/artist"
- All matches of the absolute XPath
• match="cd/artist"
- All artist nodes that have a cd parent
• match="catalog//artist"
- All artist nodes that have a catalog ancestor
• match="cd[@country='UK']/artist"
31
<xsl:apply-templates>
• Processing starts by applying a template to the root
- If no specified template matches the root, then one is
inserted by default (see the next slide)
• The XSL stylesheet must specify explicitly whether
templates should be applied to descendants of a node
• It is done by putting inside a template the instruction:
<xsl:apply-templates select="xpath"/>
- In xpath, the current processed node is used as the
context node
• Without the select attribute, this instruction
processes all the children of the current node
(including text nodes)
32
Default Templates
• XSL provides implicit built-in templates that match
every element and text nodes
<xsl:template match="/|*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
• Templates we write always override these built-in
templates (when they match)
33
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>
Dark Side of the Moon
Pink Floyd
10.90
Space Oddity
David Bowie
9.90
…
34
<?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="cd">
<h2>A cd!</h2>
</xsl:template>
</xsl:stylesheet>
<h2>A cd!</h2>
<h2>A cd!</h2>
<h2>A cd!</h2>
35
<?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="cd[@country='UK']">
<h2>A cd!</h2></xsl:template>
</xsl:stylesheet>
<h2>A cd!</h2>
<h2>A cd!</h2>
Aretha: Lady Soul
Aretha Franklin
9.90
36
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates
select="catalog/cd[@country='UK']/artist"/>
</xsl:template>
<xsl:template match="artist">
<h2>An artist!</h2>
</xsl:template>
</xsl:stylesheet>
<h2>An artist!</h2>
<h2>An artist!</h2>
37
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates
select="cd[@country='UK']/artist"/>
</xsl:template>
<xsl:template match="artist">
<h2>An artist!</h2>
</xsl:template>
</xsl:stylesheet>
38
The Most Frequently Used
Elements of XSL
• <xsl:value-of select="xpath"/>
- This element extracts the value of a node from the
node list located by xpath
• <xsl:for-each select="xpath"/>
- This element loops over all the nodes in the node
list located by xpath
• <xsl:if test="cond"/>,
<xsl:if test="xpath"/>, etc.
- This element is for conditional processing
39
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
Example 1
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><head><title>cd catalog</title></head>
<body>
<h1>CD catalog</h1>
Currently selected element is
the context (current) node
<ul>
<xsl:for-each select="catalog/cd">
<li><xsl:value-of select="title"/>
[<xsl:value-of select="artist"/>]</li>
</xsl:for-each>
</ul></body></html>
</xsl:template>
</xsl:stylesheet>
catalog2.xsl
40
Example 2
<?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><head><title>cd catalog</title></head>
<body>
<h1>CD catalog</h1>
<ul>
<xsl:for-each select="catalog/cd">
<li><xsl:apply-templates select="."/></li>
</xsl:for-each>
</ul></body></html>
</xsl:template>
catalog3.xsl
41
Example 2
(cont.)
Entities replace characters
<xsl:template match="cd">
Price<10 → price&lt;10
<b><xsl:value-of select="artist"/></b>:
<xsl:value-of select="title"/>
<xsl:if test="price&lt;10">
(<em>now on sale: $<xsl:value-of select="price"/>
</em>)
</xsl:if>
</xsl:template>
</xsl:stylesheet>
catalog3.xsl
42
<xsl:choose>:
Switch syntax for conditions
Example 3
<xsl:choose>
<xsl:when test="price &lt; 9">
<em>Special price!</em>
</xsl:when>
<xsl:when test="price&gt;9 and price&lt;=10">
<i>Good price!</i>
</xsl:when>
<xsl:otherwise>
(Normal price.)
</xsl:otherwise>
</xsl:choose>
43
The <xsl:sort> Element
• The <xsl:sort> element is used to sort the
list of nodes that are looped over by the
<xsl:for-each> element
• Thus, the <xsl:sort> must appear inside the
<xsl:for-each> element
• The looping is done in sorted order
44
<?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><head><title>cd catalog</title></head>
<body>
<h1>CD
CDs are iterated in ascending
order of the titles
catalog</h1>
<ul>
<xsl:for-each select="catalog/cd">
<xsl:sort select="title"/>
<li><xsl:value-of select="title"/></li>
</xsl:for-each>
</ul></body></html>
</xsl:template>
</xsl:stylesheet>
catalog4.xsl
45
Setting Values in Attributes
• The <xsl:value-of> element cannot be used
within attribute value
• However, we can insert expressions into attribute
values, by putting the expression inside curly
braces ({})
• Alternatively, we can use <xsl:element> in
order to construct XML elements
46
An Example
• In the following example, we add to each CD entitled t a
link to the URL /showcd.php?title=t
<xsl:template match="cd">
<b><xsl:value-of select="artist"/></b>:
<a href="/showcd.php?title={./title}">
<xsl:value-of select="title"/>
</a>
</xsl:template>
47
Using <xsl:element>
<xsl:template match="cd">
<b><xsl:value-of select="artist"/></b>:
<xsl:element name="a">
<xsl:attribute name="href">
showcd/?title=<xsl:value-of select="title"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</xsl:element>
</xsl:template>
48
On XSL Code
• Typically, an XSLT program can be written in
several, very different ways
- Templates can sometime replace loops and vice versa
- Conditions can sometimes be replaced with XPath
predicates (e.g., in the select attribute) and vice versa
• A matter of convenience and elegancy
49
On Recursive Templates
• It is not always possible to avoid recursive
templates
- That is, use only the template of the root
• Suppose that we want to write an XSL stylesheet
that generates a copy of the source 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)
50
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
Identity Transformation
</xsl:template>
</xsl:stylesheet>
Stylesheet
51
Generating Valid XHTML
• By default, the documents that XSL stylesheets
generate are not valid XHTML
• Next, we will show how XSL stylesheet can be
changed in order to generate valid XHTML
52
The Original XSL 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>
<head><title>cd catalog</title></head>
<body><h1>This is a cd catalog!</h1></body>
</html>
</xsl:template>
</xsl:stylesheet>
53
The Original Transformation Result
<html>
<head>
No DOCTYPE
<META http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>cd catalog</title>
</head>
<body>
<h1>This is a cd
Uppercase tag name,
catalog!</h1>
unclosed element
</body>
</html>
54
Modifying the XSL Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output
method="html"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system=
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
<xsl:template match="/"> …
55
The Transformation Result
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>cd catalog</title>
</head>
<body>
META is not inserted
<h1>This is a cd catalog!</h1>
</body>
</html>
56
Some Other XSL Elements
• The <xsl:text> element inserts free text
in the output
• The <xsl:copy-of select="xpath">
creates a copy of the specified nodes
• The <xsl:comment> element creates a
comment node in the result tree
• The <xsl:variable> element defines a
variable (local or global) that can be used
within the program
57
W3Schools 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
58
The End!
These slides are based on those developed for the course:
http://www.cs.huji.ac.il/~dbi
59