<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="xsl-elements" page="attribute" subpage=""/>
      <!--
           Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: xsl:attribute</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title" content="Saxonica: XSLT and XQuery Processing: xsl:attribute"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>xsl:attribute</h1>
      <p>The <code>xsl:attribute</code> element is used to add an attribute value to an
<code>xsl:element</code> element or general formatting element, or to an element created using <code>xsl:copy</code>.
The attribute must be output immediately after the element, with no intervening character data.
 The name of the attribute is indicated by the
<code>name</code> attribute and the value by the content of the <code>xsl:attribute</code> element.</p>
      <p>The attribute name is interpreted as an <i>attribute value template</i>, so it may contain string expressions
within curly braces. The full syntax of string expressions is 
outlined in <a class="bodylink" href="../expressions/intro.xml">XPath Expression Syntax</a>.
</p>
      <p>The attribute value may be given either by a <code>select</code> attribute or by an enclosed
sequence constructor. If the <code>select</code> attribute is used and the value is a sequence, then
the items in the sequence are output space-separated.</p>
      <p>The <code>separator</code> attribute can be used to specify an alternative separator.</p>
      <p>For example, the following code creates a &lt;FONT&gt; element with several attributes:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>&lt;xsl:element name="FONT"&gt;
    &lt;xsl:attribute name="SIZE"&gt;4&lt;/xsl:attribute&gt;
    &lt;xsl:attribute name="FACE"&gt;Courier New&lt;/xsl:attribute&gt;
Some output text
&lt;/xsl:element&gt;
</code>
         </pre>
      </div>
      <p>A new attribute <code>type</code> was added in XSLT 2.0. This indicates the
data type of the value of the attribute. The value must either be a built-in type defined
in XML Schema, for example <code>xs:integer</code> or <code>xs:date</code>, or a user-defined type
            defined in a schema imported using <code>xsl:import-schema</code>. Type annotations
are only accessible if the attribute is added to a temporary tree that specifies 
<code>validation="preserve"</code>. The value given to the attribute must be a string
that conforms to the rules for the data type, as defined in XML Schema.</p>
      <p>There are two main uses for the <code>xsl:attribute</code> element: </p>
      <ul>
         <li content="para">
            <p>It is the only way to set attributes on an element generated dynamically using xsl:element</p>
         </li>
         <li content="para">
            <p>It allows attributes of a literal result element to be calculated using xsl:value-of.</p>
         </li>
      </ul>
      <p>The <code>xsl:attribute</code> 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
"&gt;" 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, "&lt;" will be output as
"&amp;lt;")</p>
      <p>If two attributes are output with the same name, the second one takes precedence.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="attribute-set.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>