Inside XSL-T (3/4) - exploring XML
Attribute sets, variables, and parametersThe 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.
TemplatesTemplates 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 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:
- /doc/chapter/section selects the fourth section of the third chapter of the doc
- //section/para selects all the para elements in the same document as the current node that have a section parent
- ../@lang selects the lang attribute of the parent of the current node
- para[@type="warning"] selects all para children of the context node that have a type attribute with value warning
- para[@type="warning"] selects the fifth para child of the context node that has a type attribute with value warning
- para[@type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning
- chapter[title="Introduction"] selects the chapter children of the context node that have one or more title children with string-value equal to Introduction
- chapter[title] selects the chapter children of the context node that have one or more title children
- para[@lang and @annotation] selects all the para children of the current node that have both a lang and an annotation attribute
XPath supports the data types node-sets, booleans, numbers, and strings.
Node-set functions include last(), position(), count(), name(), id() and key().
Sub-string and concatenation functions are included for strings, as well as
the expected basic boolean operations and numerical calculations.
That way complex matching expressions can be formed, for details see
XPath core library.
Finally something about creating result elements, and the unevitable conclusion.
Created: Aug 13, 2000
Revised: Aug 13, 2000