Extensible Stylesheet Language XSL - PowerPoint PPT Presentation

About This Presentation
Title:

Extensible Stylesheet Language XSL

Description:

(Does not appear in the text of the XML document) 7 !ELEMENT countries (country ... country[_at_continent='Asia']/city. 11. XPath Expressions ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 83
Provided by: Ben5152
Category:

less

Transcript and Presenter's Notes

Title: Extensible Stylesheet Language XSL


1
Extensible Stylesheet Language (XSL)
2
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

3
XML Path Language(XPath)
4
XML example revisited
5
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
world.xml
6
The XML DOM Model
7
lt!ELEMENT countries (country)gt lt!ELEMENT
country (name,population?,city)gt lt!ATTLIST
country continent CDATA REQUIREDgt lt!ELEMENT
name (PCDATA)gt lt!ELEMENT city (name)gt lt!ATTLIST
city capital (yesno) "no"gt lt!ELEMENT
population (PCDATA)gt lt!ATTLIST population year
CDATA IMPLIEDgt lt!ENTITY eu "Europe"gt lt!ENTITY
as "Asia"gt lt!ENTITY af "Africa"gt lt!ENTITY am
"America"gt lt!ENTITY au "Australia"gt
world.dtd
8
The XPath Language
/countries/countrypopulationgt10000000
  • 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

9
/countries/countrypopulationgt10000000
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
10
//country_at_continent"Asia"/city
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
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 context node that is
    defined by the application that uses the XPath
  • For example, city/name, or ./name

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

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

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

15
Child-Axis Examples
/countries
16
Child-Axis Examples
/countries/country/city
17
Child-Axis Examples
city/name
Context
18
Child-Axis Examples
//country/
An attribute is not an element child!
19
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-selfnode(),
  • // is a shotrcut notation for /descendant-or-self
    node()/
  • For example, country//name

20
Descendant Examples
/countries//name
21
Descendant Examples
.//
Context
22
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

23
Referring Attributes
  • The attribute axis is denoted _at_attName
  • That is, go to the attribute attName of the
    current node
  • The operator _at_ matches every attribute

24
Attribute Examples
//country/_at_continent
25
Attribute Examples
_at_continent
Context
26
Attribute Examples
//_at_
27
XPath Predicates
  • Predicates in XPath are used for filtering out
    steps
  • For example, //city_at_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

28
//country./populationgt10000000
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
  • 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 //countrypopulationgt10000000

29
//country.//city
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
An XPath evaluates to true if and only if its
result is not empty
30
//country//city
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
Why?
31
//countrypopulation.gt3000000 and _at_yeargt2003
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
32
//countryname"Israel" or name"Spain"/populatio
n
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
33
//country/city2
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
  • 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)

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

35
//country/citylast()
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
36
//cityposition()lt2
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
37
//name()"city" or name()"country"
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
38
//starts-with(name(),c")
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
39
//countrycount(./city)gt1
world.xml
lt?xml version"1.0"?gt lt!DOCTYPE countries SYSTEM
"world.dtd"gt ltcountriesgt ltcountry
continent"as"gt ltnamegtIsraellt/namegt
ltpopulation year"2001"gt6199008lt/populationgt
ltcity capital"yes"gtltnamegtJerusalemlt/namegtlt/citygt
ltcitygtltnamegtAshdodlt/namegtlt/citygt
lt/countrygt ltcountry continent"eu"gt
ltnamegtFrancelt/namegt ltpopulation
year"2004"gt60424213lt/populationgt
lt/countrygt lt/countriesgt
40
Final Remarks
  • The syntax of XPath that was presented here is
    the abbreviated syntax
  • For example, city/../_at_name is an abbrv. of
  • childcity/parentnode() /attributename
  • More details on XPath
  • XPath tutorial in W3Schools
  • XPath W3C Recommendation

41
XSL Transformations (XSLT)
  • An Example
  • Useful Links in the DBI site

42
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

43
XSLT Processors
XSLT Processor
44
Web Pages The Whole Picture
Web Page
Layout
Data
Doc. Structure
Knowledge
XSL XHTML
Style
CSS
XML
45
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltcatal
oggt ltcd country"UK"gt lttitlegtDark
Side of the Moonlt/titlegt ltartistgtPink
Floydlt/artistgt ltpricegt10.90lt/pricegt
lt/cdgt ltcd country"UK"gt
lttitlegtSpace Odditylt/titlegt
ltartistgtDavid Bowielt/artistgt
ltpricegt9.90lt/pricegt lt/cdgt ltcd
country"USA"gt lttitlegtAretha Lady
Soullt/titlegt ltartistgtAretha
Franklinlt/artistgt ltpricegt9.90lt/pricegt
lt/cdgt lt/cataloggt
catalog.xml
46
Valid XML!
Commands are XML elements with the namespace xsl
Includes XHTML elements
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltheadgtlttitlegtcd
cataloglt/titlegtlt/headgt ltbodygtlth1gtThis is a
cd catalog!lt/h1gtlt/bodygt lt/htmlgt
lt/xsltemplategt lt/xslstylesheetgt
catalog.xsl
47
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, not the
    original XML

48
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
49
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 /)

50
Templates
  • A template has the form
  • ltxsltemplate match"pattern"gt
  • ...
  • lt/xsltemplategt
  • The content of a template consists of
  • XML elements (e.g., XHTML) and text that are
    copied to the result
  • XSL elements (ltxslgt) that are actually
    instructions
  • The syntax for the pattern is a subset of XPath

51
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltheadgtlttitlegtcd
cataloglt/titlegtlt/headgt ltbodygtlth1gtThis is a
cd catalog!lt/h1gtlt/bodygt lt/htmlgt
lt/xsltemplategt lt/xslstylesheetgt
catalog1.xsl
52
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt?xml-
stylesheet type"text/xsl"
href"catalog1.xsl"?gt ltcataloggt ltcd
country"UK"gt lttitlegtDark Side of the
Moonlt/titlegt ltartistgtPink Floydlt/artistgt
ltpricegt10.90lt/pricegt lt/cdgt ltcd
country"UK"gt lttitlegtSpace
Odditylt/titlegt ltartistgtDavid
Bowielt/artistgt ltpricegt9.90lt/pricegt
lt/cdgt ltcd country"USA"gt
lttitlegtAretha Lady Soullt/titlegt
ltartistgtAretha Franklinlt/artistgt
ltpricegt9.90lt/pricegt lt/cdgt lt/cataloggt
catalog1.xml
53
The Result
lthtmlgt ltheadgt ltMETA http-equiv"Content-Type
" content"text/html charsetUTF-8"gt
lttitlegtcd cataloglt/titlegt lt/headgt ltbodygt
lth1gtThis is a cd catalog!lt/h1gt lt/bodygt lt/htmlgt
Implicitly added to ltheadgt
54
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_at_country'UK'/artist"

55
ltxslapply-templatesgt
  • 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
  • ltxslapply-templates select"xpath"/gt
  • 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)

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

ltxsltemplate match"/"gt ltxslapply-templates/gt
lt/xsltemplategt ltxsltemplate
match"text()_at_"gt ltxslvalue-of
select"."/gt lt/xsltemplategt
57
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt lt/xslstylesheetgt
Dark Side of the Moon Pink Floyd 10.90 Space
Oddity David Bowie 9.90
58
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"cd"gt lth2gtA cd!lt/h2gt
lt/xsltemplategt lt/xslstylesheetgt
lth2gtA cd!lt/h2gt lth2gtA cd!lt/h2gt lth2gtA cd!lt/h2gt
59
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"cd_at_country'UK'"gt lth2gtA
cd!lt/h2gtlt/xsltemplategt lt/xslstylesheetgt
lth2gtA cd!lt/h2gt lth2gtA cd!lt/h2gt Aretha Lady Soul
Aretha Franklin 9.90
60
ltxslstylesheet version"1.0" xmlnsxsl"http//w
ww.w3.org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt ltxslapply-templates
select"catalog/cd_at_country'UK'/artist"/gt
lt/xsltemplategt ltxsltemplate match"artist"gt
lth2gtAn artist!lt/h2gt lt/xsltemplategt lt/xsl
stylesheetgt
lth2gtAn artist!lt/h2gt lth2gtAn artist!lt/h2gt
61
ltxslstylesheet version"1.0" xmlnsxsl"http//w
ww.w3.org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt ltxslapply-templates
select"cd_at_country'UK'/artist"/gt
lt/xsltemplategt ltxsltemplate match"artist"gt
lth2gtAn artist!lt/h2gt lt/xsltemplategt lt/xsl
stylesheetgt
62
Frequently Used Elements of XSL
  • ltxslvalue-of select"xpath"/gt
  • This element extracts the value of a node from
    the nodelist located by xpath
  • ltxslfor-each select"xpath"/gt
  • This element loops over all the nodes in the node
    list located by xpath
  • ltxslif test"cond"/gt,
  • ltxslif test"xpath"/gt, etc.
  • This element is for conditional processing

63
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt lthtmlgtltheadgtlttitlegtcd
cataloglt/titlegtlt/headgt ltbodygt lth1gtCD
cataloglt/h1gt ltulgt ltxslfor-each
select"catalog/cd"gt ltligtltxslvalue-of
select"title"/gt ltxslvalue-of
select"artist"/gtlt/ligt lt/xslfor-eachgt
lt/ulgtlt/bodygtlt/htmlgt lt/xsltemplategt lt/xslstyles
heetgt
Example 1
Currently selected element is the context node
catalog2.xsl
64
Example 2
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt lthtmlgtltheadgtlttitlegtcd
cataloglt/titlegtlt/headgt ltbodygt lth1gtCD
cataloglt/h1gt ltulgt ltxslfor-each
select"catalog/cd"gt ltligtltxslapply-templates
select"."/gtlt/ligt lt/xslfor-eachgt
lt/ulgtlt/bodygtlt/htmlgt lt/xsltemplategt
catalog3.xsl
65
Example 2 (cont.)
Entities replace characters pricegt10 ?
pricelt10
ltxsltemplate match"cd"gt ltbgtltxslvalue-of
select"artist"/gtlt/bgt ltxslvalue-of
select"title"/gt ltxslif test"pricelt10"gt
(ltemgtnow on sale ltxslvalue-of
select"price"/gt lt/emgt) lt/xslifgt
lt/xsltemplategt lt/xslstylesheetgt
catalog3.xsl
66
ltxslchoosegt Switch syntax for conditions
Example 3
ltxslchoosegt ltxslwhen test"price lt 9"gt
ltemgtSpecial price!lt/emgt lt/xslwhengt ltxslwhen
test"pricegt9 and pricelt10"gt ltigtGood
price!lt/igt lt/xslwhengt ltxslotherwisegt
(Normal price.) lt/xslotherwisegt lt/xslchoosegt
67
The ltxslsortgt Element
  • The ltxslsortgt element is used to sort the list
    of nodes that are looped over by the
    ltxslfor-eachgt element
  • Thus, the ltxslsortgt must appear inside the
    ltxslfor-eachgt element
  • The looping is done in sorted order

68
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt lthtmlgtltheadgtlttitlegtcd
cataloglt/titlegtlt/headgt ltbodygt lth1gtCD
cataloglt/h1gt ltulgt ltxslfor-each
select"catalog/cd"gt ltxslsort select"title"/gt
ltligtltxslvalue-of select"title"/gtlt/ligt
lt/xslfor-eachgt lt/ulgtlt/bodygtlt/htmlgt
lt/xsltemplategt lt/xslstylesheetgt
CDs are iterated in ascending order of the titles
catalog4.xsl
69
Setting Values in Attributes
  • The ltxslvalue-ofgt 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 ltxslelementgt in order
    to construct XML elements

70
An Example
  • In the following example, we add to each CD
    entitled t a link to the URL /showcd.php?titlet

ltxsltemplate match"cd"gt ltbgtltxslvalue-of
select"artist"/gtlt/bgt lta href"/showcd.php?tit
le./title"gt ltxslvalue-of
select"title"/gt lt/agt lt/xsltemplategt
71
Using ltxslelementgt
ltxsltemplate match"cd"gt ltbgtltxslvalue-of
select"artist"/gtlt/bgt ltxslelement name"a"gt
ltxslattribute name"href"gt
showcd/?titleltxslvalue-of select"title"/gt
lt/xslattributegt ltxslvalue-of
select"title"/gt lt/xslelementgt
lt/xsltemplategt
72
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

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

74
lt?xml version"1.0"?gt ltxslstylesheet
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
version"1.0"gt ltxsloutput
method"xml"/gt ltxsltemplate match""gt
ltxslelement name"name()"gt
ltxslfor-each select"_at_"gt
ltxslattribute name"name()"gt
ltxslvalue-of select"."/gt
lt/xslattributegt lt/xslfor-eachgt
ltxslapply-templates/gt
lt/xslelementgt lt/xsltemplategt lt/xslstyleshe
etgt
Identity Transformation Stylesheet
75
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

76
The Original XSL Example
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltheadgtlttitlegtcd
cataloglt/titlegtlt/headgt ltbodygtlth1gtThis is a
cd catalog!lt/h1gtlt/bodygt lt/htmlgt
lt/xsltemplategt lt/xslstylesheetgt
77
The Original Transformation Result
lthtmlgt ltheadgt ltMETA http-equiv"Content-Type
" content"text/html charsetUTF-8"gt
lttitlegtcd cataloglt/titlegt lt/headgt ltbodygt
lth1gtThis is a cd catalog!lt/h1gt lt/bodygt lt/htmlgt
No DOCTYPE
Uppercase tag name, unclosed element
78
Modifying the XSL Example
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.
org/1999/XSL/Transform" xmlns"http//www.w3.org/
1999/xhtml"gt ltxsloutput method"html"
doctype-public"-//W3C//DTD XHTML 1.0
Strict//EN" doctype-system
"http//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
"/gt ltxsltemplate match"/"gt
79
The Transformation Result
lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http//www.w3.org/TR/xhtml1/DTD/xhtml
1-strict.dtd"gt lthtml xmlns"http//www.w3.org/1999
/xhtml"gt ltheadgt lttitlegtcd cataloglt/titlegt
lt/headgt ltbodygt lth1gtThis is a cd catalog!lt/h1gt
lt/bodygt lt/htmlgt
META is not inserted
80
Some Other XSL Elements
  • The ltxsltextgt element inserts free text in the
    output
  • The ltxslcopy-of select"xpath"gt creates a copy
    of the specified nodes
  • The ltxslcommentgt element creates a comment node
    in the result tree
  • The ltxslvariablegt element defines a variable
    (local or global) that can be used within the
    program

81
Costellos 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 Costellos
    tutorial)

82
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
Write a Comment
User Comments (0)
About PowerShow.com