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

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

Inside XSL-T

Attribute sets, variables, and parameters

The xsl:attribute-set element defines a named set of attributes. These attribute sets can be attached to new elements using xsl:element, or copied from element to element using xsl:copy or xsl:use-attribute-sets. The value of the use-attribute-sets attribute is a whitespace-separated list of names of attribute sets, so it is a convenient way for inserting the same set of attributes in many places.

A variable is a name that may be bound to a value. The value to which a variable is bound can be an object of any of the types that can be returned by expressions, namely node-sets, booleans, strings, and numbers. There are two elements that can be used to bind variables: xsl:variable and xsl:param. The difference is that the value specified on the xsl:param variable is only a default value for the binding; when the template or stylesheet within which the xsl:param element occurs is invoked, parameters may be passed that are used in place of the default values.

A variable-binding element can specify the value of the variable in three alternative ways. If the variable-binding element has a select attribute, then the value of the attribute must be an expression and the value of the variable is the object that results from evaluating the expression. In this case, the content must be empty. If the variable-binding element does not have a select attribute and has non-empty content (i.e. the variable-binding element has one or more child nodes), then the content of the variable-binding element specifies the value. The content of the variable-binding element is a template, which is instantiated to give the value of the variable. If the variable-binding element has empty content and does not have a select attribute, then the value of the variable is an empty string.

Templates

Templates are the main mechanism for transforming the source into the result document. They consist of two parts, an XPath expression that is supposed to match elements in the source tree, and a fragment that gets inserted into the result document by creating new elements and copying and modifying elements from the source.

Matching source and result elements: XPath

The primary purpose of XPath is to address parts of an XML [XML] document. Supporting this, it also provides basic facilities for manipulation of strings, numbers and booleans. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document, and a natural subset can be used for matching (testing whether or not a node matches a pattern) in XSLT.

Location paths

Location paths select one node or a set of nodes in the document tree. They exist in normal and abbreviated syntax, where the latter mimics directory navigation behavior.

Here are some examples of location paths using abbreviated syntax: