Vous êtes sur la page 1sur 52

XPATH CORE FUNCTIONS:

The XPath Core Functions are also referred to as the Inherited XPath Functions. These twenty seven (27) functions
are defined in the W3C XML Path Language (XPath) Recommendation Version 1.0 which was released on
16 November 1999. All of these function are very utilitarian in nature.

Note that there are an additional nine (9) XSLT functions which are defined in the W3C XSL Transformations
(XSLT) Recommendation Version 1.0 which was also released on 16 November 1999. See the Function Index
for access to the individual XSLT functions.

Note that these function names are used as is, without a prefix. (Some companies offer proprietary XPath functions
that usually require a prefixed name.)

boolean
The Boolean function converts the value argument to a Boolean and returns a true or false.

Syntax: true | false = boolean(value)

ceiling
The ceiling function returns the smallest integer that is equal to or is larger than the numeric value of the
number argument.

Syntax: number = ceiling(number)

concat
The concat function takes all of the individual arguments, concatenates them together in order, and returns the
resultant string.

Syntax: string = concat(value1, value2, ...)

contains
The contains function determines if the substring argument is contained within the value argument and, if yes,
returns a true. If no, a false is returned.

Syntax: true | false = contains(value, substring)

count
The count function counts and returns the number of nodes in a node-set.

Syntax: number = count(node-set)

false
The false function return the Boolean value of false. There are no arguments.

Syntax: false = false()

floor
The floor function returns the largest integer that is equal to or is smaller than the numeric value of the number
argument.

Syntax: number = floor(number)

id
The id function returns a node-set containing zero or more nodes that have an attribute that match the value
argument.

Syntax: node-set = id(value)

lang
The lang function tests whether the language specified by the language argument matches the language of the
context node (which is normally assigned by the xsl:lang element). If yes, true is returned. If no, or if no
language is assigned to the context node, false is returned.

Syntax: true | false = lang(language)

last
The last function returns the position number assigned to the last node in the current node list that is being
processed by an xsl:for-each or xsl:apply-templates element. There are no arguments.

Syntax: number = last()

local-name
The local-name function returns the local part of a qname (or an empty string if there is no local name). A qname
is composed of an optional prefix, a colon that must be present if there is a prefix, followed by the local name. The
local name is dependent on node type. The node argument is optional. If omitted, the default is to use the context
node.

Syntax: string = local-name(node)

name
The name function returns the qname of the node. A qname is composed of an optional prefix, a colon that must
be present if there is a prefix, followed by the local name. The node argument is optional. If omitted, the default is
to use the context node.

Syntax: string = name(node)

namespace-uri
The namespace-uri function returns a string that is the namespace URI of the node cited in the optional node
argument. If the argument is omitted, the default is to find the namespace URI of the context node.

Syntax: uri = namespace-uri(node)

normalize-space
The normalize-space function returns a string in which all of the preceding and trailing white space has been
removed, and in which all internal sequences of white is replaced with one white space. The string argument is
optional. If omitted, the default is to use the string value of the context node.

Syntax: string = normalize-space(string)

not
The not function returns the Boolean negation of the condition argument (true becomes false and vice-versa).

Syntax: true | false = not(condition)

number
The number function converts the value argument to a number. The value argument is optional. If omitted, the
default is to use the string value of the context node.

Syntax: number = number(value)

position
The position function returns the position number in the current node list of the node that is currently being
processed by an xsl:for-each or xsl:apply-templates element. There are no arguments.

Syntax: number = position()

round
The round function rounds a number to its closest integer.

Syntax: integer = round(number)

starts-with
The starts-with function tests whether the string specified in the string argument starts with the substring
specified in the substring argument. If yes, true is returned. If no, a false is returned.

Syntax: true | false = starts-with(string, substring)

string
The string function converts the value argument to a string. The value argument is optional. If omitted, the
default is to use the string value of the context node.

Syntax: = string()

string-length
The string-length function returns the number of characters in a string. The string argument is optional. If
omitted, the default is to use the string value of the context node.

Syntax: number = string-length(string)

substring
The substring function returns a portion out of the string specified in the string argument as determined by the
starting point specified in the start argument and also by the optional length specified in the length argument.

Syntax: string = substring(string, start, length)

substring-after
The substring-after function returns a portion out of the string specified in the string argument that occurs after
the substring specified in the substring argument.

Syntax: string = substring-after(string, substring)

substring-before
The substring-before function returns a portion out of the string specified in the string argument that occurs
before the substring specified in the substring argument.

Syntax: string = substring-before(string, substring)

sum
The sum function adds and returns the total value of a set of numeric values that are contained in a node-set.
Syntax: number = sum(node-set)

translate
The translate function takes the string in the value argument, replaces all occurrences of a string specified in the
string1 argument with substitute characters specified in string2 argument and returns the modified string.

Syntax: string = translate(value, string1, string2)

true
The true function returns the Boolean value of true. There are no arguments.

Syntax: = true()

FUNCTION: current

node-set = current()

The XSLT current function returns a node-set that only contains the current node. The primary purpose of the
current function is to provide a means of accessing the current node when it is not the same as the context node.

Under most circumstances the current node and context node are the same. For example, when using the
xsl:value-of element, the current and context nodes are the same. However, when either the xsl:for-each or
xsl:apply-templates elements are used to process a selected set of nodes, as each individual node is processed
it temporarily becomes the current node.

The context node can always be accessed by using the dot (.) path expression. For example, <xsl:value-of
select="." /> will return the context node.

This function has no argument.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_current.xsl"?>
and we name it: xslt_example_current.xml

This example demonstrates how as each individual node is processed by an xsl:for-each element it temporarily
becomes the current node.

Code for xslt_example_current.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<html>
<body>
<xsl:variable name="staffname" select="devguru_staff/programmer/name" />
<xsl:for-each select="$staffname">
Current node: <xsl:value-of select="current()" />
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
ELEMENT: xsl:attribute

<xsl:attribute
name="qname"
namespace="URI"
>
</xsl:attribute>

The xsl:attribute element allows you to create an attribute node, define a value, and add it to the output. In
simple terms, you are creating a custom attribute whose value can be displayed.

After a child node has been added to an element, no additional attributes can be added to the parent node. This
restriction is required so that the XSL processor does not have to store the entire result tree in memory while
searching for additional attributes.

The related xsl:attribute-set can serve as a container for a collection of xsl:attribute elements.

This is not a self-closing element. The separate closing element is mandatory.

name="qname"

The mandatory name attribute is the qname of the attribute to be displayed in output. A qname is a qualified
name that is composed of an optional namespace prefix, a colon, which is only present if there is a prefix, and a
mandatory XML name (for example, xsl:zipcode or zipcode). The name is one of a very few attributes that can be
set to an expression that is computed at run-time. (Such attributes are interpreted as Attribute Value Templates.)
The syntax for doing this is demonstrated with the following code fragment:

<xsl:attribute name="{$att_name}" />

namespace="URI"

The optional namespace attribute is the namespace URI (Universal Resource Identifier) of the attribute. The
namespace is one of a very few attributes that can be set to an expression that is computed at run-time. (Such
attributes are interpreted as Attribute Value Templates.)

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_attribute.xsl"?>
and we name it: xslt_example_attribute.xml

In this example we create a staff_member element that contains the name of each staff member and a dob
attribute with the date of birth.

Code for xslt_example_stylesheet.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:element name="staff_member">
<xsl:attribute name="dob">
<xsl:value-of select="dob" />
</xsl:attribute>
<xsl:value-of select="name" />
</xsl:element>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:apply-templates

<xsl:apply-templates
select="expression"
mode="qname"
>
</xsl:apply-templates>

The xsl:apply-templates element defines a set of nodes to be processed, or by default selects all child nodes of
the current node being processed, and finds a matching template rule to apply to each node in the set. Since each
node in the node set is treated individually, it is possible for each node to have a different template applied to it.
Note that a template rule is not actually returned, but rather, it manifests itself by how the node is displayed in the
output.

There are only two possible procedures by which a template rule can be chosen for a node. If the node matches
the pattern defined by the match attribute of an xsl:template element, then that template will be applied. If
more that one such match occurs, then the template with the highest priority will be applied. Or if the priorities are
the same, the last template encountered with that priority will be applied. If there are no templates, or a match
cannot be found, then the XSLT processor will apply a built-in template rule.

The xsl:apply-templates element can only contain the xsl:sort or xsl:with-param elements. By default the
nodes will be assigned templates in the order that they occur. However, if there are one or more xsl:sort
instructions, then the nodes will be sorted before the templates are assigned. The actual assignment of a template
to a specific individual node is not dependent on the sorting order. The xsl:with-param element defines
parameters that will be applied to the template rules.

This is not a self-closing element. The separate closing element is mandatory.

mode="qname"

The optional mode attribute allows the same nodes to be processed more than once. Each time the nodes are
processed, they can be displayed in a different manner. A qname is a qualified name that is composed of an
optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML name (for
example, xsl:zipcode or zipcode). If an xsl:apply-templates element has a mode attribute, then it can only
apply template rules from xsl:template elements that also have a mode attribute.

select="expression"

The optional select attribute is set to an expression that returns a node set. This can simply be a string that is the
name of a node set. The nodes in the node set are processed in the order that they occur (which is called
document order). The default for omitting this attribute is to select all of the child nodes of the current node being
processed.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_applytemplates.xsl"?>
and we name it: xslt_example_applytemplates.xml
In this example we create three different templates and apply them in the desired order in a fourth template using
the xsl:apply-templates element.

Code for xslt_example_template.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="devguru_staff/programmer>
<html>
<body>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="dob" />
<xsl:apply-templates select="age" />
<br />
</body>
</html>
</xsl:template>

<xsl:template match="name">
<span style="font-size=22px;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

<xsl:template match="dob">
DOB: <span style="color:blue;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

<xsl:template match="age">
AGE: <span style="color:green;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

</xsl:stylesheet>

ELEMENT: xsl:attribute-set

<xsl:attribute-set
name="qname"
use-attribute-sets="qnames"
>
</xsl:attribute-set>

The xsl:attribute-set element defines and names a set containing zero or more xsl:attribute elements. Each of
the xsl:attribute elements are applied to the output in the order that they occur inside the xsl:attribute-set
element. An xsl:attribute element allows you to create an attribute node, define a value, and add it to the
output.
The xsl:attribute-set element can only be a child of the xsl:stylesheet or the xsl:transform elements.

The concept is that you can create a set of attributes that can be applied more than once by simply calling the
attribute set by name.

This is not a self-closing element. The separate closing element is mandatory.

name="qname"

The mandatory name attribute is the name of the attribute set. It must be a qname. A qname is a qualified name
that is composed of an optional namespace prefix, a colon, which is only present if there is a prefix, and a
mandatory XML name (for example, xsl:zipcode or zipcode).

use-attribute-sets="qnames"

The optional use-attribute-sets attribute is a white-space-delimited list of one or more qnames of attribute sets.
In other words, it is a collection of attribute set names. Each of the xsl:attribute elements in all of the attribute
sets are applied to the output in the order in which they occur in the use-attribute-sets attribute. This is done by
adding the use-attribute-sets attribute to the HTML tag or XSLT element using the following syntax:

<table xsl:use-attribute-sets="name">

<xsl:copy use-attribute-sets="name"> ... </xsl:copy>

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_attributeset.xsl"?>
and we name it: xslt_example_attributeset.xml

In this example we set the border, cellpadding, and cellspacing values for a table. We only apply the attribute set
to one table, but it can be applied to any number of tables to give a similar look.

Code for xslt_example_attributeset.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:attribute-set name="set_table">
<xsl:attribute name="border">
5
</xsl:attribute>
<xsl:attribute name="cellpadding">
15
</xsl:attribute>
<xsl:attribute name="cellspacing">
10
</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="/">
<html>
<body>
<table xsl:use-attribute-sets="set_table">
<xsl:for-each select="devguru_staff/programmer">
<tr>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="age" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

ELEMENT: xsl:call-template

<xsl:call-template
name="qname"
>
</xsl:call-template>

The xsl:call-template element is used to invoke a template by name. By invoke, we mean that the named
template is called and applied to the source document. If a template does not have a name, it cannot be called by
this element.

The xsl:template element is used to create a template. You can name a template by using the name attribute of
the xsl:template element. Further, the name called by the mandatory name attribute of the xsl:call-template
element must match the name specified by the name attribute of the xsl:template element. Complicating
matters is the fact that a template is not required to have a name. A template is only required to have either a
name or match attribute. (It can have both.) Ideally, each template will have a unique name. However, if a name
is repeated, then the two templates must have a different import precedence (refer to the xsl:import element),
otherwise it is an error.

An xsl:call-template element cannot directly return a result. You need to enclose the xsl:call-template element
inside an xsl:variable element which serves as the current output destination (see code example).

The xsl:call-template element can contain zero or more xsl:with-param elements. It cannot contain any other
XSLT elements. These xsl:with-param elements will only be evaluated if there is a matching xsl:param element
in the template being called. If there is no such match, then the xsl:with-param element is simply ignored.

This is not a self-closing element. The separate closing element is mandatory.

name="qname"

The mandatory name attribute is the unique qname of the template that you wish to invoke. A qname is a
qualified name that is composed of an optional namespace prefix, a colon which is only present if there is a prefix,
and a mandatory XML name (for example, xsl:zipcode or zipcode). This name must match the name used by the
name attribute of the xsl:template element that was used originally to create the template.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_calltemplate.xsl"?>
and we name it: xslt_example_calltemplate.xml

Code for xslt_example_calltemplate.xsl:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="hoo">
<html>
<body>
<xsl:call-template name="boo">
<xsl:with-param name="name" />
<xsl:with-param name="dob" />
</xsl:call-template>
</body>
</html>
</xsl:variable>

<xsl:template name="boo" match="/">


<xsl:param name="name" />
<xsl:param name="dob" />
<xsl:for-each select="devguru_staff/programmer">
<div>
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<hr />
</div>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

ELEMENT: xsl:choose

<xsl:choose>
<xsl:when test="expression"> ... </xsl:when>
...
<xsl:otherwise> ... </xsl:otherwise>
</xsl:choose>

The xsl:choose element is used to make a choice when there are two or more possible courses of action. It
provides a means for conducting multiple conditions testing.

The xsl:choose element must contain one or more xsl:when elements and can contain only one optional
xsl:otherwise element (which must occur after all of the xsl:when elements). If the xsl:choose element only
contains one xsl:when element, then for all practical purposes, it behaves just like the xsl:if element. When faced
with three or more choices, this element behaves like an if-then-else statement (or a Select Case) as found in
numerous other computer languages.

Each xsl:when element is examined in the order of occurrence. If and when the conditions of the test expression
are satisfied (returns True), the code contained in that element is executed. Then the xsl:choose element is
automatically exited and all further xsl:when elements are ignored and are not tested. The optional
xsl:otherwise element is also automatically ignored.

If none of the test conditions in any xsl:when element is satisfied (all return False), then the xsl:otherwise
element is automatically selected (if it is present) and the code associated with that element is executed. If there is
no xsl:otherwise element, then the xsl:choose element is exited.
This element has no attributes. It is not a self-closing tag. The separate closing element is mandatory.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_choose.xsl"?>
and we name it: xslt_example_choose.xml

This example assigns font color based upon age.

Code for xslt_example_choose.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:choose>
<xsl:when test="age &lt; 30">
<span style="color:red;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 40">
<span style="color:orange;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 50">
<span style="color:green;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 60">
<span style="color:blue;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:otherwise>
<span style="color:black;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:otherwise>
</xsl:choose>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:comment

<xsl:comment>
</xsl:comment>

The xsl:comment element is used to generate a comment in the output.


The purpose of a comment is simply to provide information that may be of interest or importance to human users
of the code. For example, you could list information about the program itself, such as the date of creation and
programmer names or catalog changes and corrections. Carefully placed comments can also be used to debug
code.

In one regard, the ability to comment in XSL is far more sophisticated than the ability to comment in other
languages such as HTML and VBScript. Like other elements in XSL, the xsl:comment element can make use of
extension functions and can contain functions. While this may be of limited use for a comment, you can do things
like include date-time stamps in your comment as demonstrated in this code fragment:

<xsl:comment>
<xsl:value-of select="Date:toString()" />
</xsl:comment>

This element has no attributes. It is not a self-closing tag. The separate closing element is mandatory.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_comment.xsl"?>
and we name it: xslt_example_comment.xml

In this example, we place a comment stating when and who wrote the code, and we list the names of the DevGuru
staff members.

Code for xslt_example_comment.xsl:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:comment>Written on 4/24/2001 by the Guru</xsl:comment>
<xsl:for-each select="devguru_staff/programmer">
NAME: <xsl:value-of select="name" />
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</stylesheet>

ELEMENT: xsl:copy-of

<xsl:copy-of
select="expression"
/>

The xsl:copy-of element inserts a duplicate copy of a node set or tree fragment into the output. Perhaps the most
important aspect of this element is that it allows you to insert multiple copies of the same set of nodes into
different places in the output. For example, you may wish to repeat a page header.

When a node set is copied, the nodes will be copied into the output in their order of occurrence in the source (this
is referred to as document order). Each node and all associated attribute nodes, namespace nodes, children and
descendants are copied. In other words, it is a complete, unabridged copy of the set.

When a root node is copied, all of the children and descendants are copied, but not the root node itself since there
can only be one root.

When a tree fragment is copied, it is copied exactly to the output.

For all other circumstances, the xsl:copy-of element behaves exactly like the xsl:value-of element. The value
being copied is converted into a string for display in the output.

In contrast, the xsl:copy element copies the current node in the source document to the output. The copy has the
same name, namespace, and type as the original node, but any attributes, children, and other descendants are not
copied.

This is a self-closing element and it cannot contain any child elements or any content.

select="expression"

The mandatory select attribute is an expression that identifies which nodes are to be copied.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_copyof.xsl"?>
and we name it: xslt_example_copyof.xml

In this example, we repeat the table header in two different tables.

Code for xslt_example_copyof.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:variable name="staff_table">
<tr>
<td><b>Name</b></td>
<td><b>Age</b></td>
</tr>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:copy-of select="$staff_table" />
<xsl:for-each select="devguru_staff/programmer">
<tr>
<xsl:if test="age &lt; 35">
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="age" /></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
<p />
<table>
<xsl:copy-of select="$staff_table" />
<xsl:for-each select="devguru_staff/programmer">
<tr>
<xsl:if test="age &gt; 34">
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="age" /></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:copy

<xsl:copy
use-attribute-sets="name-list"
>
</xsl:copy>

The xsl:copy element copies the current node in the source document to the output. The copy has the same
name, namespace, and type as the original node, but any attributes, children, and other descendants are not
copied. (Note that you can apply this element recursively to copy attributes and children.)

The xsl:copy-of element can be used to copy a node set. Children are copied with this element.

A common use of this element is to make identity transformations.

use-attribute-sets="name-list"

The optional use-attribute-sets attribute is a white-space delimited list of attribute sets that are to be applied to
the generated node. This is essentially a list of qnames. A qname is a qualified name that is composed of an
optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML name (for
example, xsl:zipcode or zipcode). The use-attribute-sets attribute performs the same purpose as the
xsl:attribute element.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_copy.xsl"?>
and we name it: xslt_example_copy.xml

For our example, we display the standard code for making an identity transformation.

Code for xslt_example_copy.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:decimal-format

<xsl:decimal-format
decimal-separator="character"
digit="character"
grouping-separator="character"
infinity="string"
minus-sign="character"
name="qname"
NaN="string"
pattern-separator="character"
percent="character"
per-mille="character"
zero-digit="character"
/>

The xsl:decimal-format element defines the symbols and characters used by the format-number function to
convert numbers to strings.

This element can be used more than once, but with certain limitations. Each element can have an optional name
value assigned to it by using by the name attribute. However, you cannot repeat name values. Further, you can
only omit the name attribute once.

This element does not effect the behavior of the xsl:number and the xsl:value-of elements when they are used
to format a number for display in the output. Nor does it effect the string function which has a default procedure
for converting numbers to strings.

The xsl:decimal-format element can only be a child of the xsl:stylesheet or the xsl:transform elements.

This is a self-closing element and it cannot contain any child elements or any content.

decimal-separator="character"

The optional decimal-separator attribute defines what character is used to separate the integer and fraction part
of a number. The default is a dot (.).

digit="character"

The optional digit attribute defines what character is used to signify that a digit is needed in a format pattern. The
default is the hash mark (#).

grouping-separator="character"

The optional grouping-separator attribute defines what character is used to separate groups of digits (for
example: 1,763,920). The default is a comma (,).

infinity="string"

The optional infinity attribute defines what string is used to represent that the value is infinite. The default is the
string, "infinity".

minus-sign="character"

The optional minus-sign attribute defines what character is used to represent a minus sign. The default is the
hyphen (-).

name="qname"
The optional name attribute assigns a qname to the element. If the name attribute is omitted, then you are
declaring that this is the default xsl:decimal-format element. You cannot repeat names. Note that this name can
be used as the optional third argument to the format-number function.

NaN="string"

The optional NaN attribute defines what string is used to indicate that the value is not a number. The default is
the string, "NaN".

pattern-separator="character"

The optional pattern-separator attribute defines what character is used to separate positive and negative sub-
patterns in a format pattern. The default is the semicolon (;).

percent="character"

The optional percent attribute defines what character is used to represent a percent sign. The default is the
percent sign (%).

per-mille="character"

The optional per-mille attribute defines what character is used to represent the per thousand sign. The default is
the Unicode per mille character (‰).

zero-digit="character"

The optional zero-digit attribute defines what character is used to represent the digit zero. The default is (0).

This code fragment shows how to format for European currency.

<xsl:decimal-format name="euro_currency" decimal-separator="," grouping-separator=".">

In this second example, we define "D" to represent a digit and display several numeric format variations.

Code for xslt_example_decimalformat.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:decimal-format name="staff" digit="D" />
<xsl:template match="/">
<html>
<body>
<xsl:value-of select='format-number(123456789, "#.000000000")' />
<br />
<xsl:value-of select='format-number(123456789, "#.0")' />
<br />
<xsl:value-of select='format-number(0.123456789, "##%")' />
<br />
<xsl:value-of select='format-number(123456789, "################")' />
<br />
<xsl:value-of select='format-number(123456789, "D.0", "staff")' />
<br />
<xsl:value-of select='format-number(123456789, "$DDD,DDD,DDD.DD", "staff")' />
<br />
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:element

<xsl:element
name="element-name"
namespace="URI"
use-attribute-sets="qname"
>
</xsl:element>

The xsl:element element is used to create and name an element (node) that can appear in the output. This
ability to create both custom elements and attributes, and to display the results, is a major reason why stylesheets
generated by XSL are a very sophisticated approach to displaying XML data.

There are two ways to add attributes to a created element: by using the xsl:attribute, xsl:copy, and the
xsl:copy-of elements or, by using the optional use-attribute-sets attribute of the xsl:element element. Any
attributes created using the use-attribute-sets attribute that have the same name as attributes created using the
xsl:attribute, xsl:copy, and the xsl:copy-of elements, will be overwritten.

There is a firm restriction regarding the addition of attributes. After a child node has been added to an element, no
additional attributes can be added to the parent node. This rule is required so that the XSL processor does not
have to store the result tree in memory. Otherwise, if attributes could be added at a later time, the result tree
would have to be stored while the XSL processor searched the entire tree for additional attributes.

Note, you can use the xsl:copy element to copy a node from the source document.

This element is not self-closing. The separate closing element is mandatory.

name="element-name"

The mandatory name attribute is the qname of the element to be created. A qname is a qualified name that is
composed of an optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML
name (for example, xsl:zipcode or zipcode). The name is one of a very few attributes in XSLT that can be set to an
expression that is computed at run-time. Such attributes are interpreted as Attribute Value Templates. The syntax
for doing this is demonstrated with the following code fragment:

<xsl:element name="{$el_name}" />

namespace="URI"

The optional namespace attribute is the namespace URI (Universal Resource Identifier) of the generated element.
The namespace is one of a very few attributes that can be set to an expression that is computed at run-time.
(Such attributes are interpreted as Attribute Value Templates.)

use-attribute-sets="qname"

The optional use-attribute-sets attributes allows the addition of attributes to the created element. It is
composed of a white space delimited list (set) of the attributes to be added. This is an alternative to using the
xsl:attribute element.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_element.xsl"?>
and we name it: xslt_example_element.xml

In this example we create a staff_member element that contains the name of each staff member.

Code for xslt_example_stylesheet.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:element name="staff_member">
<xsl:value-of select="name" />
</xsl:element>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:fallback

<xsl:fallback>
</xsl:fallback>

The xsl:fallback element is designed to provide fallback code that can be run as an alternative when an XSLT
processor fails to support an element.

Normally, when an XSLT element is not recognized by the XSLT processor an error occurs. The concept behind the
xsl:fallback element is to provide a mechanism that will allow a procedure to keep running whenever an XSLT
element is encountered that is not supported by an XSLT processor. For example, view these Microsoft
proprietary elements. Undoubtedly, as XSLT becomes more mainstream, additional proprietary elements will
become more common. A similar problem may also arise as newer and newer versions of XSLT become available
and various elements are added or removed from the standard.

In the future, it may become commonplace to use the xsl:fallback element as a means of insuring portability
between different browsers and XSLT processors.

This element has no attributes.

This is not a self-closing element. The separate closing element is mandatory.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_fallback.xsl"?>
and we name it: xslt_example_fallback.xml

We try to use the xsl:list-name element in XML version 5.0.

Code for xslt_example_fallback.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="5.0">


<xsl:template match="/">
<html>
<body>
<xsl:list-name>
<xsl:fallback>
<xsl:for-each select="devguru_staff/programmer">
NAME: <xsl:value-of select="name" />
<br />
</xsl:for-each>
</xsl:fallback>
</xsl:list-name>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:for-each

<xsl:for-each
select="expression"
/>
</xsl:for-each>

The xsl:for-each element loops through each node in a node set in itsr order of occurrence and applies the same
template to each node. A node set is simply the collection of all of the same XML tags (nodes) in an XML file. This
process is also referred to as iterating over a set of nodes. The template is contained inside the xsl:for-each
element between the opening and closing element. The syntax is:

<xsl:for-each>
code ...
</xsl:for-each>

In a formal definition, the template is said to be instantiated once to each node in the node set.

The default is to iterate through the nodes in the order in which they occur. This is referred to as document order.
However, you may prefer to rearrange the order by using the xsl:sort element. This element defines the sort key
upon which the sort (reorder) will be based.

This element is not self-closing. The separate closing element is mandatory.

select="expression"

The mandatory select attribute provides an expression that specifies which node set is to be processed by the
loop. This can simply be a string that is the name of the node. Only one node set can be defined. However, by
selecting a parent node, you can access the values of any child node via the template. You can use the position
function to return the position number of the node currently being processed (the numbering starts at one). You
can use the last function to return the total number of nodes being processed.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_foreach.xsl"?>
and we rename it: xslt_example_foreach.xml

Code for xslt_example_foreach.xsl:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<br />
AGE: <xsl:value-of select="age" />
<br />
ADDRESS: <xsl:value-of select="address" />
<br />
PHONE: <xsl:value-of select="phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:if

<xsl:if
test="expression"
>
</xsl:if>

The xsl:if element evaluates an expression which returns a Boolean result to determine if a template should be
instantiated. The evaluation is a simple True or False test on a defined condition or a set of conditions. If the test
returns True, the template is applied and the results are displayed in the output. If False, the template is not
applied (i.e., the contents between the opening and closing xsl:if are skipped over).

The actions of the xsl:if element are analogous to the if statement found in many other computer languages, such
as in C and VBScript. However, there is no else statement. Therefore, if you need to choose from two or more
possible courses of action, you may prefer to use the xsl:choose element.

One possible use of this element is to test for errors. The xsl:message element can be used to display error
messages.

This is not a self-closing element. The separate closing element is mandatory.

test="expression"

The mandatory test attribute is set to an expression that is a conditional test with either a True or False answer.
The expression can contain more than one test condition by using the and, not, and or operators.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_if.xsl"?>
and we name it: xslt_example_if.xml
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:template match="/">
<html>
<body>
The DevGuru Staff Members are
<xsl:for-each select="devguru_staff/programmer">
<xsl:value-of select="name" />
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()-1">
<xsl:text> and </xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:import

<xsl:import
href="uri"
/>

The xsl:import element is used to add (import) one stylesheet to another. You can repeat this element in order to
import more than one stylesheet.

The definitions and template rules of the importing stylesheet are treated as having greater importance that the
definitions and template rules of the imported stylesheet. Using a formal definition, the importing stylesheet is said
to have a greater "import precedence" over the imported stylesheet.

One very important aspect of the xsl:import element is that it allows you to access a library of reusable
stylesheets to create a desired appearance for the output.

The xsl:import element can only be a child of the xsl:stylesheet or the xsl:transform elements. Further, the
xsl:import element must be the first element to occur after the xsl:stylesheet element (for example, before any
xsl:include elements). If there is more than one stylesheet being imported, the one that is imported first has a
lower import precedence than the one that is imported second, the second has lower import precedence than the
third, and so on.

The similar xsl:include element can also be used to add (include) a stylesheet to another, but the definitions and
template rules of the included stylesheet are treated equal to the definitions and template rules of the including
stylesheet.

The xsl:apply-imports element is used to invoke (apply) any definitions and template rules of the imported
stylesheet that have been overridden by the importing stylesheet.
A stylesheet should not import itself.

This is a self-closing element and it cannot contain any child elements or any content.

href="uri"

The mandatory href attribute is either a relative or absolute URI (Uniform Resource Identifier) address of a valid
stylesheet to be imported. A relative URI is resolved against the base URI of the XML source document.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_import.xsl"?>
and we name it: xslt_example_import.xml

We import and apply: xslt_example_choose.xsl


(will appear in separate window - use "view source" to see code)

Code for xslt_example_import.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:import href="xslt_example_choose.xsl" />
<xsl:template match="/">
<html>
<body>
<xsl:apply-imports />
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:include

<xsl:include
href="uri"
/>

The xsl:include element is used to add (include) a stylesheet to another. You can repeat this element in order to
include more than one stylesheet.

The definitions and template rules of the included stylesheet are treated equal to the definitions and template rules
of the including stylesheet. The effect is the same as if the included stylesheet was actually part of the stylesheet
with which it is being included.

An analogy can be made about this element with regard to the #include used in the C computer language.

The similar xsl:import element is also used to add (import) a stylesheet to another, but the definitions and
template rules of the importing stylesheet are treated as having greater importance than the definitions and
template rules of the imported stylesheet.

The xsl:include element can only occur as a child of the xsl:stylesheet element. All xsl:include elements must
occur after all xsl:import elements.

A stylesheet cannot include itself, either directly or indirectly. Nor should a stylesheet be included more than once,
either directly or indirectly. Both situations may cause errors due to duplication.
This is a self-closing element and it cannot contain any child elements or any content.

href="uri"

The mandatory href attribute is either a relative or absolute URI (Uniform Resource Identifier) address of a valid
stylesheet to be included. A relative URI is resolved against the base URI of the XML source document.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_include.xsl"?>
and we name it: xslt_example_include.xml

We include: xslt_example_template.xsl
(will appear in separate window - use "view source" to see code)

Code for xslt_example_include.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:include href="xslt_example_template.xsl" />
<xsl:template match="devguru_staff/programmer">
<html>
<body>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="dob" />
<xsl:apply-templates select="age" />
<br />
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:key

<xsl:key
match="pattern"
name="qname"
use="expression"
>
</xsl:key>

The xsl:key element is used to declare a named key that can be used by the key function in expressions and
patterns. The key function has two arguments: the key name and the value (consider this to be a key-value pair).
The appropriate use of these key-value pairs can permit easy access to information in complex XML documents.

A key does not have to be unique. Further, a key can refer to more than one node and a node can have more than
one key.

There is no limit to the number of the xsl:key elements that can occur. However, each xsl:key element can only
be a child of the xsl:stylesheet or the xsl:transform elements. It cannot contain any elements as content, nor
can it appear inside a template.

The xsl:key element alerts the XSLT processor to create an indexed data structure for the key expressions ahead
of time. This indexed data structure will be used by the key function. If there are no xsl:key elements, then the
time-consuming indexing is not performed.

This is not a self-closing element. The separate closing element is mandatory.

match="pattern"

The mandatory match attribute defines the nodes to which the key will be applied.

name="qname"

The mandatory name attribute is a qname that identifies the key. A qname is a qualified name that is composed of
an optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML name (for
example, xsl:zipcode or zipcode).

use="expression"

The mandatory use attribute is an expression or string that is used to determine the value of the key for each
node.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_key.xsl"?>
and we name it: xslt_example_key.xml

We search for Road Runner.

Code for xslt_example_key.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:key name="stafflist" match="programmer" use="@name" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('stafflist', 'Road Runner')">
<div>
NAME: <xsl:value-of select="@name" />
<br />
DOB: <xsl:value-of select="@dob" />
<br />
AGE: <xsl:value-of select="@age" />
<br />
ADDRESS: <xsl:value-of select="@address" />
<br />
PHONE: <xsl:value-of select="@phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:message
<xsl:message
terminates="yes" | "no"
>
</xsl:message>

The xsl:message element is primarily used to report errors by displaying a text message and related information
of interest in the output.

This element can contain almost any other XSL element. For example, you can use the xsl:text element to add
literal text to the xsl:message element and you can use the xsl:value-of element to display values.

Exactly how the text contained in the xsl:message element (and any associated stylesheet error messages) will
be displayed in the output will be determined by the XSL processor.

This element also provides a mechanism that gives you the choice to quit the XSL processor (terminate the
process) when an error is encountered or to allow the process to continue (the default).

Alternatively, the xsl:comment element can be used to debug errors.

This is not a self-closing element. The separate closing element is mandatory.

terminates="yes" | "no"

The optional terminates attribute provides the choice of quitting the process or continuing the process if an error
is encountered. If set to no, the error in the stylesheet is reported and the process continues. This is the default. If
set to yes, the error in the stylesheet is reported and the process terminates.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_message.xsl"?>
and we name it: xslt_example_message.xml

In this example, we test to see if a phone number is the empty string. If yes, we exit and display a message.

Code for xslt_example_message.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
NAME: <xsl:value-of select="name" />
<br />
PHONE:
<xsl:if test="phone=''">
<xsl:message terminate="yes">
No phone number is provided
</xsl:message>
</xsl:if>
<xsl:value-of select="phone" />
<br />
<hr />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:namespace-alias

<xsl:namespace-alias
stylesheet-prefix="prefix"
result-prefix="prefix"
/>

The xsl:namespace-alias element is used to replace a namespace (prefix) in a stylesheet with a different
namespace (prefix) for use in the output. There must be one xsl:namespace-alias element for each namespace
you wish to change. In general, the primary use for this element is to convert non-XSLT elements (literal result
elements) into XSLT elements for display in the output. The literal result elements are said to be mapped to the
XSLT namespace.

A xsl:namespace-alias element can only be a child of the xsl:stylesheet or the xsl:transform elements.

This is a self-closing element and it cannot contain any child elements or any content.

stylesheet-prefix

The mandatory stylesheet-prefix attribute is the namespace (prefix) that you wish to change. If there is more
than one xsl:namespace-alias element that have the same value for the stylesheet-prefix attribute, the one
with the highest import precedence is selected. (If there is a tie on import precedence, an error occurs.)

result-prefix

The mandatory result-prefix attribute is the new namespace (prefix) that will appear in the output. For example,
if you are mapping literal result elements into the XSLT namespace, then this value will be: result-prefix="xsl"

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_namespacealias.xsl"?>
and we name it: xslt_example_namespacealias.xml

This stylesheet generates a very simple stylesheet where the guru prefix is converted to the xsl prefix.

Code for xslt_example_namespacealias.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:guru="guru.xsl">


<xsl:param name="var">name</xsl:param>
<xsl:param name="blank"></xsl:param>
<xsl:namespace-alias stylesheet-prefix="guru" result-prefix="xsl" />
<xsl:template match="/">
<guru:stylesheet version="1.0">
<guru:variable name="{$var}">
<guru:value-of select="{$blank}" />
</guru:variable>
</guru:stylesheet>
</xsl:template>
</xsl:stylesheet>
ELEMENT: xsl:number

<xsl:number
count="pattern"
format="{ string }"
from="pattern"
grouping-separator="{ character }"
grouping-size="{ number }"
lang="{ languagecode }"
letter-value={ "alphabetic" | "traditional" }
level="any" | "multiple" | "single"
value="expression"
>
</xsl:number>

The xsl:number element has two possible uses. It can determine the sequence number for the current node and
it can format a number for display in the output.

The sequence number is the integer position of the current node in a source document (source tree). There are
actually three ways that the sequence number can be determined and you can use the level attribute to choose
which way.

The formatting process converts either the sequence number or the number provided by the value attribute to a
string. By using various attributes of the xsl:number element, you can exert great control over the appearance of
the formatted number in the output.

In comparison, the xsl:decimal-format element defines the symbols and characters used by the format-
number function to convert numbers to strings.

This is a self-closing element and it cannot contain any child elements or any content.

count="pattern"

The optional count attribute dictates what nodes are to be counted. Only nodes that match the pattern are
counted. The default is to count any node that matches the pattern of the current node.

format="{ string }"

The optional format attribute is set to an Attribute Value Template that dictates the output format for the number.
You can use any the following choices:
Format Output Sequence

1 1 2 3 4 ... 10 11 12 ...

01 01 02 03 04 ... 10 11 12 ...

a a b c ... z aa ab ac ...

A A B C ... Z AA AB AC ...

i i ii iii iv v vi vii viii ix x ...

I I II II IV V VI VII VIII IX X ...


For example, format="1.A.a. " would allow the appropriate paragraph to be numbered 6.C.b.

from="pattern"

The optional from attribute specifies a starting point from which the sequential counting will start. The nodes are
matched to the pattern to find the starting point.

grouping-separator="{ character }"

The optional grouping-separator attribute is set to an Attribute Value Template or a character that dictates what
character is to be used to separate groups of digits in the number being displayed in the output. White-space is
permitted. The grouping-separator attribute is ignored if you do not also set the grouping-size attribute to the
digit group size. The classic example is to use a comma to separate groups of three digits.

grouping-size="{ number }"

The optional grouping-size attribute is set to an Attribute Value Template or a number that dictates how many
digits are in the groups that are being separated by the character specified in the grouping-separator attribute.
The grouping-size attribute is ignored if you do not also use the grouping-separator attribute to specify a
separator. The classic example are groups of three digits separated by a comma.

lang="{ languagecode }"

The optional lang attribute is set to an Attribute Value Template or a string that dictates the language code which
in turn specifies the language alphabet to be used for the numbering. The default language is set by the operating
system environment.

letter-value="{ alphabetic | traditional }"

The optional letter-value attribute is set to an Attribute Value Template that dictates whether the numbering
sequence in the selected language is either alphabetic or traditional. Or it can be set directly to either one of the
two permitted values. The primary example is Traditional Hebrew versus Alphabetic Hebrew. The default is
alphabetic.

level="any" | "multiple" | "single"

The optional level attribute is set to an Attribute Value Template that controls how the sequence number is
assigned. Or it can be set directly to one of the three permitted values. There are only three permitted procedures
for determining a sequence number:
Level Effect

The default value. All preceding nodes that match the target node pattern are counted. Limitations
single imposed by the from and count attributes are obeyed. The sequence starts at one. If the current target
node has four preceding nodes, then the current target node will be given the sequence number of five.

First, a list is constructed of all ancestors of the current node. The list goes back to, but does not include,
the ancestor that matches the optional from attribute pattern. Next, for each node in the list that
multiple
matches the count pattern, the XSL processor maps how many preceding siblings of that node match
the count pattern. This hierarchic position information is provided in a composite sequence number list.

Starting from the current node, the XSL processor counts how many nodes match the count attribute
any
pattern. If a node specified in the optional from attribute is encountered, the counting process stops.
The sequence number is the number of nodes counted. This can be zero, if the current node does not
match the count pattern.

value="expression"

The optional value attribute is a user-provided number that is used in place of a sequence generated number. If
the expression generates a real number, it will be rounded and converted to an integer. The default is to use a
sequence number which is based upon the current node's position in the source document (source tree).

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_number.xsl"?>
and we name it: xslt_example_number.xml

Code for xslt_example_numbert.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<xsl:number value="position()" format="1. " />
<xsl:value-of select="name" />
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:otherwise

<xsl:choose>
<xsl:when test="expression"> ... </xsl:when>
...
<xsl:otherwise> ... <xsl:otherwise>
</xsl:choose>

The xsl:otherwise element is an optional child of the xsl:choose element.

The xsl:choose element is used to make a choice when there are two or more possible courses of action. It
provides a means for conducting multiple conditions testing.

The xsl:choose element must contain one or more xsl:when elements and can contain only one optional
xsl:otherwise element (which must occur after all of the xsl:when elements). If the xsl:choose element only
contains one xsl:when element, then for all practical purposes, it behaves just like the xsl:if element. When faced
with three or more choices, these elements behave similar to an if-then-else statement (or a Select Case) as found
in numerous other computer languages.

The purpose of the xsl:when element is to contain a Boolean expression that can be tested. The test must return
a value of either true or false. Each xsl:when element is examined in the order of occurrence. If and when the
conditions of the test expression are satisfied (returns True), the code contained in that element is executed. Then
the xsl:choose element is automatically exited and all further xsl:when elements are ignored and they are not
tested. In this case, the optional xsl:otherwise element is also automatically ignored.

If none of the test conditions in any xsl:when element is satisfied (all return False), then the xsl:otherwise
element is automatically selected (if it is present) and the code associated with that element is executed. If there is
no xsl:otherwise element, then the xsl:choose element is exited.

This element has no attributes. It is not a self-closing tag. The separate closing element is mandatory.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_otherwise.xsl"?>
and we name it: xslt_example_otherwise.xml

Code for xslt_example_otherwise.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:choose>
<xsl:when test="age &lt; 30">
<span style="color:red;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 40">
<span style="color:orange;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 50">
<span style="color:green;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 60">
<span style="color:blue;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:otherwise>
<span style="color:black;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:otherwise>
</xsl:choose>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:output
<xsl:output
cdata-section-elements="namelist"
doctype-public="string"
doctype-system="string"
encoding="string"
indent="yes" | "no"
media-type="mimetype"
method="html" | "name" | "text" | "xml"
omit-xml-declaration="yes" | "no"
standalone="yes" | "no"
version="version_number"
/>

The xsl:output element is used to define the format of the output created by the stylesheet. This is accomplished
by setting one or more of ten optional attributes. The most important of these ten attributes is the method
attribute which dictates if the type of output is HTML, text, or XML. The type of output, in turn, dictates which of
the other nine attributes can be applied to the output.

The following table defines which attributes can optionally be set for each of the three types of output. A dash
signifies that the attribute cannot effect the output.

Attribute HTML Text XML

cdata-section-elements - - YES

doctype-public YES - YES

doctype-system YES - YES

encoding YES YES YES

indent YES - YES

media-type YES YES YES

omit-xml-declaration - - YES

standalone - - YES

version YES - YES

You can have zero or more xsl:output elements:

• If there is more than one xsl:output element, the XSLT processor essentially combines the information.
• If more than one xsl:output element sets the same attribute, the element with the highest import
precedence will have its attribute selected.
• If more than one xsl:output element repeats an attribute and has the same highest import precedence,
either the last will be chosen or an error will be declared.
• If there is more than one cdata-section-elements attribute, all of the values in the name lists will
effectively be merged into one list.

The xsl:output element can only be a child of the xsl:stylesheet or the xsl:transform elements.
This is a self-closing element and it cannot contain any child elements or any content.

cdata-section-elements="namelist"

The optional cdata-section-elements attribute is set to a white-space delimited list of qnames (element names)
whose content is to be output in CDATA sections. CDATA sections permit the use of sequences of characters that
contain markup elements without violating the XML requirement to be well-formed (i.e., all tags and elements are
closed).

doctype-public="string"

The optional doctype-public attribute specifies the public identifiers that go in the document type declaration
(DTD). If the doctype-system attribute is not set, then the doctype-public attribute is ignored.

doctype-system="string"

The optional doctype-system attribute specifies the system identifiers that go in the document type declaration
(DTD). The DTD should go immediately after the XML declaration.

encoding="string"

The optional encoding attribute specifies the preferred character encoding which is used to encode sequences of
characters as sequences of bytes.

indent="yes" | "no"

The optional indent attribute specifies whether or not to indent. If set to yes, the XML and HTML outputs are step-
indented to make them more readable.

media-type="mimetype"

The optional media-type attribute sets the MIME type. The default is:
media-type="text/xml"

method="html" | "qname" | "text" | "xml"

The optional method attribute dictates the type of output. The three permitted values are HTML, text and XML.
(Some XSLT processors recognize a qname as being an acceptable value for this attribute, but it is not part of the
W3C standard.) The default is XML. However, if the first child element of the root node is the HTML <html> tag
and there are no preceding text nodes, then the default output type is set to HTML.

omit-xml-declaration="yes" | "no"

The optional omit-xml-declaration attribute dictates whether the XSLT processor should output an XML
declaration. The default is no and an XML declaration is output. If yes, there is no output.

standalone="yes" | "no"

The optional standalone attribute dictates whether the XSLT processor should output a standalone declaration.
Yes signifies that it will. No, the default, signifies that it will not.

version="version_number"
The optional version attribute provides the W3C version number for the output format. If the output is XML, the
default version is 1.0 (currently, the only X3C version). Or if the output type is HTML, the default version is 4.0.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_output.xsl"?>
and we name it: xslt_example_output.xml

In this example, we declare our output to be in HTML. (By default, the occurrence of the <html> tag in the code
signifies to the Microsoft XSLT processor we are using that the output is to be in HTML.)

Code for xslt_example_output.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:output method="html" version="4.0" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<br />
AGE: <xsl:value-of select="age" />
<br />
ADDRESS: <xsl:value-of select="address" />
<br />
PHONE: <xsl:value-of select="phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:param

<xsl:param
name="qname"
>
</xsl:param>

Or:

<xsl:param
name="qname"
select="expression"
/>

The xsl:param element is used to declare a local or global parameter and to give that parameter a name and a
default value. The default value will be used only if no other value is provided when the template is called.
The default value can be assigned by either the content of the xsl:param element or by the select attribute, but
not by both. Each parameter declaration requires a separate xsl:param element. Global parameters are declared
in the top level of the style sheet (as children of the xsl:stylesheet or xsl:transform elements). Local
parameters are declared by using the xsl:param element as a child of the xsl:template element.

The actual (explicit) value is set by using xsl:with-param element when the template is applied (invoked) by
either the xsl:apply-template or the xsl:call-template elements.

The xsl:variable element can also be used to declare local and global variables. The only real difference between
a variable and a parameter is how the value is assigned.

Like all XSLT elements, the xsl:param element must be closed (well-formed). If the select attribute is present,
then this element is self-closing. If the select attribute is not present, then this element is not self-closing and the
separate closing element is mandatory.

name="qname"

The mandatory name attribute is the qname of the expression. A qname is a qualified name that is composed of
an optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML name (for
example, xsl:zipcode or zipcode). Note the following rules concerning when two different parameters can have the
same name. (The same rules apply to the name attribute of the xsl:variable element.)

• A name can be repeated if one of the names is in an imported stylesheet and therefore has a lower import
precedence. Under these circumstances, the higher import precedence name will always have precedence.
• Two different parameters can have the same name if they can never occur within the same scope.
Therefore no ambiguity can occur (which would be an error).
• A local and global parameter can have the same name. However, when the local parameter is in scope, the
global parameter cannot be accessed.

If the name attribute is assigned, a select attribute is not assigned, and there is no content, then the named
parameter is set to be the empty string.

select="expression"

The optional select attribute is an expression that defines the parameter. If the select attribute is present, then
the xsl:param element cannot contain any content and is self-closing. If an expression is given, the data type
must be Boolean, node-set, number or string. If it is a (literal) string, the string must be enclosed within opening
and closing quotes and in turn, that default value must be enclosed again in opening and closing quotes. For
example:

<xsl:param name="car" select=" ' Ford ' " />


Or
<xsl:param name="car" select=' " Ford " ' />

If the name attribute is assigned, a select attribute is not assigned, and there is no content, then the named
parameter is set to be the empty string for the default value.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_param.xsl"?>
and we name it: xslt_example_param.xml

Code for xslt_example_param.xsl:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template name="hoo" match="/">


<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<xsl:call-template name="boo">
<xsl:with-param name="myname" select="name" />
<xsl:with-param name="mydob" select="dob" />
</xsl:call-template>
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template name="boo">
<xsl:param name="myname" select="'Not Available'" />
<xsl:param name="mydob" select="'Not Available'" />
<div>
NAME: <xsl:value-of select="$myname" />
<br />
DOB: <xsl:value-of select="$mydob" />
<hr />
</div>
</xsl:template>

</xsl:stylesheet>

ELEMENT: xsl:preserve-space

<xsl:preserve-space
elements="names"
/>

The xsl:preserve-space element is used to keep white-space only nodes in the output. Note that the default is to
leave white-space only nodes. Therefore, it is only necessary to use the xsl:preserve-space element when you
use the xsl:strip-space element and wish to insure that certain white-space nodes are not removed. By white-
space, we refer to carriage returns, line feeds, spaces and tabs. No text or numbers appear in the node.

The related xsl:strip-space element is used to remove white-space only nodes so that they do not appear in the
output.

This is a self-closing element and it cannot contain any child elements or any content.

elements="names"

The mandatory elements attribute is a white-space delimited list of the names of the elements in which all of the
white-space nodes must not be removed. You can also use generic names with wild cards.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_preservespace.xsl"?>
and we name it: xslt_example_preservespace.xml
In this example, we preserve white-space nodes for name and remove white nodes for phone and address.

Code for xslt_example_preservespace.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:strip-space elements="phone address" />
<xsl:preserve-space elements="name" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
<xsl:value-of select="name" />
<br />
<xsl:value-of select="dob" />
<br />
<xsl:value-of select="age" />
<br />
<xsl:value-of select="address" />
<br />
<xsl:value-of select="phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:processing-instruction

<xsl:processing-instruction
name="process-name"
>
</xsl:processing-instruction>

The xsl:processing-instruction element writes (generates) a processing instruction to the output. The content
of this element becomes the text of the processing instruction. The syntax is:

<xsl:processing-instruction name="pi_name">
processing instruction text goes here ...
</xsl:processing-instruction>

This text should not contain any character references. In other words, all characters must be directly represented
by the selected character encoding (i.e., an < cannot be represented by an &lt;).

This is not a self-closing element. The separate closing element is mandatory.

name="process-name"

The mandatory name attribute is the name of the processing instruction. The name cannot contain a colon (:).

Processing instructions are rarely used in XML. However, the W3C standard permits the application of a CSS
(Cascading Style Sheet) file to an XML document. This is accomplished with an <?xml_stylesheet ... ?> processing
instruction which is directly compariable to the HTML <link type="text/css" href="style.css> tag.

This code fragment demonstrates how to do this:

<xsl:processing-instruction name="xml-stylesheet">
type="text/xsl" href="style_rules.css" title="Large Print"
</xsl:processing-instruction>

This creates the following tag:

<?xml-stylesheet href="style_rules.css" type="text/css" title="Large Print"?>

ELEMENT: xsl:sort

<xsl:sort
case-order="upper-first" | "lower-first"
data-type="number" "qname" | "text"
lang="language-code"
order="ascending" | " descending"
select="expression"
>
</xsl:sort>

The xsl:sort element is used to define a sort key. This sort key determines the order in which selected nodes are
processed by the xsl:for-each or xsl:apply-templates elements.

A sort can be based upon more than one xsl:sort element. Each sort is applied in the order in which it occurs.
Duplicate values are left in document order. After the first sort has reordered the nodes, the second sort is applied
to any nodes that had duplicate values in the first sort. The third sort is applied to any nodes that had duplicate
values in the second sort, and so on.

This is not a self-closing element. The separate closing element is mandatory.

case-order="upper-first" | "lower-first"

The optional case-order attribute dictates whether the sort will have upper or lower case letters listed first in the
sort. The default is to list upper case first.

data-type="number" | "qname" | "text"

The optional data-type attribute specifies the data type of the strings. There are only three permitted types:
Value Effect

number The sort key is converted to a number.

qname The sort is based upon a user-defined data type.

text The sort is alphabetic.

lang="language-code"
The optional lang attribute is set to an Attribute Value Template or a string that dictates the language code which
in turn specifies the language alphabet to be used for the sort. Clearly, the alphabet and numbers being used will
determine the sort order. The default language is set by the operating system environment.

order="ascending" | " descending"

The optional order attribute dictates whether the sort is in increasing or decreasing order. The default is
ascending.

select="expression"

The optional select attribute is an expression that defines the key upon which the sort will be based. The
expression is evaluated and converted to a string that is used as the sort key. If no select attribute is provided
(hence, no sort key), the selected nodes are sorted in document order.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_sort.xsl"?>
and we name it: xslt_example_sort.xml

Code for xslt_example_sort.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<xsl:sort data-type="number" select="age" order="descending" />
<xsl:value-of select="name" />
<xsl:text> - </xsl:text>
<xsl:value-of select="age" />
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:strip-space

<xsl:strip-space
elements="names"
/>

The xsl:strip-space element is used to remove white-space only nodes so that they do not appear in the output.
By white-space, we refer to carriage returns, line feeds, spaces and tabs. No text or numbers appear in the node.

The related xsl:preserve-space element is used to keep white-space only nodes in the output. Note that the
default is to leave white-space only nodes. Therefore, it is only necessary to use the xsl:preserve-space element
when you use the xsl:strip-space element and wish to insure that certain white-space nodes are not removed.

This is a self-closing element and it cannot contain any child elements or any content.

elements="names"
The mandatory elements attribute is a white-space delimited list of the names of the elements in which all of the
white-space nodes can be removed. You can also use generic names with wild cards. For example, elements="*"
removes all white-space nodes in all elements.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_stripspace.xsl"?>
and we name it: xslt_example_stripspace.xml

In this example, white-space phone and address nodes are removed.

Code for xslt_example_stripspace.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:strip-space elements="phone address" />
<xsl:preserve-space elements="name" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
<xsl:value-of select="name" />
<br />
<xsl:value-of select="dob" />
<br />
<xsl:value-of select="age" />
<br />
<xsl:value-of select="address" />
<br />
<xsl:value-of select="phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:stylesheet

<xsl:stylesheet
version="version_number"
id="unique_id"
exclude-result-prefixes="list"
extension-element-prefixes="list"
>
</xsl:stylesheet>

The xsl:stylesheet element contains all other XSLT elements and delimits the start and stop of the code in an .xsl
program. No other XSLT element can occur before the opening xsl:stylesheet element, nor may any other XSLT
element occur after the closing xsl:stylesheet element.

A direct comparison can be made between the opening and closing <html> ... </html> tags of the HTML
language, in that both the html tag and the xsl:stylesheet element serve as delimiting containers for all other
members of their respective languages.

An important duty performed by the xsl:stylesheet element is that it must contain the XSLT namespace
declaration. The purpose of the namespace declaration is to declare that the document is an XSLT stylesheet. Use
the following syntax:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

Older, proprietary versions of Microsoft XSLT used the following namespace:

xmlns:xsl="http://www.w3.org/TR/WD-xsl"

The xsl:stylesheet element is also commonly referred to as the root element of the stylesheet. Since it is the
root, there can be no other element that is a parent to the xsl:stylesheet element. All other XSLT elements are
either children, grandchildren, or further descendants. Only the following eight XSLT elements can be children:

 xsl:attribute-set
 xsl:import
 xsl:include
 xsl:output
 xsl:param
 xsl:script
 xsl:template
 xsl:variable

The xsl:transform element performs the exact same purpose as the xsl:stylesheet element. These two
elements may be used interchangeably. They are considered synonymous with each other.

This is not a self-closing element. The separate closing element is mandatory.

version="version_number"

The mandatory version attribute is set to the version number. Currently, the version number is "1.0".

id="unique_id"

The optional id attribute is a unique identifier for the stylesheet. This allows the stylesheet to be referenced in
another XML document.

exclude-result-prefixes="list"

The optional exclude-result-prefixes attribute is a list, delimited by white-space, of the namespaces prefixes
that should not be copied into the output (the result tree).

extension-element-prefixes="list"

The optional extension-element-prefixes attribute is a list, delimited by white-space, of the namespaces


prefixes used for extension elements.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_stylesheet.xsl"?>
and we name it: xslt_example_stylesheet.xml
Code for xslt_example_stylesheet.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<br />
AGE: <xsl:value-of select="age" />
<br />
ADDRESS: <xsl:value-of select="address" />
<br />
PHONE: <xsl:value-of select="phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:template

<xsl:template
match="pattern"
mode="qname"
name="qname"
priority="number"
>
</xsl:template>

The xsl:template element is used to define a template that can be applied to a node to produce a desired output
display.

There must be either a match or name attribute, or both, and this determines how the template rule can be
invoked. If there is only a match attribute, then you can use the xsl:apply-template element to invoke the
template rule defined by the xsl:template element. If there is only a name attribute, then you can use the
xsl:call-template element to invoke the named template defined by the xsl:template element. If both attributes
are present, then you may invoke the template by either procedure.

It is quite possible that more than one template can be applied to a node. The highest priority value template is
always chosen. If more than one suitable template has the same highest priority value, then the XSLT processor
usually chooses the one that appears last. Different templates can not have both the same name and priority
values. This is an error.

The xsl:template element is always a child of either the xsl:stylesheet or xsl:transform elements.

This is not a self-closing element. The separate closing element is mandatory.


match="pattern"

The match attribute is mandatory unless the element has a name attribute, then this attribute is optional. In
other words, there must be either a match or name attribute, or both. This attribute is a pattern that is used to
define which nodes will have which template rules applied to them.

mode="qname"

The optional mode attribute allows the same nodes to be processed more than once. Each time the nodes are
processed, they can be displayed in a different manner. A qname is a qualified name that is composed of an
optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML name (for
example, xsl:zipcode or zipcode). If an xsl:template element has a mode attribute, then it must also have a
match attribute. If the xsl:apply-templates element has a mode attribute, then it can only apply templates
from xsl:templates elements that also have a mode attribute. Likewise, if the xsl:apply-templates element
does not have a mode attribute, then it can only apply templates from xsl:templates elements that also do not
have a mode attribute.

name="qname"

The name attribute is mandatory unless the element has a match attribute, then this attribute is optional. In
other words, there must be either a match or name attribute, or both. A qname is a qualified name that is
composed of an optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML
name (for example, xsl:zipcode or zipcode). If the name attribute is present, you can use the xsl:call-template
element to invoke the template. To do this, the name attribute must be the same for both the xsl:template and
the xsl:call-template elements.

priority="number"

The optional priority attribute is a real number that ranges from -9.0 to 0.0 to 9.0 that sets the priority of
importance for a template. The higher the number, the higher the priority. If more than one template is suitable for
a node, then the highest priority template is always chosen. The most common default value is 0 (zero), however
the exact default value that will be assigned by the XSLT processor is dependent on the pattern information
contained in the match attribute.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_template.xsl"?>
and we name it: xslt_example_template.xml

In this example we create three different templates and apply them in the desired order in a fourth template using
the xsl:apply-templates element.

Code for xslt_example_template.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="devguru_staff/programmer">
<html>
<body>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="dob" />
<xsl:apply-templates select="age" />
<br />
</body>
</html>
</xsl:template>

<xsl:template match="name">
<span style="font-size=22px;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

<xsl:template match="dob">
DOB: <span style="color:blue;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

<xsl:template match="age">
AGE: <span style="color:green;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

</xsl:stylesheet>

ELEMENT: xsl:text

<xsl:text
disable-output-escaping="yes" | "no"
>
</xsl:text>

The xsl:text element is used to add literal text to the output. This element cannot contain any other XSL
elements. It can only contain text.

Normally, any text that occurs in a stylesheet will be copied to the output regardless if it is enclosed by an xsl:text
element. However, there are two primary reasons for using the xsl:text element:

 The first is to control white-space. Extra white-space is preserved by using this element. In other words, a text
will be displayed as it really is without any extra white-spaces being removed.

 The second is to handle special characters such as an ampersand (&) or a greater-than sign (>). For example,
you may wish a &gt; to be displayed as a >. This is referred to as controlling output escaping.

This is not a self-closing element. The separate closing element is mandatory.

disable-output-escaping="yes" | "no"

The optional disable-output-escaping attribute turns on or off the ability to escape special characters. If set to
no, a &gt; will appear as a &gt; in the text. If set to yes, a &gt; will appear as a >. (This attribute should be used
with care. Remember that XML requires a well-formed code in that all tags or elements must be closed. If set to
yes, you could accidentally create a non-well-formed document.)

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_text.xsl"?>
and we name it: xslt_example_text.xml

Code for xslt_example_text.xsl:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:template match="/">
<html>
<body>
<xsl:text>The DevGuru Staff Members are < /xsl:text>
<xsl:for-each select="devguru_staff/programmer">
<xsl:value-of select="name" />
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()-1">
<xsl:text> and </xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:transform

<xsl:transform
version="version_number"
id="unique_id"
exclude-result-prefixes="list"
extension-element-prefixes="list"
>
</xsl:transform>

The xsl:transform element contains all other XSLT elements and delimits the start and stop of the code in an .xsl
program. No other XSLT element can occur before the opening xsl:transform element, nor may any other XSLT
element occur after the closing xsl:transform element.

A direct comparison can be made between the opening and closing <html> ... </html> tags of the HTML
language, in that both the html tag and the xsl:transform element serve as delimiting containers for all other
members of their respective languages.

An important duty performed by the xsl:transform element is that it must contain the XSLT namespace
declaration. The purpose of the namespace declaration is to declare that the document is an XSLT stylesheet. Use
the following syntax:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

Older, proprietary versions of Microsoft XSLT used the following namespace:

xmlns:xsl="http://www.w3.org/TR/WD-xsl"

The xsl:transform element is also commonly referred to as the root element of the stylesheet. Since it is the root,
there can be no other element that is a parent to the xsl:transform element. All other XSLT elements are either
children, grandchildren, or further descendants. Only the following eight XSLT elements can be children:

 xsl:attribute-set
 xsl:import
 xsl:include
 xsl:output
 xsl:param
 xsl:script
 xsl:template
 xsl:variable

The xsl:stylesheet element performs the exact same purpose as the xsl:transform element. These two
elements may be used interchangeably. They are considered synonymous with each other.

This is not a self-closing element. The separate closing element is mandatory.

version="version_number"

The mandatory version attribute is set to the version number. Currently, the version number is "1.0".

id="unique_id"

The optional id attribute is a unique identifier for the stylesheet. This allows the stylesheet to be referenced in
another XML document.

exclude-result-prefixes="list"

The optional exclude-result-prefixes attribute is a list, delimited by white-space, of the namespaces prefixes
that should not be copied into the output (the result tree).

extension-element-prefixes="list"

The optional extension-element-prefixes attribute is a list, delimited by white-space, of the namespaces


prefixes used for extension elements.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_transform.xsl"?>
and we name it: xslt_example_transform.xml

Code for xslt_example_transform.xsl:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<br />
AGE: <xsl:value-of select="age" />
<br />
ADDRESS: <xsl:value-of select="address" />
<br />
PHONE: <xsl:value-of select="phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:transform>

ELEMENT: xsl:value-of

<xsl:value-of
select="expression"
disable-output-escaping="yes" | "no"
/>

The xsl:value-of element is used to write or display in the result tree a string representation of the value assigned
to a specified node. To explain it in another way, this XSLT element causes the value assigned to an XML tag to be
displayed as text in the HTML page that we create to display the information in our XML file. There is no limit to
the number of xsl:value-of elements that can appear in the code. However, each xsl:value-of element can only
have one node assigned to it and this is accomplished by using the mandatory select attribute. Remember that a
node can occur an unlimited number of times in an XML document. (For example, the <phone> ... </phone> xml
tags in our DevGuru Staff List can be used for each staff member.)

You can use the xsl:for-each element to access each occurrence of a specified node in the order in which they
occur in the document. As each occurrence is accessed, you can use the xsl:value-of element to display the value
of the current node. As an example, you can use the xsl:for-each element to access all of the phone numbers in
the <phone> ... </phone> XML tags in our DevGuru Staff List and then display the numbers using the xsl:value-
of element. Additionally, you can use the xsl:sort element to define a sorting key which can be used to sort
values.

You can also use the xsl:copy, xsl:copy-of, and xsl:text elements to display values as a string or text.

If the value is not a string, then the value will be converted to a string. (This is done automatically by calling the
String function.) If the value is a Boolean, then the string will display as "true" or "false". If the value is a node-
set, then only the first value of the first node in the set is displayed and the values of all other nodes in the set are
completely ignored.

This is a self-closing element and it cannot contain any child elements or any content.

select="expression"
The mandatory select attribute assigns the node (by name) to the element. Only one node can be assigned for
each occurrence of this element.

disable-output-escaping="yes" | "no"

The optional disable-output-escaping attribute specifies how special characters should appear in the output
string. A yes dictates that special characters should be displayed as is (i.e., a < or >). The default, no, dictates that
special characters should NOT be displayed as is (i.e., a < is displayed as &lt;).

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_valueof.xsl"?>
and we name it: xslt_example_valueof.xml

In this example, we use the xsl:for-each and xsl:value-of elements to display all of the values assigned to the
name, dob, age, address, and phone tags (nodes):

Code for xslt_example_valueof.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">


<xsl:template>
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<br />
AGE: <xsl:value-of select="age" />
<br />
ADDRESS: <xsl:value-of select="address" />
<br />
PHONE: <xsl:value-of select="phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:variable

<xsl:variable
name="qname"
>
</xsl:variable>

Or:

<xsl:variable
name="qname"
select="expression"
/>

The xsl:variable element is used to declare a local or global variable and to give that variable a name and a
value. The value can be assigned by either the content of the xsl:variable element or by the select attribute, but
not by both. Each variable declaration requires a separate xsl:variable element. Global variables are declared in
the top level of the style sheet (as children of the xsl:stylesheet or xsl:transform elements). Local variables are
declared within the template body.

Note that once you set a variable's value, there is no provision in the XSLT language to change or modify that
value. This is in sharp contrast to most other computer languages which allow you to repeatedly reassign variable
values.

The xsl:param element can also be used to declare parameters. The only real difference between a variable and a
parameter is how the value is assigned.

Like all XSLT elements, the xsl:variable element must be closed (well-formed). If the select attribute is present,
then this element is self-closing. If the select attribute is not present, then this element is not self-closing and the
separate closing element is mandatory.

name="qname"

The mandatory name attribute is the qname of the expression. A qname is a qualified name that is composed of
an optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML name (for
example, xsl:zipcode or zipcode). Note the following rules concerning when two different variables can have the
same name. (The same rules apply to the name attribute of the xsl:param element.)

• A name can be repeated if one of the names is in an imported stylesheet and therefore has a lower import
precedence. Under these circumstances, the higher import precedence name will always have precedence.
• Two different variables can have the same name if they can never occur within the same scope. Therefore
no ambiguity can occur (which would be an error).
• A local and global variable can have the same name. However, when the local variable is in scope, the
global variable cannot be accessed.

If the variable element contains no content and a select attribute has been not assigned, then the named
variable is set to be the empty string.

select="expression"

The optional select attribute is an expression that defines the variable. If the select attribute is present, then the
xsl:variable element cannot contain any content and is self-closing. If an expression is given, the data type must
be Boolean, node-set, number or string. If it is a literal string, the string must be enclosed within opening and
closing quotes and in turn, that value must be enclosed again in opening and closing quotes. For example:

<xsl:variable name="car" select=" ' Ford ' " />


Or
<xsl:variable name="car" select=' " Ford " ' />

If the name attribute is assigned, a select attribute is not assigned, and there is no content, then the named
variable is set to be the empty string.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_variable.xsl"?>
and we name it: xslt_example_variable.xml
We create a reuseable table header.

Code for xslt_example_variable.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:variable name="staff_table">
<tr>
<td><b>Name</b></td>
<td><b>Age</b></td>
</tr>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:copy-of select="$staff_table" />
<xsl:for-each select="devguru_staff/programmer">
<tr>
<xsl:if test="age &lt; 35">
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="age" /></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
<p />
<table>
<xsl:copy-of select="$staff_table" />
<xsl:for-each select="devguru_staff/programmer">
<tr>
<xsl:if test="age &gt; 34">
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="age" /></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:when

<xsl:choose>
<xsl:when test="expression"> ... </xsl:when>
...
<xsl:otherwise> ... </xsl:otherwise>
</xsl:choose>

The xsl:when element is a child of the xsl:choose element.


The xsl:choose element is used to make a choice when there are two or more possible courses of action. It
provides a means for conducting multiple condition testing.

The xsl:choose element must contain one or more xsl:when elements and can contain only one optional
xsl:otherwise element (which must occur after all of the xsl:when elements). If the xsl:choose element only
contains one xsl:when element, then for all practical purposes, it behaves just like the xsl:if element. When faced
with three or more choices, these elements behave similar to an if-then-else statement (or a Select Case) as found
in numerous other computer languages.

The purpose of xsl:when element is to contain a Boolean expression that can be tested. The test must return a
value of either true or false. Each xsl:when element is examined in the order of occurrence. If and when the
conditions of the test expression are satisfied (returns True), the code contained in that element is executed. Then
the xsl:choose element is automatically exited and all further xsl:when elements are ignored and they are not
tested. The optional xsl:otherwise element is also automatically ignored.

If none of the test conditions in any xsl:when element is satisfied (all return False), then the xsl:otherwise
element is automatically selected (if it is present) and the code associated with that element is executed. If there is
no xsl:otherwise element, then the xsl:choose element is exited.

This element is not a self-closing tag. The separate closing element is mandatory.

test="expression"

The mandatory test attribute is a Boolean expression that is the test condition to be satisfied. A True signifies that
the test conditions have been met. A False signifies that the test conditions have not been met. We use the
DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_when.xsl"?>
and we name it: xslt_example_when.xml

This example assigns font color based upon age.

Code for xslt_example_when.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:choose>
<xsl:when test="age &lt; 30">
<span style="color:red;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 40">
<span style="color:orange;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 50">
<span style="color:green;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age &lt; 60">
<span style="color:blue;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:otherwise>
<span style="color:black;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:otherwise>
</xsl:choose>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

ELEMENT: xsl:with-param

<xsl:with-param
name="qname"
>
</xsl:param>

Or:

<xsl:with-param
name="qname"
select="expression"
/>

The xsl:with-param element is used to set the explicit value of a named parameter when using the xsl:apply-
templates and the xsl:call-template elements.

The concept is that the xsl:param element is used to declare a local or global parameter by assigning a name and
a default value. The xsl:with-param element is used to set the actual (explicit) value which will be used in place
of the default value. The name cited by the xsl:with-param element must match a name in an xsl:param
element. If there is no such match, the xsl:with-param element is simply ignored, but it is not treated as an
error.

Like all XSLT elements, the xsl:with-param element must be closed (well-formed). If the select attribute is
present, then this element is self-closing. If the select attribute is not present, then this element is not self-closing
and the separate closing element is mandatory.

name="qname"

The mandatory name attribute is the qname of the expression. A qname is a qualified name that is composed of
an optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML name (for
example, xsl:zipcode or zipcode). Note the following rules concerning when two different parameters can have the
same name. (The same rules apply to the name attribute of the xsl:param element.)

• A name can be repeated if one of the names is in an imported stylesheet and therefore has a lower import
precedence. Under these circumstances, the higher import precedence name will always have precedence.
• Two different parameters can have the same name if they can never occur within the same scope.
Therefore no ambiguity can occur (which would be an error).
• A local and global parameter can have the same name. However, when the local parameter is in scope, the
global parameter cannot be accessed.

If the with-param element contains no content and a select attribute has been not assigned, then the named
parameter is set to be the empty string.

select="expression"

The optional select attribute is an expression that defines the parameter. If the select attribute is present, then
the xsl:with-param element cannot contain any content and is self-closing. If an expression is given, the data
type must be Boolean, node-set, number or string. If it is a (literal) string, the string must be enclosed within
opening and closing quotes and in turn, that default value must be enclosed again in opening and closing quotes.
For example:

<xsl:with-param name="car" select=" ' Ford ' " />


Or
<xsl:with-param name="car" select=' " Ford " ' />

If the name attribute is assigned, a select attribute is not assigned, and there is no content, then the named
parameter is set to be the empty string for the default value.

We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_withparam.xsl"?>
and we name it: xslt_example_withparam.xml

Code for xslt_example_withparam.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template name="hoo" match="/">


<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<xsl:call-template name="boo">
<xsl:with-param name="myname" select="name" />
<xsl:with-param name="mydob" select="dob" />
</xsl:call-template>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="boo">
<xsl:param name="myname" select="'Not Available'" />
<xsl:param name="mydob" select="'Not Available'" />
<div>
NAME: <xsl:value-of select="$myname" />
<br />
DOB: <xsl:value-of select="$mydob" />
<hr />
</div>
</xsl:template>

</xsl:stylesheet>

Vous aimerez peut-être aussi