>> English << | Français | Deutsch | Magyar | 中文 | Polski ZVON > Tutorials > XSLT Tutorial
>> Contents << | Element Index

Introduction

Page 1 With XSL you can freely modify any source text. this stylesheet and this stylesheet produce different output from the same source file.
Page 2 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.
Page 3 An XSL processors parses an XML source and tries to find a matching template rule. If it does, instructions inside matching template are evaluated.
Page 4 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

Page 5 An XSL processors parses an XML source and tries to find a matching template rule. If it does, instructions inside matching template are evaluated.
Page 6 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 )
Page 7 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 ).
Page 8 A template can match from a selection of location paths, individual paths being separated with "|" ( this stylesheet ). Wildcard "*" selects all possibilities. Compare this stylesheet with this stylesheet .
Page 9 "//" 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 ).
Page 10 With modes an element can be processed multiple times, each time producing a different result. In this stylesheet one of the modes does not exist.
Page 11 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.

Attributes

Page 12 Attributes can be accessed in similar way as elements. Notice "@" in the front of attribute names.
Page 13 Attributes can be processed in the same way as elements.
Page 14 You can also select elements, which contain or do not contain the given attribute. this stylesheet includes and this stylesheet excludes elements if the specified attribute is present.

Axes

Page 15 Axes play a very important role in XSLT. See XSLT reference for more details. Compare: child axis ( this stylesheet ), descendant axis ( this stylesheet ), parent axis ( this stylesheet ), ancestor axis ( this stylesheet ), following-sibling axis ( this stylesheet ), preceding-sibling axis ( this stylesheet ), following axis ( this stylesheet ), preceding axis ( this stylesheet ), attribute axis ( this stylesheet ), namespace axis ( this stylesheet ), self axis ( this stylesheet ), descendant-or-self axis ( this stylesheet ), ancestor-or-self axis ( this stylesheet ).
Page 16 All axes were used in this example.
Page 17 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::.

Repetition and sorting

Page 18 The xsl:for-each instruction contains a template, which is applied to each node selected with select attribute.
Page 19 Nodes selected with xsl:for-each ( this stylesheet and this stylesheet ) or xsl:apply-templates ( this stylesheet ) can be sorted. Order of sorting determines order attribute. this stylesheet sorts in ascending and this stylesheet in descending mode.
Page 20 this stylesheet sorts in text and this stylesheet in numeric mode. Notice the important difference. 2 is after 1 in alphabet so 2 goes after 10 in text mode.
Page 21 this stylesheet sorts upercase and this stylesheet lowercase letters first.

Creation of elements and attributes

Page 22 xsl:element generates elements in time of processing. this stylesheet uses this feature, while this stylesheet achieves the same effect in a different and laborious way.
Page 23 xsl:attribute generates elements in time of processing. It creates attribute in the element in which it is enclosed.
Page 24 Copy and copy-of constructs are used for nodes copying. Copy element copies only the current node without children and attributes, while copy-of copies everything.
Page 25 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

Page 26 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 )
Page 27 xsl:choose element is used for selection between several possibilities.
Page 28 How to find out that some text starts with a number.

Numbers generation and formatting

Page 29 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.
Page 30 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.
Page 31 this stylesheet and this stylesheet are examples of formatting of multilevel numbers.

Variables

Page 32 this stylesheet and this stylesheet demonstrate different ways of setting xsl:variable, this stylesheet and this stylesheet of setting xsl:param.
Page 33 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.
Page 34 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.
Page 35 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 .
Page 36 There is an important difference in variable value specification.

Numeric calculations

Page 37 Functions number transforms its argument into a number. this stylesheet demonstrates string conversion, this stylesheet conversion of boolean values true and false.
Page 38 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 )
Page 39 Function sum() sums all numbers in selected nodes. this stylesheet sums all numbers, this stylesheet only odd ones.
Page 40 Functions ceilng(), floor() and round() transform floating point numbers into integers in the specified way.
Page 41 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.
Page 42 Test, if element value is a number

Boolean functions

Page 43 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.
Page 44 The not function returns true if its argument is false, and false otherwise.
Page 45 Functions true() and false() are useful, when some conditions are tested during programming.
Page 46 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

Page 47 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.
Page 48 The concat function returns the concatenation of its arguments.
Page 49 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.
Page 50 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.
Page 51 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.
Page 52 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

Page 53 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.
Page 54 The count function returns the number of nodes in the argument node-set.
Page 55 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 ).
Page 56 An example of id function usage.
Page 57 Functions name, local-name, and namespace-uri() are used to get informations about element and attribute names and namespaces.

Output

Page 58 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.
Page 59 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 ).
Page 60 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.
Page 61 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.
Page 62 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

Page 63 Copy and copy-of constructs are used for nodes copying. Copy element copies only the current node without children and attributes, while copy-of copies everything.
Page 64 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

Page 65 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.
Page 66 Function generate-id generates id conforming to XML spec. this stylesheet uses this function to add id to all elements in source XML.

Combining Stylesheets

Page 67 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.
Page 68 this stylesheet imports this stylesheet and this stylesheet imports this stylesheet .
Page 69 Other examples of xsl:include and xsl:import.
Page 70 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 ).
Page 71 Import precedence is more important than priority precedence. Look at this stylesheet .