Creates an attribute node and attaches it to an output element.
<xsl:attribute name = "attribute-name" namespace = "uri-reference"> </xsl:attribute>
namespace
attribute. The value of the name
attribute is interpreted as an attribute value template (expressions in curly braces are evaluated and converted to strings as in the <xsl:value-of>
element). This allows the name of the attribute to be calculated or obtained from the source XML.name
attribute contains a QName, the prefix specified there will be bound to the namespace specified in the namespace attribute. This might result in the addition of other namespace declarations when serializing. This value is interpreted as an attribute value template.Number of occurrences | Unlimited |
Parent elements | xsl:copy, xsl:element, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:otherwise, xsl:param, xsl:template, xsl:variable, xsl:when, xsl:with-param |
Child elements | xsl:apply-imports, xsl:apply-templates, xsl:call-template, xsl:choose, xsl:copy, xsl:copy-of, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:number, xsl:text, xsl:value-of, xsl:variable |
The contents of this element specify the value of the attribute.
Attributes can be added or modified during transformation by placing the <xsl:attribute>
element within elements that generate output, such as the <xsl:copy>
element. Note that <xsl:attribute>
can be used directly on output elements and not only in conjunction with <xsl:element>
.
All attributes must be applied before children are added to the element.
This short example generates an attribute that obtains its value from the XML source. It generates output in the form <IMG src="value-from-XML-source"/>
, using an XPath expression that retrieves the appropriate data from the XML sourcehere, "imagenames/imagename"
.
XSLT file
<IMG> <xsl:attribute name="src"> <xsl:value-of select="imagenames/imagename" /> </xsl:attribute> </IMG>
Output
<IMG src=" imagenames/imagename"/>
While the <xsl:attribute>
element can be extremely useful for dynamically creating output attributes that are not known prior to transforming a document, you do not need to use this element if you already know the attributes. In the preceding example, you might already know that an IMG
element must contain a src
attribute. Because you know this requirement prior to transforming the document, you would not have to use the <xsl:attribute>
element. You could simplify the transformation syntax and still achieve the same result by using the following:
<IMG src="{imagenames/imagename}"/>
By using the <xsl:attribute>
element instead of attribute value templates, you can:
<xsl:copy>
or <xsl:element>
element.This section also contains the following full examples.
Selecting and Outputting Attributes | Generating Comments, Processing Instructions, and Elements | Qualified Names