Template markup

Before discussing the various ways in which attribute templates can be of benefit, it is necessary to establish how they are recognized and identify some repercussions of their possible presence.

Curly brackets

When an output element includes an attribute, the attribute is coded according to XML rules. It has a name, and a value enclosed in quotes. The value may be any text string allowed in attributes of the given type, assuming the use of a DTD to validate the output, or (when a DTD is not relevant) it can be any text string at all. The issue here is how to indicate to the XSLT processor that there is an expression embedded within the value:

TheAttribute="value ... expression ... value"

The expression is delimited using curly brackets. The left curly bracket, '{', begins the template, and the right curly bracket, '}', ends the template. The text between these brackets is intepreted as an expression:

="...{expression}..."

A single attribute can contain any number of expression templates:

="...{expression}...{expression}..."

With the curly brackets taking this special role, the obvious question that arises is how to represent these characters within a normal value. For example, the value 'a{b}c' may need to appear in an attribute, and the letter 'b' should not be interpreted as an expression. To solve this problem, the single character '{' is represented by the sequence '{{', and the single character '}' is represented by the sequence '}}'. For example:

="This is {{NOT}} an expression"

The XSLT processor removes the second bracket in both cases when outputting the string:

This is {NOT} an expression

String expressions

An expression may result in an object of any type allowed by the XPath expression language. But the need here is for a string value to be inserted into the attribute value. Therefore, the expression result is converted to a string, as if embedded within the XPath 'string(…)' function. This function is implied, and so the following two examples are equivalent:

="...{true()}..."


="...{string(true())}..."

In both cases, the function 'true()' resolves to the string 'true'. In the latter example, it is first explicitly converted to the string 'true', but this string is then also converted to a string (without effect). For the sake of brevity alone, the string function should not be included.

In order to avoid surprises, the XPath 'string()' function needs to be fully understood. Software developers familiar with the DOM (Document Object Model) can be easily misled by this function as it does not necessarily return the string value of the object, such as the current element. The string value of an element is a combination (in document order) of the string values of all the text nodes within that element. An expression that refers to the current element ('.') actually returns all the text within that element and within the descendants of that element. The string value of an attribute is the normalized value (all whitespace converted to simple spaces, entity references replaced, and in some circumstances the removal of multiple and surrounding spaces).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset