XSLT - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

XSLT

Description:

XSLT eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv XSL XSL = eXtensible Stylesheet Language XSL consists of XPath (navigation in ... – PowerPoint PPT presentation

Number of Views:198
Avg rating:3.0/5.0
Slides: 57
Provided by: mscsMuEd9
Category:
Tags: xslt | aretha | franklin

less

Transcript and Presenter's Notes

Title: XSLT


1
XSLT eXtensible Stylesheet Language
Transformations
Modified Slides from Dr. Sagiv
2
XSL
  • XSL eXtensible Stylesheet Language
  • XSL consists of
  • XPath (navigation in documents)
  • XSLT (T for transformations)
  • 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
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
An XML document
4
XSLT
  • Transforming XML documents into other XML
    documents

5
XSLT Stylesheet
  • An XSLT stylesheet is a program that transforms
    an XML document into another XML document
  • For example
  • Transforming XML to XHTML (HTML that conforms to
    XML syntax)
  • Transforming an XML document to WML (a format of
    XML that cellular phones can display)

6
A Few Things About XSL
  • XSL is a high-level, functional language
  • An XSL style sheet is a valid XML document
  • Valid with respect to the XSL namespace
  • Therefore, commands in XSL are XSL elements

7
Applying XSLT Stylesheets toXML Documents
  • There are three ways of applying an XSLT
    stylesheet to an XML document
  • Directly applying an XSLT processor to the XML
    document and the XSLT stylesheet
  • Calling an XSLT processor from within a (Java)
    program
  • Adding to the XML document a link to the XSL
    stylesheet and letting the browser do the
    transformation

8
Using an XSL Processor
XSL stylesheet
XML document
Result is either an XML, HTML or text document
XSL Processor
9
Letting a Browser Perform the Transformation
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt?xml-
stylesheet type"text/xsl" hrefcatalog.xsl"?gt
ltcataloggt ltcd country"UK"gt
lttitlegtDark Side of the Moonlt/titlegt
ltartistgtPink Floydlt/artistgt
ltpricegt10.90lt/pricegt lt/cdgt lt/cataloggt
10
The Root of the XSL Document
  • The Root of the XSL document should be one of the
    following lines

ltxslstylesheet version"1.0" xmlnsxsl"http//w
ww.w3.org/1999/XSL/Transform"gt
ltxsltransform version"1.0" xmlnsxsl"http//ww
w.w3.org/1999/XSL/Transform"gt
The namespace allows the XSL processor to
distinguish between XSL tags and tags of the
result document
11
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 document)
  • Each template has a match attribute that
    specifies to which source nodes the template can
    be applied
  • The current source node is processed by applying
    a template that matches this node
  • Processing always starts at the root (/)

12
Templates
  • A template has the form
  • ltxsltemplate match"pattern"gt
  • ...
  • lt/xsltemplategt
  • The content of a template consists of
  • XML elements and text that are copied to the
    result
  • XSL elements that are actually instructions
  • The pattern syntax is a subset of XPath

13
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth1gtHello
Worldlt/h1gt lt/bodygt lt/htmlgt lt/xsltemplategt lt/xs
lstylesheetgt
14
lthtmlgt ltbodygt lth1gtHello Worldlt/h1gt lt/bodygt lt/htmlgt
Applying a browser to catalog.xml (catalog.xml
has a link to catalog.xsl)
15
The Elementltxslapply-templatesgt
  • Processing starts by applying a template that
    matches the root (/)
  • If the given XSL stylesheet does not have a
    template that matches the root, then one is
    inserted by default (see the slide on Default
    Templates)
  • The XSL stylesheet must specify explicitly
    whether templates should be applied to
    descendants of the root
  • It is done by putting inside a template the
    instruction
  • ltxslapply-templates select"xpath"/gt
  • Without the select attribute, this instruction
    processes all the children of the current node

16
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 ltbodygt ltxslapply-templates
select"catalog/cd"/gt lt/bodygt
lt/htmlgt lt/xsltemplategt ltxsltemplate
match"cd"gt lth2gtA CD!lt/h2gt lt/xsltemplategt lt/
xslstylesheetgt
lthtmlgt ltbodygt lth2gtA CD!lt/h2gt lth2gtA CD!lt/h2gt
lth2gtA CD!lt/h2gt lt/bodygt lt/htmlgt
17
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
matchtext()gt ltxslvalue-of select./gt lt/xsl
templategt
18
The Most Frequently Used Elements of XSL
  • ltxslvalue-of selectxpath-expression/gt
  • This element extracts the value of a node from
    the nodelist located by xpath-expression
  • ltxslfor-each selectxpath-expression/gt
  • This element loops over all the nodes in the
    nodelist located by xpath-expression
  • ltxslif testxpath-expression/gt,
    ltxslif testxpath-expressionvalue/gt, etc.
  • This element is for conditional processing

19
The ltxslvalue-ofgt Element
ltxslvalue-of selectxpath-expression/gt
  • The XSL element ltxslvalue-ofgt can be used to
    extract the value of an element that is selected
    from the source XML document
  • The extracted value is added to the output stream
  • The selected element is located by an XPath
    expression that appears as the value of the
    select attribute

20
Selected values
21
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth2gtA CD
Cataloglt/h2gt lttable border"1"gt lttr
bgcoloryellow"gt ltthgtTitlelt/thgt
ltthgtArtistlt/thgt lt/trgt
22
lttrgt lttdgtltxslvalue-of select"catalog/cd
/title"/gt lt/tdgt lttdgtltxslvalue-of
select"catalog/cd/artist"/gt lt/tdgt
lt/trgt lt/tablegt lt/bodygt lt/htmlgt lt/xsltemplate
gt lt/xslstylesheetgt
Note that only the first matched element is
retrieved for each ltxslvalue ofgt
23
The ltxslfor-eachgt Element
ltxslfor-each selectxpath-expression/gt
  • The ltxslfor-eachgt element loops over all the
    nodes in the nodelist of the XPath expression
    that appears as the value of the select attribute
  • The value of each node can be extracted by an
    ltxslvalue-ofgt element

24
All the values are selected
25
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth2gtA CD
Cataloglt/h2gt lttable border"1"gt lttr
bgcoloryellow"gt ltthgtTitlelt/thgt
ltthgtArtistlt/thgt lt/trgt
As in the previous example
26
ltxslfor-each select"catalog/cd"gt lttrgt
lttdgtltxslvalue-of select"title"/gt
lt/tdgt lttdgtltxslvalue-of
select"artist"/gt lt/tdgt lt/trgt
lt/xslfor-eachgt lt/tablegt lt/bodygt
lt/htmlgt lt/xsltemplategt lt/xslstylesheetgt
Note that all the /catalog/cd elements are
retrieved
27
Consider the following change in the select
attribute
ltxslfor-each select"catalog/cdpricelt10
"gt lttrgt lttdgtltxslvalue-of
select"title"/gt lt/tdgt
lttdgtltxslvalue-of select"artist"/gt lt/tdgt
lt/trgt lt/xslfor-eachgt lt/tablegt
lt/bodygt lt/htmlgt lt/xsltemplategt lt/xslstylesheet
gt
Only elements that satisfy /catalog/cdpricelt10
are retrieved
28
(No Transcript)
29
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

30
Sorted by the name of the artist
31
ltxslfor-each select"catalog/cd"gt ltxslsort
select"artist"/gt lttrgt
lttdgtltxslvalue-of select"title"/gt lt/tdgt
lttdgtltxslvalue-of select"artist"/gt lt/tdgt
lt/trgt lt/xslfor-eachgt lt/tablegt
lt/bodygt lt/htmlgt lt/xsltemplategt lt/xslstylesheet
gt
The /catalog/cd elements are sorted according to
the value of the artist element
32
The ltxslifgt Element
  • The ltxslifgt element is used for conditional
    processing
  • The condition appears as the value of the test
    attribute, for example
  • ltxslif test"price gt 10"gt  some output
    ...lt/xslifgt
  • The elements inside the ltxslifgt element are
    processed if the condition is true

33
Note
  • Processing the inside elements means
  • Copying them into the output stream if they are
    not XSL elements, and
  • Evaluating them if they are XSL elements
  • If the value of the test attribute is just an
    XPath expression (i.e., without any comparison),
    then the test is satisfied if the nodelist of
    this XPath expression is not empty

34
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth2gtA CD
Cataloglt/h2gt lttable border"1"gt lttr
bgcoloryellow"gt ltthgtTitlelt/thgt
ltthgtArtistlt/thgt lt/trgt
As in the previous examples
35
ltxslfor-each select"catalog/cd"gt ltxslif
test"price gt 10"gt lttrgt
lttdgtltxslvalue-of select"title"/gt lt/tdgt
lttdgtltxslvalue-of select"artist"/gt lt/tdgt
lt/trgt lt/xslifgt lt/xslfor-eachgt
lt/tablegt lt/bodygt lt/htmlgt lt/xsltemplategt lt/xs
lstylesheetgt
Only /catalog/cd with pricegt10 are retrieved
36
(No Transcript)
37
The ltxslchoosegt Element
  • The ltxslchoosegt element is used in conjunction
    with ltxslwhengt and ltxslotherwisegt to express
    test with multiple conditions
  • There can be many ltxslwhengt inside an
    ltxslchoosegt element, but there should be a
    single ltxslotherwisegt inside an ltxslchoosegt
    element

38
Using ltxslchoosegt
  • To insert a conditional choose against the
    content of the XML file, simply add the
    ltxslchoosegt, ltxslwhengt, and ltxslotherwisegt
    elements to your XSL document like this
  • ltxslchoosegt   ltxslwhen test"price gt
    10"gt      ... some code ...   lt/xslwhengt  
    ltxslotherwisegt      ... some code ....  
    lt/xslotherwisegtlt/xslchoosegt

39
ltxslfor-each select"catalog/cd"gtlttrgt
lttdgtltxslvalue-of select"title"/gtlt/tdgt
ltxslchoosegt ltxslwhen test"price gt 10"gt
lttd bgcolor"red"gt ltxslvalue-of
select"artist"/gtlt/tdgt lt/xslwhengt
ltxslwhen test"pricegt9 and pricelt10"gt
lttd bgcolor"gray"gt
ltxslvalue-of select"artist"/gtlt/tdgt
lt/xslwhengt ltxslotherwisegt
lttdgtltxslvalue-of select"artist"/gtlt/tdgt
lt/xslotherwisegt lt/xslchoosegtlt/trgt lt/xslfor-ea
chgt
40
(No Transcript)
41
Applying Templates Recursively
  • The following example shows how to apply
    templates recursively
  • Generally, it is possible (but not in this
    example) that more than one template matches the
    current source node
  • The specification (www.w3.org/TR/xslt) describes
    (Section 5.5) which template should be chosen for
    application

42
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsls
tylesheet version"1.0" xmlnsxsl"http//www.w3.o
rg/1999/XSL/ Transform"gt ltxsltemplate
match"/"gt lthtmlgt ltbodygt lth2gtA CD
Cataloglt/h2gt ltxslapply-templates/gt
lt/bodygt lt/htmlgt lt/xsltemplategt
43
ltxsltemplate match"cd"gt ltpgt
ltxslapply-templates select"title"/gt
ltxslapply-templates select"artist"/gt
lt/pgt lt/xsltemplategt ltxsltemplate
match"title"gt Title ltspan style"colorred"gt
ltxslvalue-of select"."/gtlt/spangt ltbr
/gt lt/xsltemplategt
44
ltxsltemplate match"artist"gt Artist ltspan
style"colorgreen"gt ltxslvalue-of
select"."/gtlt/spangt ltbr /gt lt/xsltemplategt lt/xs
lstylesheetgt
45
(No Transcript)
46
Is Recursive Application of Templates Really
Needed?
  • The output of the previous example can also be
    generated by an XSL stylesheet that uses only one
    template that matches the root (and does not use
    the element ltxslapply-templatesgt)
  • However, some tasks can only be done by applying
    templates recursively
  • This typically happens when the structure of the
    source XML document is not known

47
For example
  • Suppose that we want to write an XSL stylesheet
    that generates an exact copy of the source XML
    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)

48
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
49
The ltxsloutputgt Element

lt?xml version"1.0" encoding"ISO-8859-1"?gt
ltxslstylesheet version"1.0" xmlnsxsl"http//
www.w3.org/1999/XSL/ Transform"gt ltxsloutput
method"xml" version"1.0" encoding"iso-8859-1"
indent"yes"/gt ...... lt/xslstylesheetgt
Tells in what format the output should be
xml/html/text
50
Some Other XSL Elements
  • The ltxsltextgt element allows to insert free text
    in the output
  • The ltxslcopy-ofgt element creates a copy of the
    current node
  • The ltxslcommentgt element is used to create a
    comment node in the result tree
  • There are more elements and functions look in
    the specification! (www.w3.org/TR/xslt)

51
ltxsltextgt
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltxslstylesheet version"1.0" xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • ltxsltemplate match"/"gt
  • lthtmlgt
  • ltbodygt
  • lth2gtMy CD Collectionlt/h2gt
  • ltpgtTitles
  • ltxslfor-each select"catalog/cd"gt
  • ltxslvalue-of select"title"/gt
  • ltxslif test"position() lt last()-1"gt
  • ltxsltextgt, lt/xsltextgt
  • lt/xslifgt
  • ltxslif test"position()last()-1"gt
  • ltxsltextgt, and lt/xsltextgt
  • lt/xslifgt

52
ltxsltextgt (contd)
  • ltxslif test"position()last()"gt
  • ltxsltextgt!lt/xsltextgt
  • lt/xslifgt
  • lt/xslfor-eachgt
  • lt/pgt
  • lt/bodygt
  • lt/htmlgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

53
ltxslcopy-ofgt
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltxslstylesheet version"1.0" xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • ltxslvariable name"header"gt
  • lttr bgcolor"9acd32"gt
  • ltth align"left"gtTitlelt/thgt
  • ltth align"left"gtArtistlt/thgt
  • lt/trgt
  • lt/xslvariablegt
  • ltxsltemplate match"/"gt
  • lthtmlgt
  • ltbodygt
  • lth2gtMy CD Collectionlt/h2gt
  • lttable border"1"gt
  • ltxslcopy-of select"header"/gt
  • ltxslfor-each select"catalog/cd"gt

54
ltxslcopy-ofgt (contd)
  • lttrgt
  • lttdgt
  • ltxslvalue-of select"title"/gt
  • lt/tdgt
  • lttdgt
  • ltxslvalue-of select"artist"/gt
  • lt/tdgt
  • lt/trgt
  • lt/xslfor-eachgt
  • lt/tablegt
  • lt/bodygt
  • lt/htmlgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

55
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

56
Summary
  • XSLT is a high-level transformation language
  • Create core output once in XML format (using
    Servlets, JSP, etc.)
  • Use XSLT to transform the core output as needed
Write a Comment
User Comments (0)
About PowerShow.com