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

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
12.74Mb size Format: txt, pdf, ePub
  • validation=“strict”
    causes the processor to look for an attribute declaration that matches the name of the attribute. That is, it looks for a top-level

    whose
    name
    attribute matches the local name of the attribute being validated, in a schema document whose target namespace matches the namespace URI of the attribute being validated. If it can't find such a definition, a fatal error is reported. Otherwise, the value of the attribute node is validated against the schema-defined rules implied by this attribute declaration. Because global attribute declarations are uncommon, this facility has rather limited use.
  • If the attribute declaration in the schema refers to a named type definition, then on successful validation, the attribute is annotated with this type name. If the attribute declaration contains an inline (and therefore unnamed) type definition, the XSLT processor invents a name for this implicit type, and uses this invented name as the type annotation.
  • validation=“lax”
    behaves in the same way as
    validation=“strict”
    , except that no failure occurs if the processor cannot locate a top-level schema definition for the attribute. Instead of reporting an error, the attribute is annotated as
    xs:untypedAtomic
    .

Note that any type annotation created for the attribute may disappear once the attribute is added to an element, unless the element is itself validated, or is created using the option
validation=“preserve”
.

XSLT does not provide any way of requesting validation of an attribute against a local attribute or type definition in a schema. The way around this is to request validation only when you create an element for which there is a top-level definition in the schema. This will then implicitly validate the whole subtree contained by that element, including elements and attributes that have local definitions in the schema.

You're not allowed to validate an attribute if its type is namespace-sensitive. This applies to the types
xs:QName
and
xs:NOTATION
and anything derived from these two. The reason for this restriction is that the validity of these types depends on the namespace context (prefixes in the
QName
or
NOTATION
must have been declared). This means it doesn't make sense to validate the attribute until it has been added to an element.

Usage

You will sometimes see stylesheets that use the

instruction whenever they need to output an attribute value. However, in XSLT there are several different ways of generating an attribute in the result tree. This section compares the different approaches. It then looks specifically at the problem of creating an attribute whose value is a QName, such as an
xsi:type
attribute.

Different Ways of Creating Attributes

Where an output element is generated using a literal result element, the simplest way to specify attributes is normally to include them as attributes on the literal result element itself. You can do this even when the value is derived from information in the source document, because the value can be generated using an attribute value template, for example:


This concatenates three attributes of the current node in the source tree to create a single attribute in the result tree. Attribute value templates are described in Chapter 3, page 122.

Using

gives you more control than writing the attribute directly using attribute value templates. It is useful where one of the following conditions applies:

  • The parent element is output using

    or

    (rather than a literal result element).
  • There is conditional logic to decide whether to output the attribute or not.
  • The name of the attribute is computed at runtime.
  • There is complex logic to calculate the value of the attribute.
  • The attribute is one of a set that can conveniently be grouped together using

    .
  • The output attribute belongs to a namespace that is not present in the source document or the stylesheet.

A third way to output attributes is to copy them from the source tree to the result tree by using

or

. This works only if the attribute you want to generate has the same name and same value as an attribute in the source.

Other books

The Death Of Joan Of Arc by Michael Scott
Airs Above the Ground by Mary Stewart
A Dead Issue by John Evans
A Strange Affair by Rosemary Smith
As an Earl Desires by Lorraine Heath
Matilda Bone by Karen Cushman
Dark Alchemy by Laura Bickle
Day Four by Sarah Lotz
The Juniper Tree by Barbara Comyns