XML information pipe

Ihre Spezialisten für XML

Banner Werbung

XSLT Contents

Contents 1 - 19


XSLT introduction

With XSL you can freely modify any source text. XSLT stylesheet 1 and XSLT stylesheet 2 produce different output from the same source file.

XSLT 1.0 instruction

Root element

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.

XSLT 1.0 instruction

Intructions inside xsl:template

An XSL processors parses an XML source and tries to find a matching template rule. If it does, instructions inside matching template are evaluated.

XSLT 1.0 instruction

xsl:value-of  |  xsl:apply-templates

Contents of the original elements can be recovered from the original sources in two basic ways. XSLT stylesheet 1 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 XSLT stylesheet 2 is different. The parser further processes selected elements, for which a template is defined.

XSLT 1.0 instruction

Page 5 ist identisch zu 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.

XSLT 1.0 instruction

XPath location steps

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. ( XSLT stylesheet 1 )

XSLT 1.0 instruction

implicit template use

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 XSLT stylesheet 1 and XSLT stylesheet 2 . When a template for the node exists, there is no default processing invoked ( XSLT stylesheet 3 ). If you want to include descendants of the node, you have to explicitly request their templates ( XSLT stylesheet 4 ).

XSLT 1.0 instruction

" | "  or  Wildcard

A template can match from a selection of location paths, individual paths being separated with "|" ( XSLT stylesheet 1 ). Wildcard "*" selects all possibilities. Compare XSLT stylesheet 1 with XSLT stylesheet 2 .

XSLT 1.0 instruction

Location step with "//"

"//" 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 ( XSLT stylesheet 1 ). 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 ( XSLT stylesheet 2 ).

XSLT 1.0 instruction

modes

With modes an element can be processed multiple times, each time producing a different result. In XSLT stylesheet 2 one of the modes does not exist.

XSLT 1.0 instruction

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. XSLT stylesheet 1 and XSLT stylesheet 2 differ by priority of their templates. XSLT stylesheet 3 shows the default action in the absence of priority attributes. Template CCC has lower priority than CCC/CCC, as it is less specific. Compare XSLT stylesheet 4 and XSLT stylesheet 5 . 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 XSLT stylesheet 6 less specific "*" has lower priority than CCC. Computed priorities ranges from -0.5 to 0.5. XSLT specification gives more details.

XSLT 1.0 instruction

Page 12

Attributes can be accessed in similar way as elements. Notice "@" in the front of attribute names.

XSLT 1.0 instruction

Page 13

Attributes can be processed in the same way as elements.

XSLT 1.0 instruction

Page 14

You can also select elements, which contain or do not contain the given attribute. XSLT stylesheet 1 includes and XSLT stylesheet 2 excludes elements if the specified attribute is present.

XSLT 1.0 instruction

Page 15

Axes play a very important role in XSLT. See XSLT reference for more details. Compare: child axis ( XSLT stylesheet 1 ), descendant axis ( XSLT stylesheet 2 ), parent axis ( XSLT stylesheet 3 ), ancestor axis ( XSLT stylesheet 4 ), following-sibling axis ( XSLT stylesheet 5 ), preceding-sibling axis ( XSLT stylesheet 6 ), following axis ( XSLT stylesheet 7 ), preceding axis ( XSLT stylesheet 8 ), attribute axis ( XSLT stylesheet 9 ), namespace axis ( XSLT stylesheet 10 ), self axis ( XSLT stylesheet 11 ), descendant-or-self axis ( XSLT stylesheet 12 ), ancestor-or-self axis ( XSLT stylesheet 13 ).

XSLT 1.0 instruction

Page 16

All axes were used in this example.

XSLT 1.0 instruction

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::.

XSLT 1.0 instruction

Page 18

The xsl:for-each instruction contains a template, which is applied to each node selected with select attribute.

XSLT 1.0 instruction

Page 19

Nodes selected with xsl:for-each ( XSLT stylesheet 1 and XSLT stylesheet 2 ) or xsl:apply-templates ( XSLT stylesheet 3 ) can be sorted. Order of sorting determines order attribute. XSLT stylesheet 1 sorts in ascending and XSLT stylesheet 2 in descending mode.

XSLT 1.0 instruction