xsl:attribute
<!--
Category: instruction -->
<xsl:attribute
name = { qname }
namespace = { uri-reference }>
<!-- Content: template -->
</xsl:attribute>
The xsl:attribute
element can be used to add attributes to
result elements whether created by literal result elements in the stylesheet
or by instructions such as xsl:element
. The expanded-name of the
attribute to be created is specified by a required name
attribute and an optional namespace
attribute. Instantiating an
xsl:attribute
element adds an attribute node to the containing
result element node. The content of the xsl:attribute
element is
a template for the value of the created attribute.
The name
attribute is interpreted as an attribute value template. It is an
error if the string that results from instantiating the attribute value
template is not a QName or is the string
xmlns
. An XSLT processor may signal the error; if it does not
signal the error, it must recover by not adding the attribute to the result
tree. If the namespace
attribute is not present, then the QName is expanded into
an expanded-name using the namespace declarations in effect for the
xsl:attribute
element, not including any default
namespace declaration.
If the namespace
attribute is present, then it also is
interpreted as an attribute value
template. The string that results from instantiating it should be a URI
reference. It is not an error if the string is not a syntactically legal URI
reference. If the string is empty, then the expanded-name of the attribute
has a null namespace URI. Otherwise, the string is used as the namespace URI
of the expanded-name of the attribute to be created. The local part of the QName specified by the
name
attribute is used as the local part of the expanded-name of
the attribute to be created.
XSLT processors may make use of the prefix of the QName specified in the
name
attribute when selecting the prefix used for outputting the
created attribute as XML; however, they are not required to do so and, if the
prefix is xmlns
, they must not do so. Thus, although it is not
an error to do:
<xsl:attribute name="xmlns:xsl" namespace="whatever">http://www.w3.org/1999/XSL/Transform</xsl:attribute>
it will not result in a namespace declaration being output.
Adding an attribute to an element replaces any existing attribute of that element with the same expanded-name.
The following are all errors:
Adding an attribute to an element after children have been added to it; implementations may either signal the error or ignore the attribute.
Adding an attribute to a node that is not an element; implementations may either signal the error or ignore the attribute.
Creating nodes other than text nodes during the instantiation of the
content of the xsl:attribute
element; implementations may
either signal the error or ignore the offending nodes.
NOTE:When anxsl:attribute
contains a text node with a newline, then the XML output must contain a character reference. For example,<xsl:attribute name="a">x y</xsl:attribute>will result in the outputa="x
y"(or with any equivalent character reference). The XML output cannot bea="x y"This is because XML 1.0 requires newline characters in attribute values to be normalized into spaces but requires character references to newline characters not to be normalized. The attribute values in the data model represent the attribute value after normalization. If a newline occurring in an attribute value in the tree were output as a newline character rather than as character reference, then the attribute value in the tree created by reparsing the XML would contain a space not a newline, which would mean that the tree had not been output correctly.