XSLT
XSLT
0
o Introduction
o Elements
XAPTH 2.0
o Introduction
o XPath Terminology
o Expression
o Operators
o Axes
o Function Library
Regular Expression
Important URLs
XSLT
Introduction
XSL stands for “Extensible Stylesheet Language”, which consists of three parts—
1. <xsl:stylesheet>
Definition: Defines the root element of a style sheet.
2. <xsl:transform>
Definition: Defines the root element of a style sheet.
3. <xsl:template>
Definition: Rules to apply when a specified node is matched. The type of node to be processed is identified by a
pattern, written in the mandatory match attribute. The following examples show some of the possibilities:
Pattern Meaning
XXX//YYY Matches any YYY element that has an ancestor named XXX
/*/XXX Matches any XXX element that is immediately below the root
(document) element
XXX[1] Matches any XXX element that is the first XXX child of its parent
element. (Note that this kind of pattern can be very inefficient: it is
better to match all XXX elements with a single template, and then use
xsl:if to distinguish them)
SECTION[TITLE="Contents"] Matches any SECTION element whose first TITLE child element has the
value "Contents"
A/TITLE | B/TITLE | C/TITLE Matches any TITLE element whose parent is of type A or B or C
4. <xsl:apply-templates>
Definition: Applies a template rule to the current element or to the current element's child nodes.
5. <xsl:element>
Definition: Creates an element node in the output document
6. <xsl:attribute>
Definition: Adds an attribute. The attribute must be output immediately after the element.
7. <xsl:value-of>
Definition: Extracts the value of a selected node
8. <xsl:output>
Definition: Defines the format of the output document. It is always a top-level element.
cdata-section-elements Optional This is used only for XML output. It is a whitespace-separated list of
element names. Character data belonging to these output elements will
be written within CDATA sections.
9. <xsl:text>
Definition: Writes literal text to the output. It allows white space to the output.
10.<xsl:comment>
Definition: Creates a comment node in the result tree.
11.<xsl:message>
Definition: Writes a message to the output (used to report errors).
12.<xsl:number>
Definition: Determines the integer position of the current node and formats a number
13.<xsl:if>
Definition: Contains a template that will be applied only if a specified condition is true.
14.<xsl:choose>
Definition: Used in conjunction with <when> and <otherwise> to express multiple conditional tests.
It is used to choose one of a number of alternative outputs.
The element typically contains a number of xsl:when elements, each with a separate test condition.
The first xsl:when element whose condition matches the current element in the source document is
expanded, the others are ignored. If none of the conditions is satisfied, the xsl:otherwise child
element, if any, is expanded.
15.<xsl:when>
Definition: It is used within an <xsl:choose> element to indicate one of a number of choices.
16.<xsl:otherwise>
Definition: The <xsl:otherwise> element is used within an <xsl:choose> element to indicate the
default action to be taken if none of the other choices matches.
17.<xsl:for-each>
Definition: Allows iteration in XSLT.
18.<xsl:sort>
Definition: used to sort the output. The xsl:sort element is used within an xsl:for-each or xsl:apply-
templates element to indicate the order in which the selected elements are processed.
19.<xsl:analyze-string>
Definition: The <xsl:analyze-string> element is new in XSLT 2.0.
It applies a regular expression to a supplied string value. The string is split into a sequence of
substrings, each of which is classified as either a matching substring (if it matches the regular
expression) or a non-matching substring (if it doesn't). The substrings are then processed individually:
the matching substrings by a <xsl:matching-substring> element that appears as a child of the
<xsl:analyze-string> instruction, the non-matching substrings by a similar <xsl:non-matching-
substring> element. If either of these is omitted, the relevant substrings are not processed.
When processing matching substrings, it is possible to call the regex-group() function to find the
parts of the matching substring that matched particular parenthesized groups within the regular
expression.
20.<xsl:matching-substring>
Definition: It is used within an <xsl:analyze-string> element to indicate the default action to be
taken with substrings that match a regular expression.
21.<xsl:non-matching-substring>
Definition: It is used within an <xsl:analyze-string> element to indicate the default action to be
taken with substrings that do not match a regular expression.
22.<xsl:strip-space>
Definition: It is used at the top level of the stylesheet to define elements in the source document for
which white-space nodes are insignificant and should be removed from the tree before processing.
Attribute Usage Description
element Required Defines a space-separated list of element names.
The value "*" may be used to mean "all elements"; in this case any
elements where whitespace is not to be stripped may be indicated by
an <xsl:preserve-space> element.
23.<xsl:preserve-space>
Definition: It is used at the top level of the stylesheet to define elements in the source document for
which white-space nodes are significant and should be retained
24.<xsl:copy>
Definition: Creates a copy of the current node (without child nodes and attributes). The actual effect
depends on whether the node is an element, an attribute, or a text node.
For an element, the start and end element tags are copied; the attributes, character content and child
elements are copied only if xsl:apply-templates is used within xsl:copy.
The following example is a template that copies the input element to the output, together with all its
child elements, character content, and attributes:
<xsl:template match="*|text()|@*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
25.<xsl:copy-of>
Definition: Creates a copy of the current node (with child nodes and attributes)
26.<xsl:attribute-set>
Definition: Defines a named set of attributes. An attribute-set contains a collection of xsl:attribute
elements.
They can be added to an element by specifying use-attribute-sets in the list of attributes for the
element. The value is a space-separated list of attribute-set names.
27.<xsl:call-template>
Definition: Calls a named template
select Optional value of the variable may be defined either by an expression within the
select attribute or by the contents of the xsl:variable element
as Optional Define the type of the variable
29.<xsl:param>
Definition: Declares a local or global parameter (formal parameter) to a template. As a template
parameter, it must be used as an immediate child of the xsl:template element. As a stylesheet
parameter, it must be used as an immediate child of the xsl:stylesheet element.
select Optional Value of the parameter may be defined either by an expression within
the select attribute or by the contents of the xsl:param element.
The default value is ignored if an actual parameter is supplied with the
same name.
as Optional Define the type of the param
30.<xsl:with-param>
Definition: Defines the value of a parameter (actual parameter) to be passed into a template. It may
be used within an xsl:call-template, xsl:apply-templates, xsl:apply-imports, or xsl:next-match
select Optional Value of the parameter may be defined either by an expression within
the select attribute or by the contents of the xsl:param element.
The parameter has no effect unless the called template includes a matching xsl:param element.
31.<xsl:apply-imports>
Definition: Applies a template rule from an imported style sheet.
The effect is to search for a template that matches the current node and that is defined in a stylesheet
that was imported (directly or indirectly, possibly via xsl:include) from the stylesheet containing the
current template, and whose mode matches the current mode. If there is such a template, it is
activated using the current node. If not, the call on xsl:apply-imports has no effect.
32.<xsl:next-match>
Definition: It is very similar to xsl:apply-imports, but with a different algorithm for choosing the next
template to execute. It chooses the template rule that matches the current node and that would have
been chosen if the current template rule and all higher precedence/priority rules were not there.
33.<xsl:import>
Definition: Imports the contents of one style sheet into another.
Note: An imported style sheet has lower precedence than the importing style sheet
34.<xsl:include>
Definition: Includes the contents of one style sheet into another.
Note: An included style sheet has the same precedence as the including style sheet
35.<xsl:processing-instruction>
Definition: Writes a processing instruction to the output and can appear anywhere within an
xsl:template.
36.<xsl:for-each-group>
Definition: The xsl:for-each-group element selects a sequence of nodes and/or atomic values and
organizes them into subsets called groups. There are four possible ways of defining the grouping:
group-by: This groups together all items having the same value for a grouping key. The grouping key
may have multiple values (a sequence of values) in which case the item is added to more than one
group.
group-adjacent: This groups together all items having the same value for a grouping key, provided
that they are also adjacent in the input sequence. This is useful when you need to wrap a new element
around a sequence of related elements in the source documents, for example a consecutive sequence
of <bullet> elements. In this case the grouping key must be single-valued.
group-starting-with: This processes the items in the supplied sequence in turn, starting a new group
whenever one of the items matches a specified pattern. This is useful, for example, when matching an
<h2> element and its following <p> elements.
group-ending-with: This processes the items in the supplied sequence in turn, closing the current
group whenever one of the items matches a specified pattern. This is useful when matching a
sequence of items in which the last item in the group carries some distinguishing attribute such as
continued="no".
37.<xsl:function>
Definition: The xsl:function element defines a function within a stylesheet. The function is written in
XSLT but it may be called from any XPath expression in the stylesheet. It must have a non-default
namespace prefix.
Example—
<xsl:function name="my:factorial" as="xs:integer">
<xsl:param name="number" as="xs:integer"/>
<xsl:sequence select="if ($number=0) then 1 else $number * my:factorial($number-1)"/>
38.<xsl:key>
Definition: Declares a named key that can be used in the style sheet with the key() function
Attribute Usage Description
name Required Gives the name of the Key
match Required Determines which nodes in the source document will be matched
use Optional Specifies the criteria which decides whether the node matches or not
39.<xsl:result-document>
Definition: It is new in XSLT 2.0. It is used to output its contents to a file.
40.<xsl:character-map>
Definition: It must be a child of xsl:stylesheet and it contains one or more xsl:output-characters. The
character specifies the original character which is replaced by the value of attribute string.
Example:
<xsl:character-map name="char-map">
<xsl:output-character character="ù" string="&#x00F9;"/>
<xsl:output-character character="ú" string="&#x00FA;"/>
</xsl:character-map>
41.<xsl:sequence>
Definition: It is used to construct arbitrary sequences. It may select any sequence of nodes and/or
atomic values, and essentially adds these to the result sequence.
Example:
(1)
<e>
<xsl:sequence select="1 to 5"/>
<br/>
<xsl:sequence select="6 to 10"/>
</e>
(2)
<xsl:variable name="seq" as="xs:integer *">
<xsl:for-each select="1 to 5">>
<xsl:sequence select=". * ."/>
</xsl:for-each/>
</xsl:variable>
(3)
<xsl:variable name="seq" as="attribute() *">
<xsl:attribute name="a">10</xsl:attribute>
<xsl:attribute name="b">20</xsl:attribute>
<xsl:attribute name="a">30</xsl:attribute>
</xsl:variable>
42.<xsl:decimal-format>
Definition: Defines the characters and symbols to be used when converting numbers into strings, with
the format-number() function
43.<xsl:fallback>
Definition: Specifies an alternate code to run if the processor does not support an XSLT element
44.<xsl:namespace-alias>
Definition: Replaces a namespace in the style sheet to a different namespace in the output
45.<xsl:import-schema>
Definition:
46.<xsl:namespace>
Definition:
47.<xsl:sort-key>
Definition:
48.<xsl:perform-sort>
Definition:
XPATH
Introduction
XPath Terminology
Nodes
Atomic values
Items
Parent
Children
Siblings
Ancestors
Descendants
J K. Rowling
"en"
Expression
Three Forms of XPath Expressions are
Path Expression— expression1/expression2
Predicate Expression— expression1[expression2]
Union Expression— expression1 | expression2
Expression Description
nodename Selects all child nodes of the named node
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes
Examples
Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!
bookstore/book Selects all book elements that are children of bookstore
//book Selects all book elements no matter where they are in the document
bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under
the bookstore element
//@lang Selects all attributes that are named lang
Predicates
Predicates are used to find a specific node or a node that contains a specific value.
Predicates are always embedded in square brackets.
In the table below we have listed some path expressions with predicates and the result of the expressions:
Note: IE5 and later has implemented that [0] should be the first node, but according to
the W3C standard it should have been [1]!!
/bookstore/book[last()] Selects the last book element that is the child of the bookstore element
/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element
/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element
//title[@lang] Selects all the title elements that have an attribute named lang
//title[@lang='eng'] Selects all the title elements that have an attribute named lang with a value of 'eng'
/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a
value greater than 35.00
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a
price element with a value greater than 35.00
Wildcard Description
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind
In the table below we have listed some path expressions and the result of the expressions:
XPATH Axes—
An axis defines a node-set relative to the current node.
Examples
Example Result
child::book Selects all book nodes that are children of the current node
attribute::lang Selects the lang attribute of the current node
child::* Selects all children of the current node
attribute::* Selects all attributes of the current node
child::text() Selects all text child nodes of the current node
child::node() Selects all child nodes of the current node
descendant::book Selects all book descendants of the current node
ancestor::book Selects all book ancestors of the current node
ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is
a book node
child::*/child::price Selects all price grandchildren of the current node
XPath Operators
1. lower-case()
Description—To convert in the lower case.
lower-case(.)
2. upper-case()
Description—To convert in the upper case.
upper-case(.)
4. substring-before(string, sub-string)
Description—returns the characters in the string prior to the first occurrence of sub-string
substring-before(.,':')
5. substring-after(string, sub-string)
Description—returns the characters in the string after the first occurrence of sub-string
substring-after(.,':')
7. starts-with(string1, string2)
Description—returns true if string1 starts with string2
starts-with(bbb,ccc)
starts-with(.,'FILE')
8. ends-with(string1, string2)
Description—returns true if string1 ends with string2
starts-with(bbb,ccc)
starts-with(.,'FILE')
9. contains(string1, string2)
Description—returns true if string1 contains string2
Example Description
contains(//Para[1], 'SCRIPT') Does the first Para element contain the
string “SCRIPT”?
//node()[contains(., 'SCRIPT')] Select all nodes containing the string
“SCRIPT”
count(//node()[contains(., 'SCRIPT')]) Count the number of nodes containing the
string “SCRIPT”
count(/Playlist/Song)
count(//Title)
10. string-length(string?)
Description—returns a number containing the length of the argument string (or the length of the
string value of the current node if the argument is omitted)
string-length(//Para[1])
12. normalize-space(string?)
Description—normalizes the space (collapses whitespace) of the string argument (or the current
node's string value if the string argument is omitted)
normalize-space()
normalize-space($x)
13.count(node-set)
Description—returns a number which counts how many nodes are in the argument
Example Description
count(//Para) Count the number of Para elements
count(//Para[@classification Count the number of Para elements with
='secret']) secret classification
14.sum(node-set)
Description—converts each node to a string then a number, then sums the numbers
sum(current-group())
15.avg()
Description—calculates average of all values of a sequence.
16.min()
Description—finds minimal value in a sequence
17.max()
Description—finds maximal value in a sequence
18.round(number)
Description—returns the integer closest to the number (by rounding)
round (2.3)
It will return 2
19.name(node-set?)
Description— returns name of currently processed elements or attributes. It include namespace also,
so use local-name() for the name only.
name()
name(/document/*[last()])
20.local-name(node-set?)
Description—returns the local name of the first node in the node-set (no prefix included)
21.position()
Description—returns the position of the current node in the current context
member[position() = 2]
22.last()
Description—returns the size of the current context
23.current()
Description—returns a node-set containing the current node as its only member
24.format-number(number,string1,string2?)
Description—produces formatted numeric output, where string1 contains a format pattern and
string2 contains an optional decimal format
25.index-of(sequence, value)
Description—The index-of() function allows you to obtain the position of value in sequence.
index-of((1,3,5,7,9,11),7)
Output: 4
26.remove(sequence, position)
Description—The remove function enables you to remove a value at a specified position from a
sequence.
remove((1,3,5,7,9,11),4)
Output: (1,3,5,7,9,11)
27.insert-before(sequence, position,value)
Description—Returns the sequence plus a new value.
insert-before((1,3,5,7,9,11),2,15)
Output: (1,15,3,5,7,9,11)
28.reverse(sequence)
Description—This function reverses the items in sequence.
reverse (1 to 10)
Output: 10 9 8 7 6 5 4 3 2 1
29.current-dateTime()
Description—returns the current date and time
30.matches()
Description—matches() is used to find patterns specified by a regular expression in a string. If a
match is found the function returns boolean value "true" otherwise it returns "false".The default mode
of matches() search is case sensitive An optional flag "i" can be specified to indicate a case insensitive
searching.
Input: <aaa>How do you do?</aaa>
<xsl:template match="/aaa">
<xxx>
<xsl:value-of select="matches(.,'do\s+you')"/>
</xxx>
<yyy>
<xsl:value-of select="matches(.,'\S{6}')"/>
</yyy>
<zzz>
<xsl:value-of select="matches(.,'how','i')"/>
</zzz>
</xsl:template>
Output:
<xxx>true</xxx>
<yyy>false</yyy>
<zzz>true</zzz>
31.replace()
Description—The function replace() replaces a pattern given as the second argument by the third
one. The "i" flag given as the forth optional argument enables case insensitive replacement. If the
second argument does not match any pattern in the first one the first argument is returned.
(1)
Input:
<aaa>How do you do?</aaa>
<xsl:template match="/aaa">
<xxx>
<xsl:value-of select="replace(.,'o.','#')"/>
</xxx>
<yyy>
<xsl:value-of select="replace(.,'ho','QQQ','i')"/>
</yyy>
<zzz>
<xsl:value-of select="replace(.,'HHH','BFF')"/>
</zzz>
</xsl:template>
Output:
<xxx>H# d#y# d#</xxx>
<yyy>QQQw do you do?</yyy>
<zzz>How do you do?</zzz>
(2)
In the second argument brackets can be used to create substring which can be referred to in the third
argument using "$number" syntax. If the number following "$" in the third argument is higher then
number of brackets in the second one an empty string is used as the replacement. "$0" refers to the
whole matched string.
Input:
<aaa>111-222-33</aaa>
<xsl:template match="/aaa">
<xxx>
<xsl:value-of select="replace(.,'(\d+)-(\d+)-(\d+)','$3$2::$1')"/>
</xxx>
<yyy>
<xsl:value-of select="replace(.,'(\d+)-\d+-\d+','$3$2::$1')"/>
</yyy>
<zzz>
<xsl:value-of select="replace(.,'(\d+)-(\d+)-(\d+)','$0 --- $3/$1')"/>
</zzz>
</xsl:template>
Output:
<xxx>33222::111</xxx>
<yyy>::111</yyy>
<zzz>111-222-33 --- 33/111</zzz>
(3)
Characters which have special meaning in the second or third argument must be escaped using
backslash "\".
Input:
<aaa>111*222?33</aaa>
<xsl:template match="/aaa">
<xxx>
<xsl:value-of select="replace(.,'(\d+)\*(\d+)\?(\d+)','$2')"/>
</xxx>
<yyy>
<xsl:value-of select="replace(.,'(\d+).*(\d+)\S?(\d+)','\$$2')"/>
</yyy>
<zzz>
<xsl:value-of select="replace(.,'(\d+).*?(\d+).*?(\d+)','\$$1\\\$$2\\\\\$$3')"/>
</zzz>
</xsl:template>
Output:
<xxx>222</xxx>
<yyy>$3</yyy>
<zzz>$111\$222\\$33</zzz>
32.tokenize()
Description—Function tokenize() creates text nodes from a string submitted as the first argument.
The second argument is a pattern which specifies separator between individual substrings. The
separators are not returned.
Input:
<aaa>How do you do</aaa>
<xsl:template match="/aaa">
<ul>
<xsl:for-each select="tokenize(.,'\s+')">
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</xsl:template>
Output:
<ul>
<li>How</li>
<li>do</li>
<li>you</li>
<li>do</li>
</ul>
Regular Expression
XPath 2.0 offers following three new functions that use regular expressions:
tokenize()
matches()
replace()
\n A newline character.
. Any single character except \n.
[a-f] The lower-case letters a, f, or anything in that range.
\d Any numeric digit. The same as [0-9].
\s Any single whitespace character—a tab, carriage return linefeed, or spacebar space.
* After any of the symbols shown above, this means "zero or more characters fitting this description"
+ Like the asterisk, but meaning "one or more characters fitting this description."
? Like the asterisk, but meaning "zero or one character fitting this description."
{4} Like the asterisk, but meaning "four characters fitting this description." Because curly braces are
used in XSLT stylesheets to show which parts of an attribute value template are expressions to be
evaluated, be careful when using these in a regular expression specified in an attribute value:
escape the curly braces by repeating them (in this case, {{4}}) to tell an XSLT 2.0 processor not to
treat the curly braces as attribute value template expression delimiters.
\+ A literal plus sign. The backslash character escapes the character after it, telling the processor not
to treat it as a special regular expression character.
Important URLs
http://kodlib.info/xsltckbk2-CHP-1-SECT-1.shtml
http://kodlib.info/xsltckbk2-CHP-1-SECT-2.shtml
http://www.xml.com/pub/a/2003/05/07/tr.html
http://nwalsh.com/docs/tutorials/xsl/xsl/slides.html#conflict
http://kodlib.info/xsltckbk2-CHP-6-SECT-7.shtml
http://www.xml.com/pub/a/2003/06/04/tr.html
http://saxon.sourceforge.net/saxon7.5/xsl-elements.html
http://saxon.sourceforge.net/saxon7.5/expressions.html
http://www.dpawson.co.uk/xsl/sect2/N6099.html
http://www.xmlplease.com/xmltraining/xslt-by-example/examples/for-each-group_1.html
http://www.w3.org/TR/xslt20/#regex-examples
http://www.xmlplease.com/xmltraining/xslt-by-example/xslt-by-example.html
http://www.regexbuddy.com/regex.html
http://manual.macromates.com/en/regular_expressions
http://zvon.org/
http://mulberrytech.com/quickref/
http://www.xmlplease.com/collection
OTHER
2. <xsl:value-of select="1,2,3,4"/> will generate 1 2 3 4 if you see an space is inserted between each of
the value. And <xsl:value-of select="bbb[3],bbb[1],bbb[2]"/> if input xml is
<bbb>b1</bbb><bbb>b2</bbb><bbb>b3</bbb> then output will be b1 b2 b3.
5. Multidimensional for…in...return
<xsl:value-of select="for $a in (1 to 3), $b in (1,10,100) return $a * $b"/>
Output: 1 10 100 2 20 200 ..etc.
<xsl:value-of select="for $a in ('a','b'), $b in ('c','d'), $c in ('e','f','g') return concat($a,$b,$c)"/>
Output: ace acf acg ade adf adg bce bcf bcg bde bdf bdg