Inside XSL-T (2/4) - exploring XML | WebReference

Inside XSL-T (2/4) - exploring XML

Inside XSL-T

Importing vs. including stylesheets

An XSLT stylesheet may include another XSLT stylesheet using an xsl:include element, which has an href attribute identifying the stylesheet to be included. The resource located by the href attribute value is parsed as an XML document, and the children of the xsl:stylesheet element in this document replace the xsl:include element in the including document. The fact that template rules or definitions are included does not affect the way they are processed. An XSLT stylesheet may also import another XSLT stylesheet using an xsl:import element. 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.

Output directives

The xsl:output element allows stylesheet authors to specify how they wish the result tree to be output. Currently three output methods are defined: xml, html and text. The xml output method outputs the result tree as a well-formed XML entity.

The html output method takes care of some specialties of HTML documents, for instance omitting end-tags for empty elements. For example, an element written as <br/> or <br></br> in the stylesheet should be output as <br>.

HTML elements should be recognized regardless of case; for example, elements named br, BR or Br should all be recognized as the HTML br element and output without an end-tag. No escaping for the content of the script and style elements should be performed.

The text output method outputs the result tree by writing the string-value of every text node in the result tree in document order without any escaping. The media-type attribute is applicable, the default being text/plain.

The encoding attribute identifies the encoding that the text output method should use to convert sequences of characters to sequences of bytes. The default is system-dependent.

For source documents, the set of whitespace-preserving element names is specified by xsl:strip-space and xsl:preserve-space top-level elements. These elements each have an elements attribute whose value is a whitespace-separated list of name tests, which are expressed in XPath (see below).

Decimal formats

The xsl:decimal-format element declares a decimal-format, which controls the interpretation of a format pattern used by the format-number function. If there is a name attribute, then the element declares a named decimal-format; otherwise, it declares the default decimal-format.

For formatting a number the following attributes are used, with their respective default values in brackets: decimal-separator (.), grouping-separator (e.g. thousands) (,), percent (%), per-mille (#x2030), zero-digit (0), a digit in the format pattern (#), pattern-separator (;), infinity (Infinity), NaN (NaN) for Not-A-Number, minus-sign (-, #x2D)

Namespace aliases

A stylesheet can use the xsl:namespace-alias element to declare that one namespace URI is an alias for another namespace URI. This is useful when the result document also contains XSLT elements.

Ids on steroids: Keys

Keys provide a way to work with documents that contain an implicit cross-reference structure. The ID, IDREF and IDREFS attribute types in XML provide a mechanism to allow XML documents to make their cross-reference explicit. XSLT supports this through the XPath id function. However, this mechanism has a number of limitations, for instance a document can contain only a single set of unique IDs.

A key is attached to a node and has a name and a value. Thus, a key is a kind of generalized ID, which is not subject to the same limitations as an XML ID. Keys are declared in the stylesheet using xsl:key elements. A key has a name as well as a value; each key name may be thought of as distinguishing a separate, independent space of identifiers. The value of a named key for an element may be specified in any convenient place; for example, in an attribute, in a child element or in content. An XPath expression (below) is used to specify where to find the value for a particular named key. The value of a key can be an arbitrary string; it is not constrained to be a name. There can be multiple keys in a document with the same node, same key name, but different key values. There can also be multiple keys in a document with the same key name, same key value, but different nodes. The key function does for keys what the id function does for IDs.

Next areattribute sets, variables, parameters, and XPath.

Produced by Michael Claßen
All Rights Reserved. Legal Notices.

Created: Aug 13, 2000
Revised: Aug 13, 2000