XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (52 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
10.12Mb size Format: txt, pdf, ePub

Curly brackets can never be nested. You can use them only to include an XPath expression in the text of a stylesheet attribute; they cannot be used within an XPath expression itself. You can always achieve the required effect some other way; for example, instead of:

 

write:


The
concat()
function, described in Chapter 13, performs concatenation of strings.

Attribute value templates cannot be used anywhere you like in the stylesheet. They can be used only for those attributes that are specifically identified as attribute value templates in the XSLT Recommendation. The following table gives a complete list of all the places you can use attribute value templates.

Element
Attributes Interpreted as Attribute Value Templates
Literal result elements
All attributes except those in the XSLT namespace
Extension elements
As defined by the specification of each extension element

regex
,
flags

name,
namespace,
separator

name,
namespace

collation

terminate

name

format,
lang,
letter-value,
ordinal,
grouping-separator,
grouping-size

name

format, href, method, byte-order-mark, cdata-section-elements, doctype-public, doctype-system, encoding, escape-uri-attributes, include-content-type, indent, media-type, normalization-form, omit-xml-declaration, standalone, undeclare-prefixes, output-version

lang,
order,
collation,
data-type,
case-order

separator

In all other contexts, don't even think of trying to use them because the curly braces will either be ignored or cause an error to be reported. It can be very tempting, if you want to use

, for example, and the name of the template you want to call is in a variable, to want to write:





However, you can't, because the
name
attribute (or any other attribute of

for that matter) of

is not in the above list of places where attribute value templates can be used.

Why are attribute value templates rationed so severely? The restrictions are there deliberately to make life easier for the XSLT processor:

  • Attribute value templates are never allowed for attributes of declarations. This ensures that the values are known before the source document is read, and are constant for each run of the stylesheet.
  • Attribute value templates are never allowed for attributes whose value is an XPath expression or a pattern. This ensures that expressions and patterns can be compiled when the stylesheet is read, and do not need to be re-parsed each time they are evaluated.
  • Attribute value templates are generally not allowed for attributes whose value is the name of another object in the stylesheet, for example a named template or a named attribute set (an exception is the
    format
    attribute of

    ). This ensures that references from one stylesheet object to another can be resolved once and for all when the stylesheet is first read. They are allowed, however, for names of nodes being written to the result tree.
  • Attribute value templates are not recognized when attributes such as
    xml:space, xml:lang
    ,
    xml:base, or xsi:type
    are interpreted by the XML parser or schema validator. This also applies to namespace declarations (
    xmlns
    and
    xmlns:prefix
    ). This is because the XML parser reads the value before the XSLT processor gets a chance to expand it. The XSLT processor won't stop you from using curly braces in such an attribute when it appears on a literal result element, but the chances are that some other software will throw an error before you get that far.

When an XPath expression within an attribute value template is evaluated, the context is the same as for any other expression in the stylesheet. The idea of an expression having a context was introduced in Chapter 2, on page 84: it determines the meaning of constructs such as
.
, which refers to the context node, and
position()
, which refers to the context position. Variables and namespace prefixes may be used within the expression only if they are in scope at that point in the stylesheet. The context item, context position, and context size are determined from the sequence being processed in the most recent call of

,

, or

. Outside such a call (for example, while a global variable is being evaluated), the context item is set to a value supplied by the caller of the stylesheet (generally the document node of the source document), and the context position and size are set to 1 (one).

Simplified Stylesheets

A simplified stylesheet uses an abbreviated syntax in which the

element and all the top-level declarations are omitted.

The original purpose of this facility was to allow people with HTML-authoring skills but no programming experience to write simple stylesheets with a minimum of training. A simplified stylesheet has a skeleton that looks like the target document (which is usually HTML, although it doesn't have to be), and uses XSLT instructions to fill in the variable parts.

A stylesheet module is interpreted as a simplified stylesheet if the outermost element is not

or

. The outermost element can have any name, provided it is not in the XSLT namespace. It must still contain a declaration of the XSLT namespace, and it must have an
xsl:version
attribute. For XSLT 2.0 the value should be
1.0
or
2.0
(use
2.0
if your stylesheet depends on features defined in XSLT 2.0, or
1.0
if you also want it to work with XSLT 1.0 processors). When the
xsl:version
attribute is greater than
2.0
, forward-compatible processing mode is enabled. This is discussed later in this chapter on page 130.

Other books

The Devastators by Donald Hamilton
The Easy Sin by Jon Cleary
The 13th Mage by Inelia Benz
Strays by Jennifer Caloyeras