Title: XSLT
1XSLT
2Catalog.xml
- lt?xml version"1.0"?gt
- ltcataloggt
- ltbookgt
- lttitle
- language"English"gtFun With XMLlt/titlegt
- ltauthorgtJohn Robotlt/authorgt
- ltisbngt12367lt/isbngt
- ltpricegt70lt/pricegt
- lt/bookgt
- ltbookgt
- lttitle
- language"English"gtXml and Javalt/titlegt
- ltauthorgtMary Joneslt/authorgt
3Applying Template Rules
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"gt
- ltxsltemplate match"catalog"gt
- lthtmlgt
- ltxslapply-templates/gt
- lt/htmlgt
- lt/xsltemplategt
- ltxsltemplate match"book"gt
- ltPgt
- ltxslapply-templates/gt
- lt/Pgt
- lt/xsltemplategt
- lt/xslstylesheetgt
Two template rules
4XSL Templates
- Template rules are defined by xsltemplate
elements. - These associate particular output with particular
input. - Each xsltemplate element has a match attribute
that specifies which nodes of the input document
the template is instantiated for. - The content of the xsltemplate element is the
actual template to be instantiated. - A template may contain both text that will appear
literally in the output document and XSLT
instructions that copy data from the input XML
document to the result.
5Example
- A template rule applied to the root node
- ltxsltemplate match"/"gt
- lthtmlgt ltheadgt lt/headgt ltbodygt lt/bodygt lt/htmlgt
- lt/xsltemplategt
This text is output in the root node
6Recursively processing the children of the root
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
gt - ltxsltemplate match"/"gt
- lthtmlgt
- ltxslapply-templates/gt
- lt/htmlgt
- lt/xsltemplategt
- ltxsltemplate match"catalog"gt
- ltbodygt
- ltxslapply-templates/gt
- lt/bodygt
- lt/xsltemplategt
- ltxsltemplate match"book"gt
- A Book
- lt/xsltemplategt
- lt/xslstylesheetgt
Matches the root node
The child nodes of the root node are processed
7- ltxsltemplate match"/"gt
- lthtmlgt
- ltxslapply-templates/gt
- lt/htmlgt
- lt/xsltemplategt
- ltxsltemplate match"catalog"gt
- ltbodygt
- ltxslapply-templates/gt
- lt/bodygt
- lt/xsltemplategt
- ltxsltemplate match"book"gt
- A Book ltxslapply-templates select"title"
/gtltbr/gt - lt/xsltemplategt
- lt/xslstylesheetgt
To choose a specific set of children
8Computing the value of a node with xslvalue-of
- The xslvalue-of element computes the value of
something (most of the time, though not always,
something in the input document) and copies it
into the output document. - The select attribute of the xslvalue-of element
specifies exactly which something's value is
being computed. - The value of a node is always a string, possibly
an empty string. - The value of an element node is concatenation of
all the character data between the element's
start tag and end tag.
9Example
- ltxsltemplate match"/"gt
- lthtmlgt
- ltxslapply-templates/gt
- lt/htmlgt
- lt/xsltemplategt
- ltxsltemplate match"catalog"gt
- ltbodygt
- ltxslapply-templates/gt
- lt/bodygt
- lt/xsltemplategt
- ltxsltemplate match"book"gt
- ltbr/gtTitle ltxslvalue-of select"title"/gt
- ltbr/gtAuthor ltxslvalue-of select"author"/gt
- lt/xsltemplategt
-
10- Output
- Title Fun With XMLAuthor John Robot Title
Xml and JavaAuthor Mary Jones Title Speaking
in SpanishAuthor Michael Herrara Title
Adventures Of Freddie FrogAuthor Susan Kidder
11Xslfor-each
- lt Xslfor-eachgt instruction selects a set of
nodes using an XPath expression and performs the
same processing for each node in the set.
12Processing multiple elements with xslfor-each -
Example
- ltxsltemplate match"/"gt
- lthtmlgt
- ltxslapply-templates/gt
- lt/htmlgt
- lt/xsltemplategt
- ltxsltemplate match"catalog"gt
- ltbodygt
- lttable width"100" border"2"gt
- lttrgt
- ltthgtTitlelt/thgt
- ltthgtAuthorlt/thgt
- ltthgtISBNlt/thgt
- ltthgtPricelt/thgt
- lt/trgt
- ltxslfor-each select"book"gt
- lttrgt
- lttdgtltxslvalue-of select"title"/gtlt/tdgt
- lttdgtltxslvalue-of select"author"/gtlt/tdgt
13Apply-templates vs for-each
- lt Xslapply-templatesgt is a rule-based pattern
and is most useful when processing an element
that may contain children of different types in
an unpredictable sequence. - This approach works well when the document itself
evolves over time. This style of processing is
sometimes called push processing. - ltxslfor-eachgt When the structure is more
regular and predictable, it is simpler to
navigate around the document using xslfor-each.
The required data is accessed directly using
xslvalue-of. This is sometimes called pull
processing.
14Making choices
- lt!-- Selection --gt
- ltxsltemplate match"/"gt
- lthtmlgt
- ltxslapply-templates/gt
- lt/htmlgt
- lt/xsltemplategt
- ltxsltemplate match"catalog"gt
- ltbodygt
- ltxslapply-templates/gt
- lt/bodygt
- lt/xsltemplategt
- ltxsltemplate match"book"gt
- ltxslchoosegt
- ltxslwhen test"./title/_at_language 'English'"gt
- ltpgtltxslvalue-of select"title"/gt -Paperback
available lt/pgt - lt/xslwhengt
- lt/xslchoosegt
- lt/xsltemplategt
-
15Patterns for matching nodes
- Using a single template to apply to more than one
element Use the asterisk wildcard () in place
of an element name in the match attribute. - For example, the template below says that all
elements should be wrapped in a P element - ltxsltemplate match""gt
- ltPgt ltxslvalue-of select"."/gt lt/Pgt
- lt/xsltemplategt
- In the event that two rules both match a single
node, then by default the more specific one takes
precedence.
16Patterns for matching nodes
- Matching children with /
- You can use the / symbol to match specified
hierarchies of elements. Used alone, the / symbol
refers to the root node. However, you can use it
between two names to indicate that the second is
the child of the first. For example, book/title
refers to title element that are children of book
elements. - Example
- ltxsltemplate match"book/price"gt
- ltugtltxslvalue-of select"."/gtlt/ugt
- lt/xsltemplategt
17Patterns for matching nodes
- Sometimes, especially with an uneven hierarchy,
you may find it easier to bypass intermediate
nodes and simply select all the elements of a
given type, whether they're immediate children,
grandchildren, and so on. - The double slash, //, refers to a descendant
element at an arbitrary level. - For example, this template rule applies to all
NAME descendants of catalog, no matter how deep - ltxsltemplate match"catalog//title"gt
- ltigtltxslvalue-of select"."/gtlt/igt
- lt/xsltemplategt
- The // operator at the beginning of a pattern
selects any descendant of the root node. - For example, this template rule processes all
price elements while completely ignoring their
location - ltxsltemplate match"//price"gt ltigtltxslvalue-of
select"."/gtlt/igt lt/xsltemplategt
18Example
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
gt - ltxsltemplate match"/"gt
- lthtmlgtltheadgtlttitlegtBook Cataloglt/titlegt lt/headgt
- ltxslapply-templates/gt
- ltbodygtlt/bodygtlt/htmlgt
- lt/xsltemplategt
- ltxsltemplate match"catalog//title"gt
- ltigtltxslvalue-of select"."/gtlt/igt
- lt/xsltemplategt
- lt/xslstylesheetgt
19Matching with
- You can test for more details about the nodes
that match a pattern using . You can perform
many different tests including - Whether an element contains a given child,
attribute, or other node - Whether the value of an attribute is a certain
string - Whether the value of an element matches a string
- What position a given node occupies in the
hierarchy
20Example
- The select attribute is used in
xslapply-templates, xslvalue-of, xslfor-each,
xslcopy-of, xslvariable, xslparam, and
xslsort to specify exactly which nodes are
operated on. - The value of select attribute is an expression
written in the XPath language. The XPath language
provides a means of identifying a particular
element, group of elements, text fragment, or
other part of an XML document. - Expressions are a superset of the match patterns
that enable you to match nodes by element name,
child elements, descendants, and attributes, as
well as by making simple tests on these items. - XPath expressions allow you to select nodes
through all these criteria but also by referring
to ancestor nodes, parent nodes, sibling nodes,
preceding nodes, and following nodes.
Furthermore, expressions aren't limited to
producing merely a list of nodes, but can also
produce booleans, numbers, and strings.
21- Node axes
- Expressions are not limited to specifying the
children and descendants of the current node.
XPath provides a number of axes that you can use
to select from different parts of the tree
relative to some particular node in the tree
called the context node. - In XSLT, the context node is normally initialized
to the current node that the template matches,
though there are ways to change this.
22Some XPath Expressions
- Axis Selects From
- ancestor The parent of the context node, the
parent of the parent of the context node, the
parent of the parent of the parent of the context
node, and so forth back to the root node - ancestor-or-self The ancestors of the context
node and the context node itself - Attribute The attributes of the context node
- Child The immediate children of the context node
- descendant The children of the context node, the
children of the children of the context node, and
so forth - descendant-or-self The context node itself and
its descendants - following All nodes that start after the end of
the context node, excluding attribute and
namespace nodes nodes that start after the end of
the context node and have the same parent as the
context node - namespace The namespace of the context
nodeparent. The unique parent node of the context
node - preceding All nodes that finish before the
beginning of the context node, excluding
attribute and namespace nodes - preceding-sibling All nodes that start before the
beginning of the context node and have the same
parent as the context nodeself
23Example
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
gt - ltxsltemplate match"/catalog"gt
- ltHTMLgt
- ltHEADgtltTITLEgtCataloglt/TITLEgtlt/HEADgt
- ltBODYgt
- ltxslapply-templates select"book"/gt
- lt/BODYgt
- lt/HTMLgt
- lt/xsltemplategt
- ltxsltemplate match"book"gt
- ltPgt
- ltxslvalue-of select"position()"/gt.
- ltxslvalue-of select"title"/gt
- lt/Pgt
- lt/xsltemplategt
- lt/xslstylesheetgt
24- Output
- 1. Fun With XML
- 2. Xml and Java
- 3. Speaking in Spanish
- 4. Adventures Of Freddie Frog
25Examples
- This template rule applies to all book elements
that are not the first child element of the
catalog by testing whether the position is
greater than 1 - ltxsltemplate matchcatalog/bookposition()gt1"gt
ltxslvalue-of select"."/gt lt/xsltemplategt - This template matches both the first and last
book elements in their parent by matching when
the position is 1 or when the position is equal
to the number of elements in the set - ltxsltemplate matchbookposition()1 or
position()last()"gt ltxslvalue-of select"."/gt
lt/xsltemplategt
26Default Template Rules
- XSLT defines several default template rules that
are implicitly included in all style sheets. The
first default rule matches root and element
nodes, and applies templates to all child nodes.
The second default rule matches text nodes and
attributes, copying their values onto the output
stream. Together these two rules mean that even a
blank XSLT style sheet with just one empty
xslstylesheet element will still produce the raw
character data of the input XML document as
output. - The default rule for elements
- The first default rule applies to element nodes
and the root node - ltxsltemplate match"/"gt ltxslapply-templates/gt
lt/xsltemplategt / is XPath shorthand for "any
element node or the root node." The purpose of
this rule is to ensure that all elements are
recursively processed even if they aren't reached
by following the explicit rules. That is, unless
another rule overrides this one (especially for
the root element), all element nodes will be
processed. - However, once an explicit rule for any parent of
an element is present, this rule will not be
activated for the child elements unless the
template rule for the parent has an
xslapply-templates child. For instance, you can
stop all processing by matching the root element
and neither applying templates nor using
xslfor-each to process the children like this - ltxsltemplate match"/"gt lt/xsltemplategt
27Default Template Rules
- Text and attribute nodes ltxsltemplate
match"text()_at_"gt ltxslvalue-of select"."/gt
lt/xsltemplategt - This rule matches all text and attribute nodes
(match"text()_at_") and outputs the value of the
node (ltxslvalue-of select"."/gt). In other
words, it copies the text from the input to the
output. This rule ensures that at the very least
an element's text is output, even if no rule
specifically matches it. Another rule can
override this one for specific elements where you
want either more or less than the text content of
an element. - This rule also copies attribute values (but not
names). However, they turn from attributes in the
input to simple text in the output. Because
there's no default rule that ever applies
templates to attributes, this rule won't be
activated for attributes unless you specifically
add a nondefault rule somewhere in the style
sheet that does apply templates to attributes of
one or more elements.
28Generating an XML document from another XML
document
29lt?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
ltxslapply-templates/gt lt/xsltemplategt
ltxsltemplate match"FitnessCenter"gt
ltxslelement name"FitnessCenter"gt
ltxslapply-templates/gt lt/xslelementgt
lt/xsltemplategt ltxsltemplate
match"Member"gt ltxslelement
name"Member"gt ltxslapply-templates/gt
lt/xslelementgt lt/xsltemplategt
Cont. --gt
30 ltxsltemplate match"Name"gt
ltxslelement name"Name"gt
ltxslapply-templates/gt lt/xslelementgt
lt/xsltemplategt ltxsltemplate
match"Phone"gt ltxslelement
name"Phone"gt ltxslapply-templates/gt
lt/xslelementgt lt/xsltemplategt
ltxsltemplate match"FavoriteColor"gt
ltxslelement name"FavoriteColor"gt
ltxslapply-templates/gt lt/xslelementgt
lt/xsltemplategt ltxsltemplate
match"text()"gt ltxslvalue-of select"."/gt
lt/xsltemplategt lt/xslstylesheetgt
31- FitnessCenter.xml
- lt?xml version"1.0"?gt
- lt?xml-stylesheet type"text/xsl"
href"FitnessCenter.xsl"?gt - ltFitnessCentergt
- ltMember level"platinum"gt
- ltNamegtJefflt/Namegt
- ltPhone type"home"gt555-1234lt/Phone
gt - ltPhone type"work"gt555-4321lt/Phone
gt - ltFavoriteColorgtlightgreylt/Favorite
Colorgt - lt/Membergt
- ltMember level"gold"gt
- ltNamegtDavidlt/Namegt
- ltPhone type"home"gt383-1234lt/Phone
gt - ltPhone type"work"gt383-4321lt/Phone
gt - ltFavoriteColorgtlightbluelt/Favorite
Colorgt - lt/Membergt
- ltMember level"platinum"gt
- ltNamegtRogerlt/Namegt
- ltPhone type"home"gt888-1234lt/Phone
gt
- FitnessCenter_new.xml
- lt?xml version"1.0" encoding"UTF-8"?gt
- ltFitnessCentergt
- ltMembergt
- ltNamegtJefflt/Namegt
- ltPhonegt555-1234lt/Phonegt
- ltPhonegt555-4321lt/Phonegt
- ltFavoriteColorgtlightgreylt/Favorite
Colorgt - lt/Membergt
- ltMembergt
- ltNamegtDavidlt/Namegt
- ltPhonegt383-1234lt/Phonegt
- ltPhonegt383-4321lt/Phonegt
- ltFavoriteColorgtlightbluelt/Favorite
Colorgt - lt/Membergt
- ltMembergt
- ltNamegtRogerlt/Namegt
- ltPhonegt888-1234lt/Phonegt
- ltPhonegt888-4321lt/Phonegt
32lt?xml version"1.0" encoding"UTF-8"?gt ltFitnessCen
tergt ltMembergt
ltNamegtJefflt/Namegt
ltPhonegt555-1234lt/Phonegt
ltPhonegt555-4321lt/Phonegt
ltFavoriteColorgtlightgreylt/FavoriteColorgt
lt/Membergt ltMembergt
ltNamegtDavidlt/Namegt
ltPhonegt383-1234lt/Phonegt
ltPhonegt383-4321lt/Phonegt
ltFavoriteColorgtlightbluelt/FavoriteColorgt
lt/Membergt ltMembergt
ltNamegtRogerlt/Namegt
ltPhonegt888-1234lt/Phonegt
ltPhonegt888-4321lt/Phonegt
ltFavoriteColorgtlightyellowlt/FavoriteColorgt
lt/Membergt lt/FitnessCentergt
Note that we've lost the attribute on the Member
element
33Getting Members Attribute
ltxsltemplate match"Member"gt
ltxslelement name"Member"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
For each attribute Add an attribute to the
element being output. The name of the
attribute is the name of the current attribute
being processed. The value of the
attribute is the value of the current
attribute being processed.
34lt?xml version"1.0" encoding"UTF-8"?gt ltFitnessCen
tergt ltMember levelplatinumgt
ltNamegtJefflt/Namegt ltPhone
type"home"gt555-1234lt/Phonegt
ltPhone type"work"gt555-4321lt/Phonegt
ltFavoriteColorgtlightgreylt/FavoriteColorgt
lt/Membergt ltMember levelgoldgt
ltNamegtDavidlt/Namegt ltPhone
type"home"gt383-1234lt/Phonegt
ltPhone type"work"gt383-4321lt/Phonegt
ltFavoriteColorgtlightbluelt/FavoriteColorgt
lt/Membergt ltMember levelplatinumgt
ltNamegtRogerlt/Namegt
ltPhone type"home"gt888-1234lt/Phonegt
ltPhone type"work"gt888-4321lt/Phonegt
ltFavoriteColorgtlightyellowlt/FavoriteColorgt
lt/Membergt lt/FitnessCentergt
35Generalize
- Our identity stylesheet will only work for
FitnessCenter XML documents. We can make a
stylesheet which does an identity transformation
on any XML document.
36lt?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
ltxslapply-templates/gt lt/xsltemplategt
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
ltxsltemplate match"text()"gt ltxslvalue-of
select"."/gt lt/xsltemplategt lt/xslstylesheet
gt
37Default Template Rules
- Every xsl document has two default template rules
- These rules are applied when the XSL Processor
cannot find a template rule to use in your
stylesheet - Here are the two default template rules
ltxsltemplate match/ gt ltxslapply-templates
/gt lt/xsltemplategt ltxsltemplate
matchtext()gt ltxslvalue-of select./gt lt/xsl
templategt
Match on the document or any element. The
action is to go to the children and execute
their template rules.
Match on a text node. The action is to output
the value of the text node.
38Multiple Applicable Rules
Suppose that the XSL Processor is processing
FitnessCenter and it gets to the ltMembergt
element. Why does it use ltxsltemplate
matchMembergt... and not the default template
rule ltxsltemplate match/ gt... ???
After all, both apply. Answer given two rules
that apply, the more specific rule wins. --gt
Clearly, is much more general than
Member. matches on any element.
Member just matches on the Member
element.
39Smallest Identity Transformation Stylesheet
- Now that we know about the default template
rules, we can further reduce the size of the
stylesheet.
40lt?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
41ltxslapply-templates selectpatterngt
- The xslapply-templates element (without the
select attribute) tells the XSL Processor to
apply the template rules to all children (in
document order) - The xsl apply-templates element can have a
select attribute that tells the XSL Processor to
process only the child element that matches
pattern. - Thus, the select attribute rule enables us to
specify the order in which the children are
processed
42ltxslapply-templates selectpatterngt
ltxsltemplate match"Member"gt
ltxslapply-templates select"Name"/gt
ltxslapply-templates select"Phone_at_type'work'"/
gt lt/xsltemplategt "Go to the template rule for my
Name child element. Then go to the template rule
for the work Phone child element."
ltxsltemplate match"Member"gt
ltxslapply-templates select""/gt lt/xsltemplategt
"Go to all the child element nodes (not to any
child text nodes)."
43Copying Elements
- The xslcopy element copies the source node into
the output tree. - Child elements, attributes, and other content are
not automatically copied. - However, the contents of the xslcopy element are
an xsltemplate element that can select these
things to be copied as well. - This is often useful when transforming a document
from one markup vocabulary to the same or a
closely related markup vocabulary.
44Shallow-copy vs Deep Copy
- Copy and copy-of constructs are used for nodes
copying. - Copy element copies the current node in the
source document to the current output
destination. - This is a shallow copy copies only the current
node without children and attributes. - The main use of ltxsl-copygt is when doing an
XML-to-XML transformation in which parts of the
document are to remain unchanged. - ltxsl-copy-ofgt copies a node-set to the current
output destination. This is a deep copy, when a
node is copied, its descendents are also copied.
45Copying ElementsExample 1
- lt!-- Find Book elements and copy them--gt
- ltxsltemplate match"catalog"gt
- ltxslcopygt
- ltxslapply-templates/gt
-
- lt/xslcopygt
-
- lt/xsltemplategt
- ltxsltemplate match"book"gt
- ltxslcopy/gt
- lt/xsltemplategt
-
- lt/xslstylesheetgt
- Catalog.xml
- lt?xml version"1.0"?gt
- ltcataloggt
- ltproduct code"1234" category"tools"gt
- ltdescriptiongtHammerlt/descriptiongt
- ltweight units"lbs"gt0.5lt/weightgt
- ltpricegt12.00lt/pricegt
- lt/productgt
- ltproduct code"0000" category"tools"gt
- ltdescriptiongtNutlt/descriptiongt
- ltweight units"lbs"gt0.1lt/weightgt
- ltpricegt2.00lt/pricegt
- lt/productgt
- ltproduct code"7777" category"tools"gt
- ltdescriptiongtBoltlt/descriptiongt
- ltweight units"lbs"gt0.1lt/weightgt
- ltpricegt1.00lt/pricegt
- lt/productgt
- ltbookgt
46Output
- lt?xml version"1.0" encoding"utf-8" ?gt
- - ltcataloggt
- ltbook /gt
- ltbook /gt
- lt/cataloggt
47Copying Elements Example 2
- lt!-- Find Book elements and copy them--gt
- ltxsltemplate match"catalog"gt
- ltxslcopygt
- ltxslapply-templates/
- lt/xslcopygt
- lt/xsltemplategt
- ltxsltemplate match"book"gt
- ltxslcopygt
- ltxslapply-templates/gt
- lt/xslcopygt
- lt/xsltemplategt
- ltxsltemplate match"title"gt
- ltxslcopygt
- ltxslvalue-of select"."/gt
- lt/xslcopygt
-
- lt/xsltemplategt
48Output
- lt?xml version"1.0" encoding"utf-8" ?gt
- - ltcataloggt
- - ltbookgt
- lttitlegtCosmoslt/titlegt
- lt/bookgt
- - ltbookgt
- lttitlegtXML Made Easylt/titlegt
- lt/bookgt
- lt/cataloggt
49Copying ElementsExample 3
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
gt - lt!-- Find Book elements and copy them--gt
- ltxsltemplate match"catalog"gt
- ltcataloggt
- ltxslfor-each select"book"gt
- ltxslcopy-of select"." /gt
- lt/xslfor-eachgt
- lt/cataloggt
-
- lt/xsltemplategt
-
- lt/xslstylesheetgt
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
gt - lt!-- Deep copy --gt
- ltxsltemplate match"catalog"gt
- ltxslfor-each select"product_at_code'7777'"gt
- ltxslcopy-of select"." /gt
- lt/xslfor-eachgt
-
- lt/xsltemplategt
-
- lt/xslstylesheetgt
50- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
gt - lt!-- Generate another catalog with products that
are less than 10 dollars--gt - ltxsltemplate match""gt
- ltxslcopygt
- ltxslcopy-of select"_at_" /gt
- ltxslapply-templates /gt
- lt/xslcopygt
-
- lt/xsltemplategt
- ltxsltemplate match"product price gt
10.00" /gt - ltxsltemplate match"product/weight" /gt
-
51What does this do?
- lt?xml version"1.0"?gt ltxslstylesheet
version"1.0" xmlnsxsl"http//www.w3.org/1999/XS
L/Transform"gt - ltxsltemplate match"_at_processing-instruction(
)text()"gt - ltxslcopygt ltxslapply-templates
select"_at_processing-instruction()text()"/gt - lt/xslcopygt lt/xsltemplategt lt/xslstylesheetgt
52Sum()
- The sum function calculates totals for a
node-set. You need to be aware of nodes that do
not contain values, as this function will return
'NaN' if one of the items is not numeric (i.e.
empty). - You will need to do formatting of your code to
overcome this, i.e. replacing empty values with a
0. - Example calculate the total of all the book
prices - ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0"gt ltxsltemplate
match"/"gt ltpgtTotal Price ltxslvalue-of
select"sum(//price)"/gtlt/pgt lt/xsltemplategtlt/x
slstylesheetgt
53Sum()
- ltxsloutput method"html"/gt
- ltxsltemplate match"/"gt
- ltHTMLgt
- ltBODYgt
- ltxslvariable name"Space" select"' '"/gt
- ltxslfor-each select"//data/month"gt
- ltbr/gtltxslvalue-of select"name"/gt
- ltxslvalue-of select"Space"/gt
- ltxslvalue-of select"format-number(sum(week/_at_boo
ks_sold), ',')"/gt - lt/xslfor-eachgt
-
- lt/BODYgt
- lt/HTMLgt
- lt/xsltemplategt
- lt/xslstylesheetgt
- ltsalesgt
- ltsummarygt
- ltheadinggtBook Storelt/headinggt
- ltsubheadgtSales Reportlt/subheadgt
- ltdescriptiongtSales Report for two
monthslt/descriptiongt - lt/summarygt
- ltdatagt
- ltmonthgt
- ltnamegtJanuary 2002lt/namegt
- ltweek number"1" books_sold"1000" /gt
- ltweek number"2" books_sold"2000" /gt
- ltweek number"3" books_sold"3000" /gt
- ltweek number"4" books_sold"4000" /gt
- lt/monthgt
- ltmonthgt
- ltnamegt April 2002lt/namegt
- ltweek number"1" books_sold"700" /gt
- ltweek number"2" books_sold"2000" /gt
- ltweek number"3" books_sold"1000" /gt
54Using modes
- Sometimes you want to include the same content
from the source document in the output document
multiple times. That's easy to do simply by
applying templates multiple times, once in each
place where you want the data to appear. However,
suppose you want the data to be formatted
differently in different locations? - For example, you need two different rules that
both apply to the same element at different
places in the document. The solution is to give
each of the different rules a mode attribute.
Then you can choose which template to apply by
setting the mode attribute of the
xslapply-templates element.
55Example
- 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
- ltxslapply-templates/gt
- lt/xsltemplategt
- ltxsltemplate match"title" mode"normal"gt
- ltxslelement name"title"gt
- ltxslapply-templates/gt
- lt/xslelementgt
- lt/xsltemplategt
- ltxsltemplate match"catalog"gt
- ltxslelement name"catalog"gt
- ltxslapply-templates/gt
- ltxslelement name"SpanishBooks"gt
- ltxslfor-each select"book_at_langua
ge'Spanish'"gt - ltxslapply-templates
select"author" mode"footnote"/gt
56Example
- ltxsltemplate match"book"gt
- ltxslelement name"book"gt
- ltxslfor-each select"_at_"gt
- ltxslattribute name"name(.)"gt
- ltxslvalue-of select"."/gt
- lt/xslattributegt
- lt/xslfor-eachgt
- ltxslapply-templates mode"normal"/gt
- lt/xslelementgt
- lt/xsltemplategt
- ltxsltemplate match"author" mode"normal"gt
- ltxslelement name"author"gt
- ltxslapply-templates/gt
- lt/xslelementgt
- lt/xsltemplategt
- ltxsltemplate match"author" mode"footnote"gt
- ltxslelement name"author"gt
- ltxsltextgtlt/xsltextgt
57mode Attribute
- Allows you to create multiple template rules for
the same element. Each template rule can process
the element differently. - So, you can have multiple template rules for the
same element. Just give each template rule a
different mode
ltxsltemplate match"Name" modenormal"gt
ltxsltemplate match"Name" mode"footnote"gt
58Using Variables
- ltxsltemplate match"/"gt
- ltxslapply-templates/gt
- lt/xsltemplategt
- ltxsltemplate match"/catalog/book"gt
- ltxslvariable name"booktitle"
select"title"/gt - ltxslvariable name"firstWord"
select"substring(booktitle,1,3)"/gt -
- ltxslchoosegt
- ltxslwhen test"firstWord 'XML'"gt
- XML Book ltxslvalue-of
select"booktitle"/gt - lt/xslwhengt
- lt/xslchoosegt
- lt/xsltemplategt
- .
-
- lt/xslstylesheetgt
59Named Templates
- lt?xml version"1.0"?gt
- ltxslstylesheet version"1.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
gt - lt!-- Named templates--gt
- ltxslvariable name"someVar select"catalog/produc
t"/gt - ltxsltemplate match"catalog/book"gt
- ltxslcall-template name"show"gt
- ltxslwith-param name "someVar"
select"'book/title'"/gt - lt/xslcall-templategt
- lt/xsltemplategt
- ltxsltemplate name"show"gt
- ltxslparam name"someVar"/gt
- ltxslvalue-of select"concat('Hi',someVar)"/gt
- lt/xsltemplategt
-
- lt/xslstylesheetgt
60Catalog.xml
- lt?xml version"1.0"?gt
- ltcataloggt
- ltbookgt
- lttitle
- language"English"gtFun With XMLlt/titlegt
- ltauthorgtJohn Robotlt/authorgt
- ltisbngt12367lt/isbngt
- ltpricegt70lt/pricegt
- lt/bookgt
- ltbookgt
- lttitle
- language"English"gtXml and Javalt/titlegt
- ltauthorgtMary Joneslt/authorgt
61Controlling which nodes to processpush model
- ltxslstylesheet version"1.0"
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"gt - ltxsltemplate match catalog"gt
- lthtmlgtltbodygt
- ltxslapply-templates/gt
- lt/bodygtlt/htmlgt
- lt/xsltemplategt
- ltxsltemplate matchbookgt
- ltxslapply-templates/gt
- lt/xsltemplategt
- ltxsltemplate matchauthortitlepricegt
- ltxslvalue-of select./gt
- lt/xsltemplategt
- lt/xslstylesheetgt
62Controlling which nodes to processpull model
- ltxsltemplate matchbookgt
- ltxslapply-templates selectauthorgt
- ltxslapply-templates selecttitlegt
- ltxslapply-templates selectpricegt
- lt/xsltemplategt
- lt/xslstylesheetgt
63Pull model
- ltxsltemplate matchbookgt
- ltxslvalue-of selectauthorgt
- ltxslvalue-of selecttitlegt
- ltxslvalue-of selectpricegt
- lt/xsltemplategt
- lt/xslstylesheetgt