XSLT Programming - PowerPoint PPT Presentation

1 / 53
About This Presentation
Title:

XSLT Programming

Description:

repeat: run the transformation 20 times --debug: dump the tree of the result instead ... a hole to wait for the stuff you put in between them. ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 54
Provided by: peopleCs80
Category:

less

Transcript and Presenter's Notes

Title: XSLT Programming


1
XSLT Programming
2
Using XSL

XML Servers
Client Side
IE Mozilla
Web Server
CGI
XSLT-enabled Browser
http request
XML Stylesheet
The Internet
XML response
Server Side
XSLT-engine
HTML
http request
Web Server
Servlet
The Internet
HTML
XML response
3
XSLT
  • XSLT
  • Extensible Stylesheet Language Transformations
  • Turing-complete, declarative programming language
  • Syntax is XML
  • XSLT1.0 accepted in 1999 as W3C standard
  • XSLT2.0 just reached recommendation level in
    January 2007

4
Dont underestimate XSLT
  • XSLT is particularly good (designed for)
    extracting info from XML documents and outputting
    some other text-based format -- text, xhtml,
    html, xml, etc.
  • XSLT is not simply a stylesheet to present XML,
    though it is frequently used that way
  • For publishing applications, often the entire
    application can be performed in XSLT.
  • Add prefix or suffix text to content
  • Remove, create, re-order and sort elements
  • Re-use elements elsewhere in the document
  • Transform data between different XML dialects
  • XSLT can be used like a stylesheet or embedded in
    a programming language

5
Running Ex. BridgeOfDeathEpisode.xml
lt?xml version"1.0" encoding"UTF-8"?gt ltepisodegt lt
dialog speaker"Bridgekeeper"gtStop! Who would
cross the Bridge of Death must answer me
these questions three, ere the other side he
see.lt/dialoggt ltdialog speaker"Sir Launselot"gtAsk
me the questions, bridgekeeper. I am not
afraid.lt/dialoggt ltdialog speaker"Bridgekeeper"gtWh
at... is your name?lt/dialoggt ltdialog speaker"Sir
Launselot"gtMy name is 'Sir Launcelot of
Camelot'.lt/dialoggt ltdialog speaker"Bridgekeeper"gt
What... is your quest?lt/dialoggt ltdialog
speaker"Sir Launselot"gtTo seek the Holy
Grail.lt/dialoggt ltdialog speaker"Bridgekeeper"gtWha
t... is your favourite colour?lt/dialoggt ltdialog
speaker"Sir Launselot"gtBluelt/dialoggt ltdialog
speaker"Bridgekeeper"gtRight. Off you
go.lt/dialoggt ltdialog speaker"Sir Launselot"gtOh,
thank you. Thank you very much.lt/dialoggt ltdialog
speaker"Sir Robin"gtThat's easy!lt/dialoggt ltdialog
speaker"Bridgekeeper"gtStop! Who approacheth the
Bridge of Death must answer me these
questions three, ere the other side he
see.lt/dialoggt ltdialog speaker"Sir Robin"gtAsk me
the questions, bridgekeeper. I'm not
afraid.lt/dialoggt ltdialog speaker"Bridgekeeper"gtWh
at... is your name?lt/dialoggt
6
Running Ex. BridgeOfDeathEpisode.xml
ltdialog speaker"Sir Robin"gtTo seek the Holy
Grail.lt/dialoggt ltdialog speaker"Bridgekeeper"gtWha
t... is the capital of Assyria?lt/dialoggt ltdialog
speaker"Sir Robin"gtI don't know that!
Auuuuuuuugh!lt/dialoggt ltactiongtRobin is lifted by
a giant unseen hand and thrown into the
gorgelt/actiongt ltdialog speaker"Bridgekeeper"gtStop
! What... is your name?lt/dialoggt ltdialog
speaker"Sir Galahad"gtSir Galahad of
Camelotlt/dialoggt ltdialog speaker"Bridgekeeper"gtWh
at... is your quest?lt/dialoggt ltdialog
speaker"Sir Galahad"gtI seek the
Grail.lt/dialoggt ltdialog speaker"Bridgekeeper"gtWha
t... is your favourite colour?lt/dialoggt ltdialog
speaker"Sir Galahad"gtBlue. No, yel--
auuuuuuuugh!lt/dialoggt ltactiongtGalahad is lifted
by a giant unseen hand and thrown into the
gorgelt/actiongt ltdialog speaker"Bridgekeeper"gtHee
hee heh. Stop! What... is your name?lt/dialoggt ltdia
log speaker"King Arthur"gtIt is 'Arthur', King of
the Britons.lt/dialoggt ltdialog speaker"Bridgekeepe
r"gtWhat... is your quest?lt/dialoggt ltdialog
speaker"King Arthur"gtTo seek the Holy
Grail.lt/dialoggt ltdialog speaker"Bridgekeeper"gtWha
t... is the air-speed velocity of an unladen
swallow?lt/dialoggt ltdialog speaker"King
Arthur"gtWhat do you mean? An African or
European swallow?lt/dialoggt
7
Executing the stylsheet foo.xslOn file
BridgeOfDeathEpisode.xml
  • foo.xsl
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999
    /XSL/Transform" version"1.0"gt
  • ltxsltemplate match/gt ltHTMLgtlttitlegtHWlt/title
    gtltBODYgtHELLOWORLDlt/BODYgtlt/HTMLgtlt/xsltemplategt
  • lt/xslstylesheetgt
  • BridgeOfDeath.html lthtmlgtlttitlegthwlt/titlegtltBODYgt
    HELLO WORLD lt/BODYgt lt/htmlgt

Stylesheets consist of template rules that say
when you match this, output this. Here match
/ means the root of the xml document
8
Running XSLT
  • To see the output in an XSL ready browser
  • Add a processing instruction to the XML with a
    reference to the stylesheet (xsl or css --- basic
    syntax is the same).
  • BridgeOfDeath.xml
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • lt?xml-stylesheet hreffoo.xsl typetext/xsl?gt
  • ltepisodegt
  • ltdialog speaker"Bridgekeeper"gtStop! Who would
    cross the Bridge of Death must answer me these
    questions three, ere the other side he
    see.lt/dialoggt
  • lt/episodegt

9
Running XSLT
  • Can also run them from the command line on any
    Linux-based system
  • Usage xsltproc options stylesheet file file
    ...
  • Options
  • --version or -V show the version of
    libxml and libxslt used
  • --verbose or -v show logs of what's
    happening
  • --output file or -o file save to a given
    file
  • --timing display the time used
  • --repeat run the transformation 20 times
  • --debug dump the tree of the result
    instead
  • --dumpextensions dump the registered
    extension elements and functions to stdout
  • --novalid skip the Dtd loading phase
  • --nodtdattr do not default attributes
    from the DTD
  • --noout do not dump the result
  • --maxdepth val increase the maximum
    depth
  • --maxparserdepth val increase the
    maximum parser depth
  • --html the input document is(are) an
    HTML file(s)
  • --param name value pass a
    (parameter,value) pair
  • value is an UTF8 XPath expression.
  • string values must be quoted like
    "'string'"

10
XSL apply-templates
  • ltxslapply-templates /gt
  • continue processing my children by applying
    templates to them.

To understand this, imagine the xslt processor
traversing the XML document depth-first and
outputting the node info it encounters until it
reaches a node that matches a template
(rule) that specifies it do otherwise. When a
rule is reached for a particular node in the xml
file, whatever is specified in the rule is
carried out, and then the transformer would go on
to the sibling node unless there is an embedded
ltxslapply-templatesgt command, which would then
indicate that the children of the current nodes
should be processed.
11
xsltemplate
  • The template is the basis of the rule-based
    architecture that XSLT uses.  
  • The template is processed either by its match or
    name attributes.  
  • The match attribute uses a pattern (Xpath
    expression)
  • Used with ltapply-templates match gt
  • The name is whatever name you give it.
  • Used with ltcall-template name gt
  • Another important attribute of the template is
    the mode attribute.  If you want different
    formatting from the general template that you
    have defined, you can define different modes for
    that node, which will ignore the general match
    instruction.
  • Note You need to have at least one template in
    your XSLT file.

12
Built-in rules
  • There are two built-in template rules -- ie rules
    that are applied even when no matching template
    is encountered
  • This traverses your descendants
  • ltxsltemplate match"/"gt
  • ltxslapply-templates/gt
  • lt/xsltemplategt?
  • This copies text to the output?
  • ltxsltemplate match"text()_at_"gt
  • ltxslvalue-of select"."/gt
  • lt/xsltemplategt
  • Note Attribute text is only copied if you apply
    templates to an attribute.
  • ?These get dropped from the output
  • ltxsltemplate match"processing-instruction()co
    mment()"/gt
  • You get these for free and the processor will do
    them in an "optimal" way.

13
XSLapply-templates
  • The ltapply-templatesgt instruction is always found
    inside a template body.  
  • It defines a set of nodes to be processed.  In
    this process, it then may find any 'sub' template
    rules to process (child elements of element in
    context).
  • If you want the apply-template instruction to
    only process certain child elements, you can
    define a select attribute, to only process
    specific nodes.  In the following example, we
    only want to process the 'name' and 'address'
    elements in the root document element
  • ltxsltemplate match"/"gt    ltxslapply-templates
    select"person/name person/address"/gtlt/xsltempl
    ategt

Apply templates to all name nodes that
are children of all person nodes that are
children cf the context node OR
14
  • lt?xml version"1.0"?gt
  • ltxslstylesheet
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
  • version"1.0"gt
  • ltxsltemplate match/gt
  • ltHTMLgtltBODYgt ltxslapply-templates /gt
    lt/BODYgtlt/HTMLgt
  • lt/xsltemplategt
  • ltxsltemplate matchepisodegt
  • ltPgtltIgtI have hit the top-level episode
    elementlt/Igtlt/Pgt
  • ltxslapply-templates /gt
  • lt/xsltemplategt
  • ltxsltemplate matchdialoggt
  • ltPgtI have hit some dialoglt/Pgt
  • lt/xsltemplategt
  • ltxsltemplate matchactiongt
  • ltH2gt I have hit some action lt/H2gt

At the beginning output these tags and leave . .
a hole to wait for the stuff you put in between
them. Fill that hole by matching further
templates against subelements of the root.
All xsl commands start with xsl
An XSL stylesheet has to be well-formed XML
15
XSLvalue-of
  • ltxslvalue-of selectnodeinfo/gt output a string
    that is derived from the node represented by
    nodeinfo.
  • ltxslvalue-of select.gt output the value of
    this node for an element, this will be cdata
    inside of it.
  • ltxslvalue-of select_at_speakergt output the
    value of the speaker attribute.

16
XSLvalue-of
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform" version"1.0"gt
  • ltxsltemplate match"/"gt
  • ltHTMLgtltBODYgt ltxslapply-templates /gt
    lt/BODYgtlt/HTMLgt
  • lt/xsltemplategt
  • ltxsltemplate match"dialog"gt
  • ltPgt
  • ltxslvalue-of select"_at_speaker /gt
  • ltIgt ltxslvalue-of select". /gt lt/Igt
  • lt/Pgt
  • lt/xsltemplategt
  • ltxsltemplate match"action"gt
  • ltPgt ltBgt ltxslvalue-of select"."/gt lt/Bgt lt/Pgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

17
Algorithm to Write XSLT
  • Match against a pattern.
  • Generate a part of the result tree.
  • The partial result tree has some holes where
    you have continue processing
  • e.g. where apply-templates sit.
  • Continue until all holes are filled.

18
XSL Document Structure
lt?xml version1.0?gt ltxslstylesheetgt
ltxsltemplate match/gt
action lt/xsltemplategt
ltxsltemplate matchDialoggt
action lt/xsltemplategt
ltxsltemplate matchActiongt
action lt/xsltemplategt
... lt/xslstylesheetgt
19
Select Attributes and Xpath
  • The apply-templates tag takes a select attribute,
    as do several other tags (e.g. for-each,
    variable, ).
  • Every select attribute takes as a value an Xpath
    expression
  • Xpath expressions
  • Find nodes (elements or attributes in the input
    document).
  • Can select specific children
  • ltxsltemplate matchEpisode"gt
    ltxslapply-templates selectDialog_at_speake
    rKing Arthur'"/gtlt/xsltemplategt
  • By default, the pattern is always applied
    starting at the node matching the template -- the
    context node

20
XSLT Outline
  • Language for transforming XML to XML
  • Components of the XSLT Language
  • Control structures
  • ltxslapply-templates selectexpressiongt
  • ltxslchoose gt
  • Commands for creating new tags in the output
  • ltxslvalue-of selectexpression/gt
  • Xpath expression language for selecting
    attributes and elements
  • Miscellaneous other stuff

21
Control Structures Conditionals
  • Two basic conditional types.
  • First is ltxslchoosegt which is like a switch
  • ltxslchoose gt
  • ltxslwhen testexpressiongt
  • lt/xslwhengt
  • ltxslotherwisegt
  • lt/xslotherwisegt
  • lt/xslchoosegt
  • Theres also a simpler form ltxslifgt
  • ltxslif testexpressiongt
  • lt/xslifgt
  • ltxslif test_at_salary gt 200000/gt
  • High-Salaried Employee!
  • lt/xslifgt

22
Priority
  • Suppose more than one template matches?
  • XSLT has a way of assigning priorities to
    templates
  • It picks the highest-priority match.
  • The most specific expression has highest
    priority.
  • ltxsltemplate matchArticlegt
  • Special treatment for article here!
  • lt/xsltemplategt
  • ltxsltemplate matchgt
  • Heres stuff we do for anything that isnt
    special
  • lt/xsltemplategt

23
ltxslapply-templatesgt
  • Select doesnt have to move down the tree.
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform" version"1.0"gt
  • ltxsltemplate match/gt
  • ltHTMLgtltBODYgt
  • ltxslapply-templates selectHTML/BODY /gt
  • lt/BODYgtlt/HTMLgt
  • lt/xsltemplategt
  • ltxsltemplate matchBODYgt
  • This is the text that is part of the document
  • ltxslapply-templates select/HTML/HEAD /gt.
  • lt/xsltemplategt
  • ltxsltemplate matchHEADgt
  • ltfont colorredgt
  • ltxslvalue-of selectTITLEgt
  • lt/fontgt

Jumps back to the HEAD element.
24
vNamed Templates
  • Can also give a template a name
  • ltxsltemplate nameheader gt
  • ltH1gt
  • The average number of wingbeats per second of a
    laden African Swallow is 5
  • lt/H1gt
  • lt/xsltemplategt
  • One can then call a template by name
  • ltxsltemplate match/gt
  • ltHTMLgt
  • ltBODYgt
  • ltxslcall-template nameheader /gt
  • lt/BODYgt
  • lt/HTMLgt
  • lt/xsltemplategt
  • Important for reusing code!

25
Parameters
  • Think of templates as being equivalent to
    functions.
  • Thus, it makes sense to be able to pass arguments
    to templates.
  • XSL has a verbose way of doing this
  • ltxsltemplate nameemphasizegt
  • ltxslparam nametext /gt
  • ltxslparam namecolor selectblue/gt
  • ltfont colorcolorgt
  • ltxslvalue-of selecttext /gt
  • lt/fontgt
  • lt/xsltemplategt
  • ltxsltemplate matchTITLEgt
  • ltxslcall-template nameemphasizegt
  • ltxslwith-param nametext select. /gt
  • ltxslwith-param namecolor selectred /gt
  • lt/xslcall-templategt
  • lt/xsltemplategt

default value
need to use single quotes so as not to select
ltbluegt
Attribute value template
26
Parameters
  • Parameters are untyped they could contain
    strings, numbers, booleans, or nodes of the tree.
  • Organization.xml
  • ltorganizationgt
  • ltPREZ fnameDaria/gt
  • ltVP fnameQuinn/gt
  • lt/organizationgt
  • Organization.xsl
  • ltxsltemplate nameprocesspersongt
  • ltxslparam nameperson/gt
  • First Name ltxslvalue-of selectperson/_at_fname
    /gt
  • lt/xsltemplategt
  • ltxsltemplate matchorganizationgt
  • ltxslcall-template nameprocesspersongt
  • ltxslwith-param nameperson selectPREZ /gt
  • lt/xslcall-templategt
  • ltxslcall-template nameprocesspersongt
  • ltxslwith-param nameperson selectVP /gt

27
Global Parameters
  • If an ltxslparam namemyparam/gt occurs at the
    beginning of the stylesheet (before any
    templates) then myparam is a global parameter.
  • A value for myparam should be given on the
    command line
  • In Xalan
  • java org.apache.xalan.xslt.Process -IN
    episode.xml XSL global.xsl PARAM color red
    OUT globalout.html

28
Variable Element
  • Variables are declared and initialized with an
    ltxslvariablegt statement. Two ways to give the
    value
  • ltxslvariable nameCE"gtCarl Everettlt/xslvariab
    legt
  • ltxslvariable namepresident
    selectorganization/PREZ/gt
  • A variable is referenced with varname, like
    parameters
  • ltxslvalue-of selectCE"/gt
  • ltxslvalue-of selectpresident/_at_fname/gt
  • ltxslapply-templates select"PRESIDENT/gt
  • Once the value of the variable is set, it cannot
    change
  • Can only be used (dereferenced) in attribute
    value templates
  • The values of variables declared in a template
    are not visible outside of the template.
  • ltxsltemplate match/gt
  • ltxslvariable nameCE"gtCarl
    Everettlt/xslvariablegt
  • ltxslapply-templates selectPREZ/gt
  • ltxslvalue-of selectCE/gt
  • lt! Carl Everett will print --gt
  • lt/xsltemplategt
  • ltxsltemplate matchPREZgt
  • ltxslvariable nameCE"gtChris
    Eigemanlt/xslvariablegt

29
Loops
  • ltxslfor-each selectexpressiongt
  • Block
  • lt/xslfor-eachgt
  • expression must evaluate to some set of nodes in
    the input. Will insert the output of Block into
    the result


30
Controlling Order
  • Both xslfor-each and xslapply-templates have as
    default order
  • The order in which the matching elements occur
    within the document.
  • You can change that order using ltxslsort gt
  • ltxslfor-each selectstudentgt
  • ltxslsort selectstudidgt
  • lt/xslsortgt
  • ......
  • lt/xslfor-eachgt
  • Analogous to SQL order by clause.
  • ltxslsort selectstudid orderdescending/gt


31
Mode
  • A good way to process the same set of nodes
    several times is using the mode attribute
  • ltxsltemplate match/gt
  • ltxslapply-templates selecttutorial
    modebuild-main-index/gt
  • ltxslapply-templates selecttutorial
    modebuild-section-indexes/gt
  • ltxslapply-templates selecttutorial
    modebuild-section-indexes/gt
  • ltxsltemplate matchtutorial modebuild-main-in
    dexgt
  • lt/xsltemplategt
  • ltxsltemplate matchtutorial modebuild-section
    -indexesgt
  • lt/xsltemplategt

32
XSLT Outline
  • Language for transforming XML to XML
  • Components
  • Control structures
  • Commands for creating new tags in the output
  • Xpath expression language
  • Miscellaneous other stuff

33
Attribute Value Templates
  • We saw that we can create a fixed tag by just
    writing it inside the template.
  • ltxsltemplates match/gt
  • ltHTMLgt ltBODYgt Bonjour Monde lt/BODYgt lt/HTMLgt
  • ltxslapply-templatesgt
  • We also know how to create dynamic text content
    using
  • ltxslvalue-ofgt
  • Suppose the values of attributes in a tag depend
    on stuff in the input document
  • ltA href article/url gt Go to article lt/Agt
  • The bracket says that the stuff inside is to be
    evaluated, not written out literally.

34
Creating new elements
  • New element put in output stream using
  • ltxslelement nameelementName/gt
  • This produces ltelementName/gt
  • ltxslelement namefoogt32.1lt/xslelementgt
  • This produces ltfoogt32.1lt/foogt
  • ltxslelement namefoogt
  • ltxslelement namefoo2gtfoolt/xslelementgt
  • lt/xslelementgt
  • ltfoogtltfoo2gtfoolt/foo2gtlt/foogt

35
Creating New Elements
  • Suppose the name of the attribute depends on the
    document?
  • ltxslattribute namesomeexpression gt
  • lt! Whatever is computed here is the value
    --gt
  • lt/xslattributegt
  • ltxslattribute namemyname gt
  • ltxslvalue-of selectmyvalue /gt
  • lt/xslattributegt
  • Can do the same thing to create a new element
  • ltxslelement namesomethinggt
  • ltxslattribute namemyattnamegt
  • lt/xslattributegt
  • lt/xslelementgt

36
Copying nodes
  • Sometimes you just want to simply copy part of
    the input document to the output.
  • You can do this quickly with ltxslcopygt and
    ltxslcopy-ofgt
  • ltxsltemplate matchOLgt
  • ltxslcopy-of select. /gt
  • lt!-- this will copy the OL tag and all of its
    subelements verbatim--gt
  • lt/xsltemplategt
  • ltxsltemplate matchOLgt
  • ltxslcopy /gt
  • lt!-- this will copy the OL tag but leave all of
    its subelements out --gt
  • lt/xsltemplategt
  • Note ltxslcopy /gt copies the current node
  • But
  • ltxslcopy-of gt expects a selectexpression
    argument, and copies the result of expression.

37
XSLT Outline
  • Language for transforming XML to XML
  • Components
  • Control structures
  • Commands for creating new tags in the output
  • Xpath expression language
  • Overview
  • Node-sets, axes, and predicates
  • Node-set operators and functions
  • Built-in Functions
  • Multiple Documents
  • Match expressions
  • Miscellaneous other stuff
  • Namespaces
  • XML-schema

38
XPath
  • A language for identifying stuff within a
    document.
  • Xpath expressions can return
  • Bunch of stuff within the document a nodeset
  • True/false, Number(s), String(s)
  • Used by other XML specifications
  • XPointer, XQL, XSLT.
  • In XSLT, it is used
  • in select attribute values
  • in other places (e.g. the test attribute of an
    xslwhen and xslif)
  • Examples
  • ltxslapply-templates selectCHAPTERgt
  • ltxslvalue-of selectchapter/_at_numbergt
  • ltxslvalue-of selectname(..)gt

39
Navigating the Tree
  • Simple Xpath expressions consist of a navigation
    path.
  • ltxslapply-templates selectCHAPTER/VERSEgt
  • ltxslfor-each selectCHAPTER/VERSE/LINEgt
  • When used in XSLT, this XPATH expression
  • takes as input the current node
  • the node that we are processing at that point in
    the XSLT transformation
  • returns the set of elements that are VERSE
    subelements of CHAPTER subelements of the node.
  • The above are examples of relative paths. One can
    also have an absolute path.
  • ltxslapply-templates select/CHAPTER/VERSEgt
  • Rough idea of navigation pathssimilar to Unix
    path names.
  • ./CHAPTER any CHAPTER child of mine. (same as
    CHAPTER)
  • .//CHAPTER any CHAPTER child of some child of
    mine.
  • ../CHAPTER any CHAPTER child of my parent.

40
Navigation Paths
  • On the other hand..bet you cant do this in Unix
    (but can in Xpath)
  • .//CHAPTER any CHAPTER descendant
    of the current node.
  • /CHAPTER//VERSE any VERSE that is a
    descendant of some CHAPTER child of the root
    node.
  • But XPATH allows you to select not just element
    nodes of the input, but attributes also.
  • ltxslfor-each selectepisode/dialog/_at_speakergt
  • ltxslvalue-of select//comment() /gt
  • lt!- Will match every comment in the document
  • So, puts the content of every comment each
  • time any speaker attribute is found --gt
  • lt/xslfor-eachgt

41
ChangeDialogToCommentValue.xsl
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltxslstylesheet version"1.0" xmlnsxslhttp//www
    .w3.org/1999/XSL/Transform xmlnsfo"http//www.w3
    .org/1999/XSL/Format"gt
  • ltxsltemplate match"/"gt
  • ltHTMLgtltBODYgt
  • ltxslfor-each select"episode/dialog
    /_at_speaker"gt
  • ltxslvalue-of select"//comment()"
    /gt
  • lt!-- Will match every comment in
    the document --gt
  • lt/xslfor-eachgt
  • lt/BODYgtlt/HTMLgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

42
Dialog to Comment
  • BridgeOfDeathEpisodeDialogToComment.xml
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • lt!-- BridgeOfDeathEpisodeDialogToComment.xml --gt
  • lt?xml-stylesheet hrefChangeDialogToComment.xsl
    typetext/xsl?gt
  • ltepisodegt
  • ltdialog speaker"Bridgekeeper"gtStop! Who would
    cross the Bridge of Death must answer me these
    questions three, ere the other side he
    see.lt/dialoggt
  • ltdialog speaker"Sir Launselot"gtAsk me the
    questions, bridgekeeper. I am not
    afraid.lt/dialoggt
  • Output
  • ltHTML xmlnsfo"http//www.w3.org/1999/XSL/Format"
    gtltBODYgt BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    BridgeOfDeathEpisodeDialogToComment.xml
    lt/BODYgtlt/HTMLgt

43
Abbreviate
  • Many XPath expressions are actually abbreviations
  • ./CHAPTER is an abbreviation for childCHAPTER
  • DIALOG/_at_speaker abbreviates childDIALOG/attribut
    espeaker
  • ./_at_speaker abbreviates attributespeaker
  • We will generally use the short name whenever
    possible.

44
The Full Tree
/
Think of an Xpath expression as working on a tree
that shows all the information in the
XML document (this tree is called the InfoSet of
the document)
  • episode

dialog
action
speaker BridgeKeeper
A giant unseen hand pick up Robin and casts
him into the gorge
What is your favorite color?
Every node has a Type (element, attribute,
Comment,processing-instruction, text), and either
a Name (dialog, episode) or a Value
(BridgeKeeper,What is your)
45
Location Path
  • You can use Xpath to get to various features of a
    node
  • ltxslvalue-of selectname(.)gt
  • ltxslapply-templates selectprocessing-instructio
    n()comment()/gt
  • ltxsltemplate matchprocessing-instruction()comm
    ent()gt
  • You can also do things like
  • ltxslvalue-of selectname(..)gt
  • ltxslvalue-of selectname(preceding-sibling(.))/
    gt

46
Xpath Wildcards
  • Xpath has three wildcards
  • The asterisk () selects all element notes in the
    current context
  • The at-sign and asterisk (_at_) selects all
    attribute nodes in the current context
  • The node() test, selects all nodes in the current
    context

47
Xpath Axes
  • Child axis
  • childlines/childline (for example)
  • Select all nodes named line who are children of a
    node named lines who are children of the context
    node
  • Same as lines/line (that is, child is default)
  • Parent axis
  • Parentlines
  • Select the parent node named lines of the context
    node (if one exists, otherwise empty node-set)
  • Same as ../lines

48
Xpath Axes
  • Self axis
  • self
  • Contains the context node
  • Same as .
  • Attribute axis
  • attributetype (for example)
  • Contains the attributes of the context node
  • Same as _at_type

49
Xpath Axes, cont.
  • ancestor axis
  • Contains the parent, parents parent, etc of the
    context node
  • ancestor-or-self
  • Same as ancestor but also contains context node
  • Also descendant axis, descendant-or-self,
    preceding-sibling, following-sibling, preceding,
    following, namespace.

50
Predicates
  • In addition to a location path, you might want to
    restrict to nodes that satisfy certain additional
    conditions
  • Predicate filter is used to narrow down the list
  • Predicate is held between ' '
  • Examples
  • //dialog_at_speakerKing Arthur
  • All dialog elements anywhere in the document
    spoken by King Arthur.
  • student_at_e-mail
  • All student subelements of the current node that
    have an e-mail address
  • courses/course_at_year2001 and
    _at_semesterspring
  • All course elements that are subelements of a
    courses subelement of the current node
  • And which are for spring 2001
  • /courses/courseposition()2
  • The second course grandchild of the root.

51
More Filters
ltxslapply-templates selectdialog_at_speakerspea
kernamegt Apply templates to any dialog
children of the current node whose speaker is the
variable speakername ltxslfor-each
selectcalendarperson/_at_handlevarun/activity
date/_at_month3gt . ltxslvalue-of select
./_at_description/gt lt! Will print out the
description of the activity --gt lt/xslfor-eachgt
For every calendar item subelement of the
current node for every person whose handle is
varun grab any activity they did in month 3, and
do the following (print out description of the
activity.
52
Multi-step Xpath
  • Can continue on after a predicate test
  • student_at_lnamegt c/address/city
  • cities of all students whose last name is above c
    in the alphabet
  • (student_at_lnamegt c)position()1
  • The first student whose name is above c in the
    alphabet
  • student _at_lnamename/_at_
  • All attributes of the student whose last name
    matches the variable (or parameter) name

53
Multi-step Xpath
  • ltxsltemplate matchuniversitygt
  • ltxslfor-each selectenrollment/classcount(./stu
    dent)5 gt
  • ltxslvalue-of select.gt
  • Note that . here means the class. . normally
    stands for the context node, which changes with
    each step of the multi-step evaluation process.
  • Compare this with
  • ltxsltemplate matchuniversitygt
  • ltxslfor-each select enrollment/classcount(./s
    tudent) count(current()/student gt
Write a Comment
User Comments (0)
About PowerShow.com