extensible stylesheet language (xsl)

82
1 Extensible Extensible Stylesheet Language Stylesheet Language (XSL) (XSL)

Upload: tia

Post on 07-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Extensible Stylesheet Language (XSL). The XSL Standard. XSL consists of 3 parts: XPath (navigation in documents) XSLT (transformation of documents) XSLFO (FO for formatting objects ) A rather complex language for typesetting (e.g., for preparing text for printing) It will not be taught. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Extensible Stylesheet Language (XSL)

1

Extensible Stylesheet Extensible Stylesheet Language (XSL)Language (XSL)

Page 2: Extensible Stylesheet Language (XSL)

2

The XSL StandardThe XSL Standard

XSL consists of 3 parts:

• XPathXPath (navigation in documents)

• XSLTXSLT (transformation of documents)

• XSLFOXSLFO (FO for formatting objects)

- A rather complex language for typesetting

(e.g., for preparing text for printing)- It will not be taught

Page 3: Extensible Stylesheet Language (XSL)

3

XML Path LanguageXML Path Language(XPath)(XPath)

Page 4: Extensible Stylesheet Language (XSL)

4

XML example revisited: XML example revisited:

Page 5: Extensible Stylesheet Language (XSL)

5

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries> world.xmlworld.xml

Page 6: Extensible Stylesheet Language (XSL)

6

The XML DOM ModelThe XML DOM Model

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document rootThe root is implicit(Does not appear in the

text of the XML document)

Page 7: Extensible Stylesheet Language (XSL)

7

<!ELEMENT countries (country*)> <!ELEMENT country (name,population?,city*)> <!ATTLIST country continent CDATA #REQUIRED> <!ELEMENT name (#PCDATA)> <!ELEMENT city (name)><!ATTLIST city capital (yes|no) "no"> <!ELEMENT population (#PCDATA)> <!ATTLIST population year CDATA #IMPLIED> <!ENTITY eu "Europe"> <!ENTITY as "Asia"><!ENTITY af "Africa"><!ENTITY am "America"><!ENTITY au "Australia"> world.dtdworld.dtd

Page 8: Extensible Stylesheet Language (XSL)

8

The XPath LanguageThe XPath Language

• XPath expressions are used for addressing elements (nodes) of an XML document

• Used in XSLT (next subject today) and in XQuery (a query language for XML)

• The syntax resembles that of the Unix file system- But the semantics have some substantial differences

/countries/country[population>10000000]

Page 9: Extensible Stylesheet Language (XSL)

9

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml/countries/country[population>10000000]

Page 10: Extensible Stylesheet Language (XSL)

10

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//country[@continent="Asia"]/city

Page 11: Extensible Stylesheet Language (XSL)

11

XPath ExpressionsXPath Expressions

• An XPath expression (or just XPath for short) matches paths in the XML tree

• An absolute path begins with the root of the document- Starts with "/" (or "//")- For example, /countries/country/city, //city

• A relative path begins with a context node that is defined by the application that uses the XPath- For example, city/name, or ./name

Page 12: Extensible Stylesheet Language (XSL)

12

Applying XPath to XMLApplying XPath to XML

• Formally, the result of applying an XPath e to an XML document (or a context node in the

document) is the list of all nodes n in the document, such that e matches the path from the root (or the context node) to n

• The order in the list is defined by the order of the nodes in the document

Page 13: Extensible Stylesheet Language (XSL)

13

XPath Steps and Axis XPath Steps and Axis

• An XPath describes a sequence of steps that together characterize a path

• A step is defined by an axis that specifies a tree relationship between nodes- More particularly, the axis describes how to get from the

current node to the next one- For example, parent-child, child-parent, ancestor-

descendant, etc.

• Consecutive steps are separated by /

Page 14: Extensible Stylesheet Language (XSL)

14

Child AxisChild Axis

• A child axis has the simple form tagName - Go to an element child with the tag tagName

• For example, - /tagName matches the tagName child of root- city/name - /countries/country/city

• The child axis * matches every tag- For example: /*/*/city, */name

Page 15: Extensible Stylesheet Language (XSL)

15

Child-Axis ExamplesChild-Axis Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

/countries

Page 16: Extensible Stylesheet Language (XSL)

16

Child-Axis ExamplesChild-Axis Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

/countries/country/city

Page 17: Extensible Stylesheet Language (XSL)

17

Child-Axis ExamplesChild-Axis Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

city/name

Context

Page 18: Extensible Stylesheet Language (XSL)

18

Child-Axis ExamplesChild-Axis Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

/*/country/*

An attribute is not an element child!

Page 19: Extensible Stylesheet Language (XSL)

19

Self and Descendant-or-Self Self and Descendant-or-Self

• The self axis “.” denotes the identity relationship - That is, the step “remain in the current node”- /countries/country/. ≡ /countries/country- country/./city ≡ country/city

• The descendant-or-self axis means: either stay in the current node or go to some descendant of the current node - descendant-or-self:node(),

• // is a shotrcut notation for /descendant-or-self:node()/- For example, country//name

Page 20: Extensible Stylesheet Language (XSL)

20

Descendant ExamplesDescendant Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

/countries//name

Page 21: Extensible Stylesheet Language (XSL)

21

Descendant ExamplesDescendant Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

.//*

Context

Page 22: Extensible Stylesheet Language (XSL)

22

Other Axis TypesOther Axis Types

• The parent axis “..” denotes the parent relationship- That is, the step “go to the parent of the current node”- For example, //name/../population

• XPath has more axis types (denoted by a different syntax from the ones shown earlier):- descendant- ancestor- ancestor-or-self- following-sibling- preceding-sibling- …

Page 23: Extensible Stylesheet Language (XSL)

23

Referring AttributesReferring Attributes

• The attribute axis is denoted @attName - That is, “go to the attribute attName of the current node”

• The operator @* matches every attribute

Page 24: Extensible Stylesheet Language (XSL)

24

Attribute ExamplesAttribute Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

//country/@continent

Page 25: Extensible Stylesheet Language (XSL)

25

Attribute ExamplesAttribute Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

@continent

Context

Page 26: Extensible Stylesheet Language (XSL)

26

Attribute ExamplesAttribute Examples

country

Asia Israel 6199008

name populationcontinent

AshdodJerusalem

namename

2001

year

countries

city

no

capital

yes

capital

city

document root

//@*

Page 27: Extensible Stylesheet Language (XSL)

27

XPath PredicatesXPath Predicates

• Predicates in XPath are used for filtering out steps

• For example, //city[@captial="yes"] will match only capital cities

• Formally, given a predicate [PExpr], the expression PExpr is transformed into a Boolean value and the step is taken only if this value is true- The node reached in the last step is the context node

• XPath has a rather rich language for predicate expressions; we only demonstrate common ones

Page 28: Extensible Stylesheet Language (XSL)

28

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//country[./population>10000000]

• The XPath ./population is transformed into a number by taking its embedded text

• The XPath ./population is relative to the current node (i.e., country) in the path

• Equivalent to //country[population>10000000]

Page 29: Extensible Stylesheet Language (XSL)

29

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//country[.//city]

An XPath evaluates to true if and only if its result is not empty

Page 30: Extensible Stylesheet Language (XSL)

30

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//country[//city]

Why?

Page 31: Extensible Stylesheet Language (XSL)

31

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//country[population[.>3000000 and @year>2003]]

Page 32: Extensible Stylesheet Language (XSL)

32

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml

//country[name="Israel" or name="Spain"]/population

Page 33: Extensible Stylesheet Language (XSL)

33

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//country/city[2]

• A number acts as an index

• That is, the number n evaluates to true if n is the position of the node among all those reached in the last step (i.e., city)

Page 34: Extensible Stylesheet Language (XSL)

34

FunctionsFunctions

• Inside XPath predicates, you can use a set of predefined functions

• Here are some examples:- last() – returns the number of nodes obtained from

the last axis step- position() – returns the position of the node in the list

of nodes satisfying the last axis step- name() – returns the name (tag) of the current node- count(XPath) – returns the number of nodes

satisfying XPath

Page 35: Extensible Stylesheet Language (XSL)

35

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//country/city[last()]

Page 36: Extensible Stylesheet Language (XSL)

36

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//city[position()<2]

Page 37: Extensible Stylesheet Language (XSL)

37

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//*[name()="city" or name()="country"]

Page 38: Extensible Stylesheet Language (XSL)

38

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//*[starts-with(name(),“c")]

Page 39: Extensible Stylesheet Language (XSL)

39

<?xml version="1.0"?>

<!DOCTYPE countries SYSTEM "world.dtd">

<countries>

<country continent="&as;">

<name>Israel</name>

<population year="2001">6199008</population>

<city capital="yes"><name>Jerusalem</name></city>

<city><name>Ashdod</name></city>

</country>

<country continent="&eu;">

<name>France</name>

<population year="2004">60424213</population>

</country>

</countries>

world.xmlworld.xml//country[count(./city)>=1]

Page 40: Extensible Stylesheet Language (XSL)

40

Final RemarksFinal Remarks

• The syntax of XPath that was presented here is the abbreviated syntax

• For example, city/../@name is an abbrv. of

child::city/parent::node()

/attribute::name

• More details on XPath:- XPath tutorial in W3Schools- XPath W3C Recommendation

Page 41: Extensible Stylesheet Language (XSL)

41

XSL Transformations XSL Transformations (XSLT)(XSLT)

An Example:

Useful Links in the DBI site

Page 42: Extensible Stylesheet Language (XSL)

42

XSLTXSLT

• XSLT is a language for transforming XML documents into other XML documents- For example, XHTML, WML- Can also transform XML to general text documents, e.g.,

SQL programs

• An XSLT program is itself an XML document (called an XSL stylesheet) that describes the transformation process for input documents

Page 43: Extensible Stylesheet Language (XSL)

43

Text

WML

DTD

XSLT ProcessorsXSLT Processors

XML

XSL

XSLT Processor

XHTML

Page 44: Extensible Stylesheet Language (XSL)

44Web Page

Presentation

Doc. Structure

Data

Web Pages Web Pages –– The Whole Picture The Whole Picture

XMLXML

XSLXSL

XHTMLXHTML

Style

KnowledgeKnowledge

CSSCSS

DynamicsJavaScriptJavaScript

Page 45: Extensible Stylesheet Language (XSL)

45

<?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>

catalog.xmlcatalog.xml

Page 46: Extensible Stylesheet Language (XSL)

46

<?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>

Valid XML! Commands are XML elements with the namespace xslIncludes XHTML

elements

catalog.xslcatalog.xsl

Page 47: Extensible Stylesheet Language (XSL)

47

Applying XSL Stylesheets to XMLApplying 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, not the original XML

Page 48: Extensible Stylesheet Language (XSL)

48

Processing XSL in JavaProcessing XSL in Java

You can use the XALAN package of Apache in order to process XSL transformations

java org.apache.xalan.xslt.Process -IN myXmlFile.xml -XSL myXslFile.xsl -OUT myOutputFile.html

Page 49: Extensible Stylesheet Language (XSL)

49

How Does XSLT Work?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 to recursively process other nodes, e.g., the children of the current node

• Finally, the XSLT processor simply processes the root node of the document (that matches /)

Page 50: Extensible Stylesheet Language (XSL)

50

TemplatesTemplates

• 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 syntax for the pattern is a subset of XPath

Page 51: Extensible Stylesheet Language (XSL)

51

<?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

Page 52: Extensible Stylesheet Language (XSL)

52

<?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

view catalog1.xm

l

Page 53: Extensible Stylesheet Language (XSL)

53

The ResultThe 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 catalog!</h1>

</body>

</html>

Implicitly added to <head>

Page 54: Extensible Stylesheet Language (XSL)

54

Examples of Match AttributesExamples 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"

Page 55: Extensible Stylesheet Language (XSL)

55

• Processing starts by applying a temp. to the root - If no specified template matches the root, then one is

inserted by default (see the next slide)

• You must specify explicitly whether templates should be applied to descendants of a node

• Done using the instruction:<xsl:apply-templates select="xpath"/>

- In xpath, cur. processed node is the context node

• Without the select attribute, this instruction processes all the children of the current node (including text nodes)

<xsl:apply-templates><xsl:apply-templates>

Page 56: Extensible Stylesheet Language (XSL)

56

Default TemplatesDefault 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)

<xsl:template match="/|*"><xsl:apply-templates/>

</xsl:template>

<xsl:template match="text()|@*"><xsl:value-of select="."/>

</xsl:template>

Page 57: Extensible Stylesheet Language (XSL)

57

<?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

Page 58: Extensible Stylesheet Language (XSL)

58

<?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>

Page 59: Extensible Stylesheet Language (XSL)

59

<h2>A cd!</h2>

<h2>A cd!</h2>

Aretha: Lady Soul

Aretha Franklin

9.90

<?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>

Page 60: Extensible Stylesheet Language (XSL)

60

<h2>An artist!</h2>

<h2>An artist!</h2>

<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>

Page 61: Extensible Stylesheet Language (XSL)

61

<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>

Why?

Page 62: Extensible Stylesheet Language (XSL)

62

Frequently Used Elements of XSLFrequently Used Elements of XSL

• <xsl:value-of select="xpath"/>- This element extracts the value of a node from the

nodelist 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

Page 63: Extensible Stylesheet Language (XSL)

63

<?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:value-of select="title"/>

[<xsl:value-of select="artist"/>]</li>

</xsl:for-each>

</ul></body></html>

</xsl:template>

</xsl:stylesheet>

Currently selected element is the context node

Example 1

catalog2.xsl

view catalog2.xm

l

Page 64: Extensible Stylesheet Language (XSL)

64

<?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>

Example 2

catalog3.xsl

Page 65: Extensible Stylesheet Language (XSL)

65

<xsl:template match="cd">

<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>

Example 2 (cont.)

Entities replace characters

price>10 → price&lt;10

catalog3.xsl

view catalog3.xm

l

Page 66: Extensible Stylesheet Language (XSL)

66

Example 3

<xsl:choose>:

Switch syntax for conditions

<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>

Page 67: Extensible Stylesheet Language (XSL)

67

The The <xsl:sort><xsl:sort> Element 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

Page 68: Extensible Stylesheet Language (XSL)

68

<?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">

<xsl:sort select="title"/>

<li><xsl:value-of select="title"/></li>

</xsl:for-each>

</ul></body></html>

</xsl:template>

</xsl:stylesheet>

CDs are iterated in ascending order of the titles

catalog4.xsl

view catalog4.xm

l

Page 69: Extensible Stylesheet Language (XSL)

69

Setting Values in AttributesSetting Values in Attributes

• The <xsl:value-of> element cannot be used within an 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

Page 70: Extensible Stylesheet Language (XSL)

70

An ExampleAn 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>

view catalog5.xm

l

Page 71: Extensible Stylesheet Language (XSL)

71

UsingUsing <xsl:element> <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>

Page 72: Extensible Stylesheet Language (XSL)

72

On XSL CodeOn 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

Page 73: Extensible Stylesheet Language (XSL)

73

On Recursive TemplatesOn 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)

Page 74: Extensible Stylesheet Language (XSL)

74

<?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> </xsl:template>

</xsl:stylesheet>

Identity TransformationStylesheet

Page 75: Extensible Stylesheet Language (XSL)

75

Generating Valid XHTMLGenerating 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

Page 76: Extensible Stylesheet Language (XSL)

76

<?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>

The Original XSL ExampleThe Original XSL Example

Page 77: Extensible Stylesheet Language (XSL)

77

<html>

<head>

<META http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>cd catalog</title>

</head>

<body>

<h1>This is a cd catalog!</h1>

</body>

</html>

Uppercase tag name, unclosed element

No DOCTYPE

The Original Transformation ResultThe Original Transformation Result

Page 78: Extensible Stylesheet Language (XSL)

78

<?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="/"> …

Modifying the XSL ExampleModifying the XSL Example

Page 79: Extensible Stylesheet Language (XSL)

79

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>cd catalog</title>

</head>

<body>

<h1>This is a cd catalog!</h1>

</body>

</html>

META is not inserted

The Transformation ResultThe Transformation Result

Page 80: Extensible Stylesheet Language (XSL)

80

Some Other XSL ElementsSome 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

Page 81: Extensible Stylesheet Language (XSL)

81

Costello’s TutorialCostello’s Tutorial

Costello has an excellent, 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 Costello’s

tutorial)

(See DBI timetable for a reference)

Page 82: Extensible Stylesheet Language (XSL)

82

W3Schools Tutorial on XSLTW3Schools 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