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

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

Higher order functions such as this are a powerful programming technique, particularly when handling complex data structures.

See Also


on page 322


on page 344


on page 452


on page 455


on page 483


on page 517

xsl:attribute

The

instruction constructs an attribute node and adds it to the result sequence.

Changes in 2.0

The content of the attribute can be specified using a
select
attribute, as an alternative to using a sequence constructor. The
separator
attribute can be used to format the attribute value when the content is supplied as a sequence.

With a schema-aware XSLT processor, the
type
and
validation
attributes can be used to control the type annotation given to the new attribute node.

Format

  name = { qname }

  namespace? = { uri-reference }

  select? = expression

  separator? = { string }

  validation? = “strict” | “lax” | “preserve” | “strip”

  type? = qname>

  


Position


may be used either as an instruction within a
sequence constructor
, or within an

element.

Attributes

Name
Value
Meaning
name
mandatory
Attribute value template returning a lexical QName
The name of the attribute node to be generated
namespace
optional
Attribute value template returning a URI
The namespace URI of the generated attribute node
select
optional
XPath Expression
Provides the value of the attribute
separator
optional
Attribute value template returning a string
Separator string to be inserted between items when the value is a sequence
validation
optional
strict
,
lax
,
preserve
, or
skip
Indicates whether and how the attribute should be subjected to schema validation
type
optional
Lexical QName
Identifies a type declaration (either a built-in type, or a user-defined type imported from a schema) against which the new element is to be validated

The
type
and
validation
attributes are mutually exclusive: if one is present, the other must be absent.

Content

A sequence constructor, unless the
select
attribute is present, in which case the content must be empty.

Effect

The effect of this instruction is to create a new attribute node and to return this node as the result of the instruction. In the usual case where the sequence constructor containing the

instruction is used to construct the content of an element, the attribute must not be preceded in the result sequence by any node other than a namespace node or another attribute node. The name of the generated attribute node is determined using the
name
and
namespace
attributes. The way in which these attributes are used is described below in the section
The Name of the Attribute
.

The value of the new attribute node may be established either using the
select
attribute or using the sequence constructor contained in the

instruction. These are mutually exclusive: if the
select
attribute is present, the

element must be empty. If neither is present, the value of the attribute will be a zero-length string. The way the value of the attribute is established is described in more detail in the section
The Value of the Attribute
.

When a schema-aware XSLT processor is used, the new attribute may be validated to ensure that it conforms to a type defined in a schema. This process results in the new attribute node having a type annotation. The type annotation affects the behavior of subsequent operations on this attribute node even though it is not visible when the result tree is serialized as raw XML. The validation and annotation of the new attribute node are controlled using the
type
and
validation
attributes. This is described in the section
Validating and Annotating the Attribute
.

The Name of the Attribute

The name of an attribute node has three parts: the prefix, the local name, and the namespace URI. These are controlled using the
name
and the
namespace
attributes.

Both the
name
and the
namespace
attributes may be given as attribute value templates; that is, they may contain expressions nested within curly braces. One of the main reasons for using the

instruction in preference to attributes on a literal result element (described in the section
Literal Result Elements
in Chapter 3, page 112) is that

allows the name of the attribute node to be decided at runtime, and this is achieved by using attribute value templates in these two attributes.

The result of expanding the
name
attribute value template must be a lexical
QName
; that is, a valid XML name with an optional namespace prefix, for example,
code
or
xsi:type
. If there is a prefix, it must correspond to a namespace declaration that is in scope at this point in the stylesheet, unless there is also a
namespace
attribute, in which case it is taken as referring to that namespace.

Other books

Smuggler Nation by Andreas, Peter
The Storyteller by Mario Vargas Llosa
Presa by Michael Crichton
The Way of Wanderlust by Don George
Howtown by Michael Nava
Love Beyond Time by Speer, Flora
Beneath the Surface by Heidi Perks
CAUSE & EFFECT by THOMPSON, DEREK