XSLT - PowerPoint PPT Presentation

About This Presentation
Title:

XSLT

Description:

XSLT Processing XML using XSLT Using XPath Processing XML using XSLT One More Time -- Input One More Time -- XSLT One More Time - Output XPATH XPATH XPATH XPATH XPATH ... – PowerPoint PPT presentation

Number of Views:166
Avg rating:3.0/5.0
Slides: 81
Provided by: mm6
Category:
Tags: xslt | block | elements

less

Transcript and Presenter's Notes

Title: XSLT


1
XSLT
  • Processing XML using XSLT
  • Using XPath

2
Processing XML using XSLT
XSLT is available in Java and
C/.NET
3
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href"demo1.xsl"?gt ltbookgt
lttitlegtThe Catcher in the Ryelt/titlegt
ltauthorgtJ. D. Salingerlt/authorgt
ltpublishergtLittle, Brown and Companylt/publishergt
lt/bookgt
Input
4
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "book"gt
ltHTMLgtltBODYgtltxslapply-templates/gtlt/BODYgtlt/HTMLgt
lt/xsltemplategt ltxsltemplate match
"title"gt ltH1gtltxslapply-templates/gtlt/H1gt
lt/xsltemplategt ltxsltemplate match
"author"gt ltH3gtltxslapply-templates/gtlt/H3gt
lt/xsltemplategt ltxsltemplate match
"publisher"gt ltPgtltIgtltxslapply-templates/gtlt/
Igtlt/Pgt lt/xsltemplategt lt/xslstylesheetgt
Processing
5
ltHTMLgt ltBODYgt ltH1gtThe Catcher in the
Ryelt/H1gt ltH3gtJ. D. Salingerlt/H3gt
ltPgt ltIgtLittle, Brown and Companylt/Igt lt/Pgt
lt/BODYgt lt/HTMLgt
Output
6
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href"demo1.xsl"?gt ltlibrarygt ltbloc
kgt ltbookgt lttitlegtThe Catcher in the
Ryelt/titlegt ltauthorgtJ. D.
Salingerlt/authorgt ltpublishergtLittle, Brown
and Companylt/publishergt lt/bookgt lt/blockgt lt/lib
rarygt
Input
7
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "book"gt
ltHTMLgtltBODYgtltxslapply-templates/gtlt/BODYgtlt/HTMLgt
lt/xsltemplategt ltxsltemplate match
"title"gt ltH1gtltxslapply-templates/gtlt/H1gt
lt/xsltemplategt ltxsltemplate match
"author"gt ltH3gtltxslapply-templates/gtlt/H3gt
lt/xsltemplategt ltxsltemplate match
"publisher"gt ltPgtltIgtltxslapply-templates/gtlt/
Igtlt/Pgt lt/xsltemplategt lt/xslstylesheetgt
The default rules matches the root, library and
block elements.
8
ltHTMLgt ltBODYgt ltH1gtThe Catcher in the
Ryelt/H1gt ltH3gtJ. D. Salingerlt/H3gt
ltPgt ltIgtLittle, Brown and Companylt/Igt lt/Pgt
lt/BODYgt lt/HTMLgt
The output is the same.
9
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href"demo1.xsl"?gt ltbookgt
lttitlegtThe Catcher in the Ryelt/titlegt
ltauthorgtJ. D. Salingerlt/authorgt
ltpublishergtLittle, Brown and Companylt/publishergt
ltbookgtCliff Notes on The Catcher in the
Ryelt/bookgt lt/bookgt
Two books in the input
10
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "book"gt
ltHTMLgtltBODYgtltxslapply-templates/gtlt/BODYgtlt/HTMLgt
lt/xsltemplategt ltxsltemplate match
"title"gt ltH1gtltxslapply-templates/gtlt/H1gt
lt/xsltemplategt ltxsltemplate match
"author"gt ltH3gtltxslapply-templates/gtlt/H3gt
lt/xsltemplategt ltxsltemplate match
"publisher"gt ltPgtltIgtltxslapply-templates/gtlt/
Igtlt/Pgt lt/xsltemplategt lt/xslstylesheetgt
Whats the output?
11
ltHTMLgt ltBODYgt ltH1gtThe Catcher in the
Ryelt/H1gt ltH3gtJ. D. Salingerlt/H3gt
ltPgt ltIgtLittle, Brown and Companylt/Igt lt/Pgt
ltHTMLgt ltBODYgtCliff Notes on The Catcher in the
Ryelt/BODYgt lt/HTMLgt lt/BODYgt lt/HTMLgt
Illegal HTML
12
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href"demo1.xsl"?gt ltbookgt
lttitlegtThe Catcher in the Ryelt/titlegt
ltauthorgtJ. D. Salingerlt/authorgt
ltpublishergtLittle, Brown and Companylt/publishergt
lt/bookgt
Input
13
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "book"gt
ltHTMLgtltBODYgtltxslapply-templates/gtlt/BODYgtlt/HTMLgt
lt/xsltemplategt ltxsltemplate match
"title"gt ltH1gtltxslapply-templates/gtlt/H1gt
lt/xsltemplategt ltxsltemplate match
"author"gt ltH3gtltxslapply-templates/gtlt/H3gt
lt/xsltemplategt lt!-- ltxsltemplate match
"publisher"gt ltPgtltIgtltxslapply-templates/gtlt/
Igtlt/Pgt lt/xsltemplategt --gt lt/xslstylesheetgt
We are not matching on publisher.
14
ltHTMLgt ltBODYgt ltH1gtThe Catcher in the
Ryelt/H1gt ltH3gtJ. D. Salingerlt/H3gt
Little, Brown and Company lt/BODYgt lt/HTMLgt
We get the default rule matching the publisher
and then printing its child.
15
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href"demo1.xsl"?gt ltbookgt
lttitlegtThe Catcher in the Ryelt/titlegt
ltauthorgtJ. D. Salingerlt/authorgt
ltpublishergtLittle, Brown and Companylt/publishergt
lt/bookgt
Input
16
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "book"gt
ltHTMLgtltBODYgtltxslapply-templates/gtlt/BODYgtlt/HTMLgt
lt/xsltemplategt ltxsltemplate match
"title"gt ltH1gtltxslapply-templates/gtlt/H1gt
lt/xsltemplategt ltxsltemplate match
"author"gt ltH3gtltxslapply-templates/gtlt/H3gt
lt/xsltemplategt ltxsltemplate match
"publisher"gt lt!-- Skip the publisher --gt
lt/xsltemplategt lt/xslstylesheetgt
We can skip the publisher by matching and
stopping the recursion.
17
ltHTMLgt ltBODYgt ltH1gtThe Catcher in the
Ryelt/H1gt ltH3gtJ. D. Salingerlt/H3gt
lt/BODYgt lt/HTMLgt
18
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href"demo1.xsl"?gt ltshelfgt
ltbookgt lttitlegtThe Catcher in the
Ryelt/titlegt ltauthorgtJ. D.
Salingerlt/authorgt ltpublishergtLittle, Brown
and Companylt/publishergt lt/bookgt ltbookgt
lttitlegtThe Catcher in the Ryelt/titlegt
ltauthorgtJ. D. Salingerlt/authorgt
ltpublishergtLittle, Brown and Companylt/publishergt
lt/bookgt ltbookgt lttitlegtThe Catcher in
the Ryelt/titlegt ltauthorgtJ. D.
Salingerlt/authorgt ltpublishergtLittle, Brown
and Companylt/publishergt lt/bookgt lt/shelfgt
A shelf has many books.
19
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "book"gt
ltHTMLgtltBODYgtltxslapply-templates/gtlt/BODYgtlt/HTMLgt
lt/xsltemplategt ltxsltemplate match
"title"gt ltH1gtltxslapply-templates/gtlt/H1gt
lt/xsltemplategt ltxsltemplate match
"author"gt ltH3gtltxslapply-templates/gtlt/H3gt
lt/xsltemplategt ltxsltemplate match
"publisher"gt ltigtltxslapply-templates/gtlt/igt
lt/xsltemplategt lt/xslstylesheetgt
Will this do the job?
20
ltHTMLgt ltBODYgt ltH1gtThe Catcher in the
Ryelt/H1gt ltH3gtJ. D. Salingerlt/H3gt
ltigtLittle, Brown and Companylt/igt
lt/BODYgt lt/HTMLgt ltHTMLgt ltBODYgt ltH1gtThe
Catcher in the Ryelt/H1gt ltH3gtJ. D.
Salingerlt/H3gt ltigtLittle, Brown and
Companylt/igt lt/BODYgt lt/HTMLgt ltHTMLgt ltBODYgt
ltH1gtThe Catcher in the Ryelt/H1gt ltH3gtJ.
D. Salingerlt/H3gt ltigtLittle, Brown and
Companylt/igt lt/BODYgt lt/HTMLgt
This is not what we want.
21
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href"demo1.xsl"?gt ltshelfgt
ltbookgt lttitlegtThe Catcher in the
Ryelt/titlegt ltauthorgtJ. D.
Salingerlt/authorgt ltpublishergtLittle, Brown
and Companylt/publishergt lt/bookgt ltbookgt
lttitlegtThe Catcher in the Ryelt/titlegt
ltauthorgtJ. D. Salingerlt/authorgt
ltpublishergtLittle, Brown and Companylt/publishergt
lt/bookgt ltbookgt lttitlegtThe Catcher in
the Ryelt/titlegt ltauthorgtJ. D.
Salingerlt/authorgt ltpublishergtLittle, Brown
and Companylt/publishergt lt/bookgt lt/shelfgt
Same input.
22
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "shelf"gt
ltHTMLgtltBODYgtFound a shelflt/BODYgtlt/HTMLgt
lt/xsltemplategt lt/xslstylesheetgt
Checks for a shelf and quits.
23
ltHTMLgt ltBODYgtFound a shelflt/BODYgt lt/HTMLgt
Output
24
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href"demo1.xsl"?gt ltshelfgt
ltbookgt lttitlegtThe Catcher in the
Ryelt/titlegt ltauthorgtJ. D.
Salingerlt/authorgt ltpublishergtLittle, Brown
and Companylt/publishergt lt/bookgt ltbookgt
lttitlegtThe Catcher in the Ryelt/titlegt
ltauthorgtJ. D. Salingerlt/authorgt
ltpublishergtLittle, Brown and Companylt/publishergt
lt/bookgt ltbookgt lttitlegtThe Catcher in
the Ryelt/titlegt ltauthorgtJ. D.
Salingerlt/authorgt ltpublishergtLittle, Brown
and Companylt/publishergt lt/bookgt lt/shelfgt
Same input.
25
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "shelf"gt ltHTMLgt
ltBODYgt ltbgtThese are a few of
my favorite bookslt/bgt lttable width
"640 border 5gt
ltxslapply-templates/gt lt/tablegt
lt/BODYgt lt/HTMLgt lt/xsltemplategt
ltxsltemplate match "book"gt lttrgt
lttdgt ltxslnumber/gt lt/tdgt
ltxslapply-templates/gt lt/trgt
lt/xsltemplategt ltxsltemplate match "title
author publisher"gt lttdgtltxslapply-templat
es/gtlt/tdgt lt/xsltemplategt lt/xslstylesheetgt
Produce a table of books.
26
ltHTMLgt ltBODYgt ltbgtThese are a few of my favorite
bookslt/bgt lttable width"640 border 5gt
lttrgt lttdgt1lt/tdgt lttdgtThe Catcher in the
Ryelt/tdgt lttdgtJ. D. Salingerlt/tdgt
lttdgtLittle, Brown and Companylt/tdgt lt/trgt
lttrgt lttdgt2lt/tdgt lttdgtThe XSLT Programmer's
Referencelt/tdgt lttdgtMichael Kaylt/tdgt
lttdgtWrox Presslt/tdgt lt/trgt lttrgt
lttdgt3lt/tdgt lttdgtComputer Organization and
Designlt/tdgt lttdgtPatterson and
Henesseylt/tdgt lttdgtMorgan Kaufmannlt/tdgt
lt/trgt lt/tablegt lt/BODYgt lt/HTMLgt
27
(No Transcript)
28
One More Time -- Input
lt?xml version"1.0" ?gt ltshelfgt ltbookgt
lttitlegtThe Catcher in the Ryelt/titlegt
ltauthorgtJ. D. Salingerlt/authorgt
ltpublishergtLittle, Brown and Companylt/publishergt
lt/bookgt ltbookgt lttitlegtMindfulness In
Plain Englishlt/titlegt ltauthorgtJ. D.
Salingerlt/authorgt ltpublishergtWisdom
Publications Bostonlt/publishergt lt/bookgt
ltbookgt lttitlegtTo Kill A Mokingbirdlt/titlegt
ltauthorgtHarper Leelt/authorgt
ltpublishergtAddison-Wesleylt/publishergt
lt/bookgt lt/shelfgt
29
One More Time -- XSLT
ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt
ltxsltemplate match "shelf"gt ltHTMLgt
ltBODYgt ltbgtThese are a few of
my favorite bookslt/bgt lttable width
"640" border "5"gt
ltxslapply-templates/gt lt/tablegt
lt/BODYgt lt/HTMLgt lt/xsltemplategt
ltxsltemplate match "book"gt lttrgt
lttdgt ltxslnumber value"position() div
2"/gt lt/tdgt ltxslapply-templates/gt
lt/trgt lt/xsltemplategt ltxsltemplate
match "title author publisher"gt
lttdgtltxslapply-templates/gtlt/tdgt
lt/xsltemplategt lt/xslstylesheetgt
Lets do some calculations on the fly.
30
One More Time - Output
31
XPATH
  • Non-xml language used to identify particular
    parts
  • of an xml document
  • Used by XSLT for matching and selecting
    particular
  • elements to be copied into the result tree.
  • Used by Xpointer to identify a particular point
    in or part
  • of an xml document that an Xlink links to.
  • Slides adapted from XML in a Nutshell by Harold

32
XPATH
First, well look at three commonly used XSLT
instructions xslvalue-of
xsltemplate xslapply-templates
33
XPATH
ltxslvalue-of select XPathExpression /gt
The xslvalue-of element computes the string
value of an Xpath expression and inserts it into
the result tree. XPath allows us to select nodes
in the tree and different node types produce
different values.
34
XPATH
ltxslvalue-of select XPathExpression /gt
element gt the text content of the element after
all tags are stripped
text gt the text of the node attribute gt the
value of the attribute root gt the value of
the root processing-instruction gt the
processing instruction data
(lt?, ?gt, and the target
are not
included comment gt the text of the
comment (no comment symbols) namespace gt the
namespace URI node set gt the value of the
first node in the set
35
XPATH
ltxsltemplate match pattern /gt The
xsltemplate top-level element is the key to all
of xslt. The match attribute contains a pattern
(location path) against which nodes are compared
as theyre processed. If the pattern matches a
node, then the contents are instantiated
36
XPATH
ltxslapply-templates select XPath node set
expression /gt Find and apply the
highest priority template that matches
the node set expression. If the select
attribute is not present then all children
of the context node are processed.
37
The Tree Structure of an XML Document
See Harold Pg. 147
lt?xml version"1.0" ?gt lt?xml-stylesheet
type"text/xsl" href "pi.xsl" ?gt ltpeoplegt
ltperson born"1912" died "1954" id"p342"gt
ltnamegt ltfirst_namegtAlanlt/first_namegt
ltlast_namegtTuringlt/last_namegt lt/namegt
lt!-- Did the word "computer scientist" exist
in Turing's day? --gt ltprofessiongtcomputer
scientistlt/professiongt ltprofessiongtmathemati
cianlt/professiongt ltprofessiongtcryptographerlt
/professiongt lt/persongt
38
ltperson born"1918" died "1988"
id"p4567"gt ltnamegt
ltfirst_namegtRichardlt/first_namegt
ltmiddle_initialgtx4Dlt/middle_initialgt
ltlast_namegtFeynmanlt/last_namegt lt/namegt
ltprofessiongtphysicistlt/professiongt
lthobbygtPlaying the bongoeslt/hobbygt
lt/persongt lt/peoplegt
Unicode M
39
/
lt?xml-stylesheet type"text/xsl" href
some.xsl" ?gt
born 1914
person
person
died 1952
idp342
name
profession
lt! Did the word computer scientist exist in
Turings day?-- gt
first_name
Alan
40
Nodes seen by XPath
Constructs not seen by XPath
The root Element Nodes Text Nodes Attribute
Nodes Comment Nodes Processing Instructions Namesp
ace Nodes
CDATA sections Entity references Document Type
Declarations
41
Note
The following appears in each example below so
it has been removed from the slides.
ltxslstylesheet xmlnsxsl"http//www.w3.org/199
9/XSL/Transform" version"1.0" gt lt/xslstyles
heetgt
42
Location Paths
  • The root

ltxsltemplate match"/"gt ltagtmatched the
rootlt/agt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt ltagtmatched
the rootlt/agt
43
Location Paths
  • Child element location paths (relative to
    context node)

ltxsltemplate match"/"gt ltxslvalue-of select
"people/person/profession" /gt lt/xsltemplategt
computer scientist
44
Location Paths
  • Attribute location paths (relative to context
    node)

ltxsltemplate match"/"gt ltxslvalue-of select
"people/person/_at_born" /gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt 1912
45
Location Paths
  • Attribute location paths (relative to context
    node)

ltxsltemplate match"/"gt ltxslapply-templates
select "people/person" /gt lt/xsltemplategt ltxsl
template match "person"gt ltdategt
ltxslvalue-of select "_at_born" /gt
lt/dategt lt/xsltemplategt
ltdategt1912lt/dategtltdategt191
8lt/dategt
46
Location Paths
  • Comment Location Step (comments dont have names)

ltxsltemplate match"/"gt ltxslvalue-of select
"people/person/comment()" /gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt Did the
word "computer scientist" exist in Turing's day?
47
Location Paths
  • Comment Location Step

ltxsltemplate match "comment()" gt
ltigtcomment deletedlt/igt lt/xsltemplategt
Document content with comments replaced as
shown. Default no comments output
48
Location Paths
  • Text Location Step (Text nodes dont have names)

ltxsltemplate match"/"gt ltxslvalue-of select
"people/person/profession/text()"
/gt lt/xsltemplategt
computer scientist
49
Location Paths
  • Processing Instruction Location Step

ltxsltemplate match"/"gt ltxslvalue-of select
"processing-instruction()" /gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt type"text/
xsl" href "pi.xsl"
50
Location Paths
  • Wild cards
  • There are three wild cards , node(), _at_
  • The matches any element node. It will not
    match
  • attributes, text nodes, comments or
    processing
  • instructions nodes.

51
Location Paths
  • Matching with
  • ltxsltemplate match "" gt
  • ltxslapply-templates select "" /gt
  • lt/xsltemplategt

Matches all elements and requests calls on
sub-elements only. Nothing is displayed. The text
nodes are never reached.
52
Location Paths
  • Matching with node()

The node() wild card matches all nodes element
nodes, text nodes, attribute nodes, processing
instruction nodes, namespace nodes and comment
nodes.
53
Matching with Node
ltxsltemplate match"node()"gt
ltxslapply-templates/gt lt/xsltemplategt
What is the output?
54
Matching with Node -Output
lt?xml version"1.0" encoding"UTF-8"?gt
55
Location Paths
  • Matching with _at_

The _at_ wild card matches all attribute nodes.
56
Matching with _at_
ltxsltemplate match"_at_"gt Found an
attribute ltxslvalue-of select"."/gt lt/xslte
mplategt ltxsltemplate match"node()"gt
ltxslapply-templates select"_at_"/gt
ltxslapply-templates/gt lt/xsltemplategt
What is the output?
57
Matching with _at_ - Output
lt?xml version"1.0" encoding"UTF-8"?gt Found
an attribute 1912 Found an attribute
1954 Found an attribute p342 Found an
attribute 1918 Found an attribute
1988 Found an attribute p4567
58
Matching with _at_
ltxsltemplate match "person" gt ltbgt
ltxslapply-templates select "_at_" /gt
lt/bgt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt
ltbgt19121954p342lt/bgt ltbgt19181988p4567lt/bgt
59
Location Paths
  • Multiple matches with
  • ltxsltemplate match "professionhobby" gt
  • ltactivitygt
  • ltxslvalue-of select "text()"/gt
  • lt/activitygt
  • lt/xsltemplategt
  • ltxsltemplate match "" gt
  • ltxslapply-templates /gt
  • lt/xsltemplategt
  • ltxsltemplate match "text()" gt
  • lt/xsltemplategt

Matches all the elements. Skips the text nodes
unless they describe a profession or hobby.
60
Location Paths
  • Selecting from all descendants with //

// selects from all descendants of the context
node as well as the context node itself. At the
beginning of an Xpath expression, it selects from
all descendants of the root node.
61
Location Paths
  • Selecting from all descendants with //

ltxsltemplate match "//name/last_name/text()"
gt ltxslvalue-of select "."
/gt lt/xsltemplategt ltxsltemplate match
"text()" gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt TuringFeynm
an
62
Location Paths
  • Selecting from all descendants with //

ltxsltemplate match "/" gt ltxslvalue-of
select "//first_name/text()" /gt lt/xsltemplategt

lt?xml version"1.0" encoding"utf-8"?gt Alan
63
Location Paths
  • Selecting from all descendants with //

ltxsltemplate match "/" gt
ltxslapply-templates select "//first_name/text()
" /gt lt/xsltemplategt ltxsltemplate select
"text()" gt ltxslvalue-of select "."
/gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt AlanRichard
64
Location Paths
  • Selecting from all descendants with //

ltxsltemplate match "/" gt
ltxslapply-templates select "//middle_initial/..
/first_name" /gt lt/xsltemplategt ltxsltemplate
select "text()" gt ltxslvalue-of select
"." /gt lt/xsltemplategt lt/xslstylesheetgt
lt?xml version"1.0" encoding"utf-8"?gt Richard
65
Specifying the Child Axis
Consider the following path /Envelope/Header/Si
gnature The above is an abbreviation
for /childEnvelope/childHeader/childSignat
ure
66
Using an Axis
ltxsltemplate match"people"gt
ltxslapply-templates select"person"/gt lt/xsltemp
lategt ltxsltemplate match "person" gt
ltxslif test"position() last()"gt
ltxslvalue-of select"preceding-siblingpers
on/name"/gt lt/xslifgt
67
ltxslif test"position() ! last()"gt
ltxslvalue-of select"following-sibl
ingperson/name"/gt
lt/xslifgt lt/xsltemplategt
What is the output?
68
Axis Example - Output
lt?xml version"1.0" encoding"UTF-8"?gt
Richard M Feynman
Alan Turing
69
Writing Output to an Attribute
ltxsltemplate match"_at_"gt ltsomeTag
id"."gtlt/someTaggt lt/xsltemplategt ltxsltemplat
e match"node()"gt ltxslapply-templates
select"_at_"/gt ltxslapply-templates/gt lt/xslt
emplategt
70
Writing Output to an Attribute
lt?xml version"1.0" encoding"UTF-8"?gt ltsomeTag
id"1912"/gt ltsomeTag id"1954"/gt ltsomeTag
id"p342"/gt ltsomeTag id"1918"/gt ltsomeTag
id"1988"/gt ltsomeTag id"p4567"/gt
71
Predicates
In general, an Xpath expression may refer to
more than one node. Predicates allow us to reduce
the number of nodes we are interested in. Each
step in a location path may have a predicate that
selects from the node list that is current at
that step in the expression. The boolean
expression in the predicate is tested against
each node in the context node list. If the
expression is false then that node is deleted
from the list.
72
Predicates
ltxsltemplate match "/" gt
ltxslapply-templates select "//profession.'phy
sicist'/../name" /gt lt/xsltemplategt ltxsltempla
te select "text()" gt ltxslvalue-of select
"." /gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt
Richard M Feynman
73
Predicates
ltxsltemplate match "/" gt
ltxslapply-templates select "//person_at_id'p4567
'" /gt lt/xsltemplategt ltxsltemplate select
"text()" gt ltxslvalue-of select "."
/gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt
Richard M Feynman
physicist Playing the bongoes
74
Predicates
ltxsltemplate match "/" gt
ltxslapply-templates select "//person_at_born
lt 1915" /gt lt/xsltemplategt ltxsltemplate
select "text()" gt ltxslvalue-of select
"." /gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt
Alan Turing computer scientist
mathematician cryptographer
75
Predicates
ltxsltemplate match "/" gt
ltxslapply-templates select "//person_at_born
lt 1919 and _at_born gt 1917"
/gt lt/xsltemplategt ltxsltemplate select
"text()" gt ltxslvalue-of select "."
/gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt
Richard M Feynman
physicist Playing the bongoes
76
Predicates
ltxsltemplate match "/" gt
ltxslapply-templates select "/people/person_at_bor
n lt 1950/
namefirst_name'Alan'" /gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt
Alan Turing
77
General XPath Expressions
Xpath expressions that are not node sets cant be
used in the match attribute of an xsltemplate
element. They can be used for the values for the
select attribute of xslvalue-of elements and in
location path predicates.
78
General XPath Expressions
ltxsltemplate match "/" gt
ltxslapply-templates select "/people/person"
/gt lt/xsltemplategt ltxsltemplate match
"person"gt ltxslvalue-of select"_at_born div 10"
/gt lt/xsltemplategt ltxsltemplate match
"text()"gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt 191.2191.8
79
General XPath ExpressionsXpath Functions
ltxsltemplate match "/" gt
ltxslapply-templates select "/people/person"
/gt lt/xsltemplategt ltxsltemplate match
"person"gt Person ltxslvalue-of
select"position()" /gt lt/xsltemplategt ltxsltempl
ate match "text()"gt lt/xsltemplategt
lt?xml version"1.0" encoding"utf-8"?gt
Person 1 Person 2
80
General XPath ExpressionsXpath Functions
ltxsltemplate match "/" gt
ltxslapply-templates select "//namestarts-with(
last_name,'T')"/gt lt/xsltemplategt ltxsltemplate
match "name"gt Mr. T. ltxslvalue-of
select"." /gt lt/xsltemplategt
Node set converted to string
lt?xml version"1.0" encoding"utf-8"?gt Mr.
T. Alan Turing
Write a Comment
User Comments (0)
About PowerShow.com