SAXONICA

Saxon Documentation
Full Contents
About Saxon
Changes in this Release
Conditions of Use
Using XSLT 2.0
Using XQuery
Handling Source Documents
XML Schema Processing
The XPath API
Standards Conformance
Extensibility
Saxon Extensions
Sample Saxon Applications
The Saxon SQL Extension
XSLT Elements
| Introduction
| xsl:analyze-string
| xsl:apply-imports
| xsl:apply-templates
» | xsl:attribute
| xsl:attribute-set
| xsl:call-template
| xsl:character-map
| xsl:choose
| xsl:comment
| xsl:copy
| xsl:copy-of
| xsl:decimal-format
| xsl:document
| xsl:element
| xsl:fallback
| xsl:for-each
| xsl:for-each-group
| xsl:function
| xsl:if
| xsl:include
| xsl:import
| xsl:import-schema
| xsl:key
| xsl:matching-substring
| xsl:message
| xsl:namespace
| xsl:namespace-alias
| xsl:next-match
| xsl:non-matching-substring
| xsl:number
| xsl:otherwise
| xsl:output
| xsl:output-character
| xsl:param
| xsl:perform-sort
| xsl:preserve-space
| xsl:processing-instruction
| xsl:result-document
| xsl:sequence
| xsl:sort
| xsl:strip-space
| xsl:stylesheet
| xsl:template
| xsl:text
| xsl:value-of
| xsl:variable
| xsl:when
| xsl:with-param
| Literal Result Elements
XPath 2.0 Expression Syntax
Function Library
XSLT Patterns

# xsl:attribute

The `xsl:attribute` element is used to add an attribute value to an `xsl:element` element or general formatting element, or to an element created using `xsl:copy`. The attribute must be output immediately after the element, with no intervening character data. The name of the attribute is indicated by the `name` attribute and the value by the content of the `xsl:attribute` element.

The attribute name is interpreted as an attribute value template, so it may contain string expressions within curly braces. The full syntax of string expressions is outlined in XPath Expression Syntax.

The attribute value may be given either by a `select` attribute or by an enclosed sequence constructor. If the `select` attribute is used and the value is a sequence, then the items in the sequence are output space-separated.

The `separator` attribute is not yet available.

For example, the following code creates a <FONT> element with several attributes:

    <xsl:element name="FONT">
        <xsl:attribute name="SIZE">4</xsl:attribute>
        <xsl:attribute name="FACE">Courier New</xsl:attribute>
    Some output text
    </xsl:element>

A new attribute `type` is available in XSLT 2.0. This indicates the data type of the value of the attribute. In Saxon, the value must be a built-in type defined in XML Schema, for example `xs:integer` or `xs:date`. Type annotations are only accessible if the attribute is added to a temporary tree that specifies `validation="preserve"`. The value given to the attribute must be a string that conforms to the rules for the data type, as defined in XML Schema.

There are two main uses for the `xsl:attribute` element:

- It is the only way to set attributes on an element generated dynamically using xsl:element

- It allows attributes of a literal result element to be calculated using xsl:value-of.

The `xsl:attribute` must be output immediately after the relevant element is generated: there must be no intervening character data (other than white space which is ignored). Saxon outputs the closing ">" of the element start tag as soon as something other than an attribute is written to the output stream, and rejects an attempt to output an attribute if there is no currently-open start tag. Any special characters within the attribute value will automatically be escaped (for example, "<" will be output as "&lt;")

If two attributes are output with the same name, the second one takes precedence.

Next