Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The local part of the name of the created attribute node will always be the same as the local part of the
QName
supplied as the value of the
name
attribute.
If the
namespace
attribute, it is evaluated (expanding the attribute value template if necessary) to determine the namespace URI part of the name of the created attribute node:
If there is no
namespace
attribute:
Where possible, the prefix of the attribute name (or the absence of a prefix) will be taken from the prefix given in the
name
attribute. However, if it isn't possible to use this prefix, then the namespace fixup process (described on page 310) will allocate a different prefix. This arises, for example, if two attributes of the same element use the same prefix to refer to different namespaces, or if the attribute is in a namespace but no prefix is supplied, or if a reserved prefix such as
xml
or
xmlns
is used.
Namespace fixup is not performed at the level of an individual attribute node. This is because attributes can only be associated with namespace nodes via their parent element.
If the
name
attribute provides a name that has no prefix, but a
namespace
attribute is present so that the namespace URI is not null, then the system has no choice but to invent a namespace prefix for the attribute. For example, if you write:
then the output might be:
The XSLT specification explicitly states that you cannot use
xmlns
or
xmlns:*
. In the XPath data model, attributes and namespaces are quite different animals, and you cannot simulate a namespace declaration by creating an attribute node with a special name. Namespace declarations will be added to the output automatically whenever you generate elements or attributes that require them, and if you really need to, you can also force them to be generated using the
Using the New Attribute Node
The new attribute node, when initially created, has no parent node. Usually, however, the result of the instruction will form part of a sequence that is used to construct the content of a new element node. This will always be the case when the parent of the
Very often the
but this is not essential, for example you could also do:
and then create the attribute from within the
set-border
template. Less commonly, parentless attribute nodes may be created and held in variables, for example:
In this particular case, just to show what is possible, we have created a variable whose value is a sequence of three parentless attributes, each of which has the same attribute name. These attributes could later be added to elements using instructions such as:
Whether the attribute node is added immediately to an element, or whether it is perhaps stored in a variable and added to an element later, at the point where the attribute is attached to an element, it must appear in the sequence before any child nodes (text nodes, elements, comments, or processing instructions) that are being added to the same element node.
This rule is there for the convenience of implementors. It means that the XSLT processor can execute the stylesheet and build the result tree sequentially, if it wishes. If it does so, the attributes for an element will always be evaluated before the children of the element, which means that the processor doesn't actually need to build the result tree in memory. Instead, each node can be serialized to an XML file as soon as it is generated. If it weren't for this rule, the software wouldn't be able to write the first start tag until right at the end, because there would always be a chance of an attribute being added to it.
When a sequence of nodes containing several attribute nodes is used to form the content of an element, and several of the attributes have the same name, the one that is used is the one that appears last in the sequence. This is not an error: in fact, when named attribute sets are used to add attributes to an output element, it is an important mechanism. Named attribute sets are described under
XQuery does this differently. In XQuery 1.0, adding two attributes with the same name to the same element is an error.
The Value of the Attribute
The string value of the new attribute node is obtained by evaluating the
select
attribute or the sequence constructor. The value of the
separator
attribute, if present, also plays a role.
Whether the
select
attribute or the sequence constructor is used, it is evaluated to produce a sequence of items, and this sequence of items is processed as follows:
1.
Adjacent text nodes in the sequence are merged, and zero-length text nodes are removed.
2.
The sequence is atomized. Atomization is described in Chapter 2 (page 81). This process replaces any nodes in the sequence by their typed values.
3.
Every value in the atomized sequence is converted to a string by applying the XPath casting rules.
4.
The strings in the resulting sequence are concatenated, with an optional separator between adjacent strings. If the
separator
attribute is present, its value is used as the separator (this may be a zero-length string). If the
select
attribute is used, then the default separator is a single space; otherwise, it is a zero-length string—in other words, there is no separator.
5.
The resulting string is used as the value of the new attribute node.