Presentation is loading. Please wait.

Presentation is loading. Please wait.

Extensible Stylesheet Language (XSL)

Similar presentations


Presentation on theme: "Extensible Stylesheet Language (XSL)"— Presentation transcript:

1 Extensible Stylesheet Language (XSL)

2 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

3 XML Path Language (XPath)

4 XML example revisited:

5 world.xml <?xml version="1.0"?>
<!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries> world.xml

6 The XML DOM Model

7 world.dtd <!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.dtd

8 /countries/country[population>10000000]
XPath /countries/country[population> ] XPath is a language for addressing nodes of an XML document XPath is used in XSL Transformations (next subject today) and in XQuery (a query language for XML) XPath expressions are written in a syntax that resembles paths in file systems

9 /countries/country[population>10000000]
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

10 //country[@continent="Asia"]/city
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

11 XPath 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 given context node For example, city/name, or ./name Formally, the result of applying an XPath e to a document (or a context node) is the list of all nodes n, such that e matches some path that ends with n The order of the list is defined by the order of the nodes in the document

12 XPath Steps and Axis An XPath describes a sequence of steps that together constitute a path A step is characterized by an axis that specifies a tree relationship between nodes For example, parent-child, child-parent, ancestor-descendant, etc. Consecutive steps are separated by /

13 Child Axis A child axis has the simple form tagName and it means: go to an element child with the tag tagName For example, the absolute path /tagName matches the tagName child of the document root city/name /countries/country/city Alternatively, a child axis can have the form * that matches every tag For example: /*/*/city, */name

14 Child-Axis Examples /countries

15 /countries/country/city
Child-Axis Examples /countries/country/city

16 Child-Axis Examples city/name Context

17 An attribute is not an element child!
Child-Axis Examples /*/country/* An attribute is not an element child!

18 Self and Descendant-or-Self Axis
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, denoted descendant-or-self:node(), means: either stay in the current node or go to some descendant of the current node // is a shotrcut notation for /descendant-or-self:node()/ For example, country//name

19 /countries//name Descendant Examples

20 .//* Descendant Examples Context

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

22 Referring Attributes The attribute axis is denoted @attName
That is, "go to the attribute attName of the current node" The matches every attribute

23 //country/@continent
Attribute Examples

24 Attribute Examples @continent Context

25 Attribute Examples

26 XPath Predicates Predicates in XPath are used for filtering out steps
For example, will match only capital cities Formally, given a predicate [PExpr], the expression PExpr is transformed into a Boolean value and the step is taken if and only if this value is true XPath has a rather rich language for predicate expressions and we will only demonstrate common ones

27 //country[./population>10000000]
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries> 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> ]

28 An XPath evaluates to true if and only if its result is not empty
//country[.//city] world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries> An XPath evaluates to true if and only if its result is not empty

29 Why? //country[//city] world.xml <?xml version="1.0"?>
<!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries> Why?

30 //country[population[.>3000000 and @year>2003]]
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

31 //country[name="Israel" or name="France"]/population
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

32 A number acts as an index
//country/city[2] world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries> 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)

33 Functions XPath predicates 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 of the current node count(XPath) – returns the number of nodes satisfying XPath

34 //country/city[last()]
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

35 //city[position()<2]
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

36 //*[name()="city" or name()="country"]
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

37 //*[starts-with(name(),"n")]
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

38 //country[count(./city)>=1]
world.xml <?xml version="1.0"?> <!DOCTYPE countries SYSTEM "world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001"> </population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004"> </population> </countries>

39 child::city/parent::node() /attribute::name
Final Remarks The syntax of XPath that was presented here is the abbreviated syntax For example, is an abbreviation of child::city/parent::node() /attribute::name More details on XPath: XPath tutorial in W3Schools XPath W3C Recommendation

40 XSL Transformations (XSLT)
An Example

41 XSLT XSLT is a language for transforming XML documents into other XML documents For example, XHTML, WML Can also transform XML to 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

42 XSLT Processors XSLT Processor

43 Web Pages – The Whole Picture
Data Layout Knowledge Doc. Structure XSL XHTML Style CSS XML

44 catalog.xml <?xml version="1.0" encoding="ISO-8859-1"?>
<cd country="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> <cd country="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> </catalog> catalog.xml

45 Commands are XML elements with the namespace xsl
Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements <?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <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

46 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

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

48 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 at 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 (/)

49 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

50 catalog1.xsl <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <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

51 <?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> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> <cd country="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> </catalog> catalog1.xml

52 Automatically added to <head>
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 catalog!</h1> </body> </html> Automatically added to <head>

53 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

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

55 Default 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 <xsl:value-of select="."/>

56 <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" </xsl:stylesheet> Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90

57 <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="cd"> <h2>A cd!</h2> </xsl:template> </xsl:stylesheet> <h2>A cd!</h2>

58 <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template <h2>A cd!</h2></xsl:template> </xsl:stylesheet> <h2>A cd!</h2> Aretha: Lady Soul Aretha Franklin 9.90

59 <xsl:stylesheet version="1.0"
xmlns:xsl=" <xsl:template match="/"> <xsl:apply-templates </xsl:template> <xsl:template match="artist"> <h2>An artist!</h2> </xsl:stylesheet> <h2>An artist!</h2>

60 <xsl:stylesheet version="1.0"
xmlns:xsl=" <xsl:template match="/"> <xsl:apply-templates </xsl:template> <xsl:template match="artist"> <h2>An artist!</h2> </xsl:stylesheet>

61 The Most Frequently 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

62 Currently selected element is the context (current) node
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <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> Example 1 Currently selected element is the context (current) node catalog2.xsl

63 Example 2 catalog3.xsl <xsl:template match="/">
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <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

64 Entities replace characters
Example 2 (cont.) Entities replace characters price>10 → price<10 <xsl:template match="cd"> <b><xsl:value-of select="artist"/></b>: <xsl:value-of select="title"/> <xsl:if test="price<10"> (<em>now on sale: $<xsl:value-of select="price"/> </em>) </xsl:if> </xsl:template> </xsl:stylesheet> catalog3.xsl

65 Switch syntax for conditions
<xsl:choose>: Switch syntax for conditions Example 3 <xsl:choose> <xsl:when test="price < 9"> <em>Special price!</em> </xsl:when> <xsl:when test="price>9 and price<=10"> <i>Good price!</i> <xsl:otherwise> (Normal price.) </xsl:otherwise> </xsl:choose>

66 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

67 CDs are iterated in ascending order of the titles
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <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

68 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

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

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

71 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

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

73 Identity Transformation
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl=" version="1.0"> <xsl:output method="xml"/> <xsl:template match="*"> <xsl:element name="{name()}"> <xsl:for-each <xsl:attribute name="{name()}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <xsl:apply-templates/> </xsl:element> </xsl:template> </xsl:stylesheet> Identity Transformation Stylesheet

74 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

75 The Original XSL Example
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <head><title>cd catalog</title></head> <body><h1>This is a cd catalog!</h1></body> </html> </xsl:template> </xsl:stylesheet>

76 The Original Transformation 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> No DOCTYPE Uppercase tag name, unclosed element

77 Modifying the XSL Example
<?xml version="1.0" encoding="ISO "?> <xsl:stylesheet version="1.0" xmlns:xsl=" xmlns=" <xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system= " <xsl:template match="/"> …

78 The Transformation Result
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " <html xmlns=" <head> <title>cd catalog</title> </head> <body> <h1>This is a cd catalog!</h1> </body> </html> META is not inserted

79 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

80 Costello’s Tutorial (See DBI Lecture Schedule for a reference)
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)

81 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


Download ppt "Extensible Stylesheet Language (XSL)"

Similar presentations


Ads by Google