XSL reference documentation generated from the W3C Recommendation 16 November 1999
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
attribute and an optional
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
name attribute is interpreted as an
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
namespace attribute is present, then it also is
interpreted as an
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
element; implementations may either signal the error or ignore the
Note: When an
xsl:attribute contains a text node with
a newline, then the XML output must contain a character reference.
<xsl:attribute name="a">x y</xsl:attribute>will result in the output
y"(or with any equivalent character reference). The XML output cannot be
a="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.