Introduction

Every XSL stylesheet must start with xsl:stylesheet element. The atribute version='1.0' specifies version of XSL(T) specification.
This example shows the simplest possible stylesheet. As it does not contain any information, default processing is used.

Contents of the original elements can be recovered from the original sources in two basic ways. this stylesheet uses xsl:value-of construct. In this case the contents of the element is used without any further processing. The instruction
xsl:apply-templates in this stylesheet is different. The parser further processes selected elements, for which a template is defined.

Templates

Parts of XML document to which template should be applied are determined by location paths. The required syntax is specified
in the XPath specification. Simple cases looks very similar to filesystem addressing. ( this stylesheet )

Processing always starts with the template match="/" . This matches the root node (the node its only element child is the
document element, in our case "source"). Many stylesheets do not contain this element explicitly. When this template is not
explicitly given, the implicit template is used (it contains as the sole instruction). This instruction means: process all children of the current node, including text nodes. Compare
this stylesheet and this stylesheet . When a template for the node exists, there is no default processing invoked ( this stylesheet ). If you want to include descendants of the node, you have to explicitly request their templates ( this stylesheet ).

"//" is very common in location paths. When it is used at the beginning of a location path, it means: select all nodes in
the document of the specified type ( this stylesheet ). In the middle of a location path it means: select all nodes which appear in the node selected with the first part of the
location path ( this stylesheet ).

Quite often several templates match the same element in XML source. It must be therefore decided which one should be used.
This priority order can be specified with the priority attributte. If this attribute is not specified, its priority is calculated
according to several rules. this stylesheet and this stylesheet differ by priority of their templates. this stylesheet shows the default action in the absence of priority attributes. Template CCC has lower priority than CCC/CCC, as it is less
specific. Compare this stylesheet and this stylesheet . Template CCC has lower priority than both CCC/CCC or AAA/CCC/CCC, but the latest two have the same priority. In such a
case an XSLT processor may signal the error; if it does not signal an error, it must recover by choosing, from amongst the
matching template rules that are left, the one that occurs last in the stylesheet. In this stylesheet less specific "*" has lower priority than CCC. Computed priorities ranges from -0.5 to 0.5. XSLT specification gives more
details.

Axis child:: can be be omitted from a location step as it is the default axis. Axis attribute:: can be abbreviatet to @. //
is short for /descendant-or-self::, . is short for self:: and .. is short for parent::.

The xsl:copy element may have a use-attribute-sets attribute. In this way attributes for copied element can be specified.
this stylesheet does not work as expected (setting use-attribute-sets with name function)., because expresions in attributes that refer to
named XSLT objects are not evaluated.

Conditional processing

xsl:if instruction enables conditional processing. this stylesheet demonstrates a typical case of xsl:for-each usage, adding a text between individual entries. Very often you do not want to
add text after the last element. xsl-if construct comes handy here. ( this stylesheet )

Numbers generation and formatting

this stylesheet demonstrates the default behaviour of xsl:number element. Numbering of individual chapter elements depends on position of
the chapter element. Each level of chapters is numbered independently.
Setting the attribute level into multiple in
this stylesheet enables more natural numbering.

xsl:number inserts formated numbers into output. The format is given with format attribute. The attribute starts with format
identificator followed by separator characters. Study individual stylesheets to compare notation.

Variables

A stylesheet can contain several variables of the same name. this stylesheet demonstrates a way how to recover the value of global variable which has the same name as a local one. The this stylesheet demonstrates an incorrect approach. The value of local variable is bounded to xsl:when element. The rest of template therefore
sees only the global variable.

Parameters for a template can be passed with xsl:with-param element. If the template contains a xsl:param element with the
same name as name attribute of xsl:with-param, this value is used. this stylesheet shows a typical example. If you want to pass a variable, you have to define this variable with xsl:param element. Look at
this stylesheet for wrong approach.

A variable can hold a result tree fragment. The operations permitted on a result tree fragment are a subset of those permitted
on a node-set. An operation is permitted on a result tree fragment only if that operation would be permitted on a string (the
operation on the string may involve first converting the string to a number or boolean). In particular, it is not permitted
to use the /, //, and [] operators on result tree fragments. When a permitted operation is performed on a result tree fragment,
it is performed exactly as it would be on the equivalent node-set. Compare this stylesheet and this stylesheet .

Numeric calculations

Addition, subtraction and multiplication uses common syntax ( this stylesheet ). Division syntax is less usual. Slash / is used in patterns and so keyword div is used instead ( this stylesheet ).Operator mod returns the remainder from a truncating division. ( this stylesheet )

Function string() transforms its argument into string. This function is not usualy directly used in stylesheets as it is in
most cases called by default. this stylesheet shows examples of number to string conversions. Notice results of zero divisions.

Boolean functions

In this stylesheet strings are arguments of boolean() function. A string is true if and only if its length is non-zero. In this stylesheet is text transformed into numbers and then subjected to boolean() function. this stylesheet compares "0" as a string and as a number. this stylesheet uses node-sets as arguments for boolean() function.

The lang function returns true or false depending on whether the language of the context node as specified by xml:lang attributes
is the same as or is a sublanguage
of the language specified by the argument string. The language of the context node is determined by the value of the xml:lang
attribute on the context node, or, if
the context node has no xml:lang attribute, by the value of the xml:lang attribute on the nearest ancestor of the context
node that has an xml:lang attribute. If
there is no such attribute, then lang returns false. If there is such an attribute, then lang returns true if the attribute
value is equal to the argument ignoring case, or if
there is some suffix starting with - such that the attribute value is equal to the argument ignoring that suffix of the attribute
value and ignoring case.

String functions

Function string() transforms its argument into string. This function is not usualy directly used in stylesheets as it is in
most cases called by default. this stylesheet shows examples of number to string conversions. Notice results of zero divisions.

The starts-with function returns true if the first argument string starts with the second argument string, and otherwise returns
false. The contains function returns true if the first argument string contains the second argument string, and otherwise
returns false.

The substring-before function returns the substring of the first argument string that precedes and the substring-after function
that follows the first occurrence of the second argument string in the first argument string. The substring function returns
the substring of the first argument starting at the position specified in the second argument with length specified in the
third argument. If the third argument is not specified, it returns the substring starting at the position specified in the
second argument and continuing to the end of the string.Counting starts with 1. ( this stylesheet ). this stylesheet demonstrates a situation where some arguments are out of range or they are not integrals. The returned substring contains
those characters for which the position of the character is greater than or equal to the second argument and, if the third
argument is specified, less than the sum of the second and third arguments.

The string-length function returns the number of characters in the string. The normalize-space function returns the argument
string with white space normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters
by a single space.

The translate function returns the first argument string with occurrences of characters in the second argument string replaced
by the character at the corresponding position in the third argument string. If a character occurs more than once in second
argument string, then the first occurrence determines the replacement character. If the third argument string is longer than
the second argument string, then excess characters are ignored.

Node set functions

The position function returns a number equal to the context position and the last function returns a number equal to the context
size from the expression evaluation context. this stylesheet demonstrates use of these functions in several contexts. this stylesheet compares sorted and unsorted xsl:for-each element.

The id function selects elements by their unique ID. this stylesheet shows simple examples of its use. Carefully study this stylesheet . Contents of title element is not displayed in square brackets "[]" as in DTD its attribute id is defined as CDATA, not ID.
Several id's can be provided at once ( this stylesheet ).

Output

The xsl:output element allows stylesheet authors to specify how they wish the result tree to be output. If an XSLT processor
outputs the result tree, it should do so as specified by the xsl:output element; however, it is not required to do so. The
xsl:output element is only allowed as a top-level element. this stylesheet outputs as html and this stylesheet as xml. Compare how empty tags are outputed.

In the absence of xml:output element the default output method is xml
( this stylesheet ), but if document element of the output has value html (case insensitive) and it doesn't have an 'xmlns' attribute, then
html method is used ( this stylesheet ).

The html output method should not output an end-tag for empty elements specified in HTML specification.The html output method
should not perform escaping for the content of the script and style elements (look at source of the lowest middle window in
your browser). Compare with this stylesheet and consult XSLT specification for more details.

The encoding attribute specifies the preferred encoding to be used. The html output method should add a META element immediately
after the start-tag of the HEAD element specifying the character encoding actually used. this stylesheet outputs in UTF-8, this stylesheet in UTF-16, and this stylesheet in Cp1250. In this stylesheet look at the source of the lowest middle window in your browser. The xml source contains characters which are not present
in specified character set and they are therefore escaped.

The text output method outputs the result tree by outputting the string-value of every text node in the result tree in document
order without any escaping. Look at the source in your browser to see the output.

Copying

The xsl:copy element may have a use-attribute-sets attribute. In this way attributes for copied element can be specified.
this stylesheet does not work as expected (setting use-attribute-sets with name function)., because expresions in attributes that refer to
named XSLT objects are not evaluated.

Miscellaneous Additional Functions

The current function returns a node-set that has the current node as its only member. For an outermost expression (an expression
not occurring within another expression), the current node is always the same as the context node. However, within square
brackets the current node is usually different from the context node.

Combining Stylesheets

Other stylesheets can be imported (xsl:import) or included (xsl:include) into a stylesheet.. Importing a stylesheet is the
same as including it except that definitions and template rules in the importing stylesheet take precedence over template
rules and definitions in the imported stylesheet. this stylesheet and this stylesheet are imported or included into remaining xtylesheets.

You can use xsl:apply-imports element to get information from an imported template, whose behaviour you are changing. this stylesheet imports this stylesheet and overrides its template. this stylesheet imports this stylesheet and changes its template. xsl-apply-imports works only for templates imported with xsl:import, not for templates included
with xsl:include ( this stylesheet ).