<html>
<head>
<title>&lt;xsl:attribute&gt;</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="Description">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
<tr>
<td valign="top" class="NAME">&lt;xsl:attribute&gt;</td>
<td valign="top" class="COMPATIBILITY">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
</tr>
<tr>
<td valign="top" colspan="2" class="description">
Allows you to create an attribute in the output document. The advantage of <span class="LITERAL">&lt;xsl:attribute&gt;</span> is that it allows you to build the attribute's value from parts of the input document, hardcoded text, values returned by functions, and any other value you can access from your stylesheet.</td></tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Category</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Instruction</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Required Attributes</td>
</tr>
<tr>
<td colspan="2" class="description">
<dl>
<dt>
name
</dt>
<dd>
The
<span class="LITERAL">
name
</span>
attribute defines the name of the attribute created by the
<span class="LITERAL">
&lt;
xsl:attribute
&gt;
</span>
element. (No matter how you try to say this, talking about the attributes of the
<span class="LITERAL">
&lt;
xsl:attribute
&gt;
</span>
element is confusing, isn't it?)
<P></p>
</dl>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Optional Attributes</td>
</tr>
<tr>
<td colspan="2" class="description">
<dl>
<dt>
namespace
</dt>
<dd>
The
<span class="LITERAL">
namespace
</span>
attribute defines the namespace URI that should be used for this attribute in the output document. You don't have control over the namespace prefix used; the only thing you specify with the
<span class="LITERAL">
namespace
</span>
attribute is the URI of the namespace.
<P></p>
</dl>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Content</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>An XSLT template. In other words, you can build the contents of an attribute with <span class="LITERAL">&lt;xsl:choose&gt;</span> elements, <span class="LITERAL">&lt;xsl:text&gt;</span>, and <span class="LITERAL">&lt;xsl:value-of&gt;</span> elements. </p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Appears in</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>
<span class="LITERAL">&lt;xsl:attribute&gt;</span> appears inside a template.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Defined in</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>XSLT section 7.1.3, Creating Attributes with <span class="LITERAL">xsl:attribute</span>.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Example</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>For this example, we want to create an HTML table from the following XML document:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;list xml:lang="en"&gt;
  &lt;title&gt;Albums I've bought recently:&lt;/title&gt;
  &lt;listitem&gt;The Sacred Art of Dub&lt;/listitem&gt;
  &lt;listitem&gt;Only the Poor Man Feel It&lt;/listitem&gt;
  &lt;listitem&gt;Excitable Boy&lt;/listitem&gt;
  &lt;listitem xml:lang="sw"&gt;Aki Special&lt;/listitem&gt;
  &lt;listitem xml:lang="en-gb"&gt;Combat Rock&lt;/listitem&gt;
  &lt;listitem xml:lang="zu"&gt;Talking Timbuktu&lt;/listitem&gt;
  &lt;listitem xml:lang="jz"&gt;The Birth of the Cool&lt;/listitem&gt;
&lt;/list&gt;</pre></span>
<p>We'll create a table that has each <span class="LITERAL">&lt;listitem&gt;</span> in a separate row in the right column of the table, and a single cell with <span class="LITERAL">rowspan</span> equal to the number of <span class="LITERAL">&lt;listitem&gt;</span> elements in the XML document on the left. Clearly we can't hardcode a value for the <span class="LITERAL">rowspan</span> attribute because the number of <span class="LITERAL">&lt;listitem&gt;</span>s can change. This stylesheet uses <span class="LITERAL">&lt;xsl:attribute&gt;</span> to do what we want:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  &lt;xsl:output method="html"/&gt;

  &lt;xsl:template match="/"&gt;
    &lt;html&gt;
      &lt;head&gt;
        &lt;title&gt;&lt;xsl:value-of select="list/title"/&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;xsl:apply-templates select="list"/&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  &lt;/xsl:template&gt;

  &lt;xsl:template match="list"&gt;
    &lt;table border="1" width="75%"&gt;
      &lt;tr&gt;
        &lt;td bgcolor="lightslategray" width="100" align="right"&gt;
          &lt;xsl:attribute name="rowspan"&gt;
            &lt;xsl:value-of select="count(listitem)"/&gt;
          &lt;/xsl:attribute&gt;
          &lt;p style="font-size: 125%"&gt;
            &lt;xsl:value-of select="title"/&gt;
          &lt;/p&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;xsl:value-of select="listitem[1]"/&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;xsl:for-each select="listitem"&gt;
        &lt;xsl:if test="position() &gt; 1"&gt;
          &lt;tr&gt;
            &lt;td&gt;
              &lt;xsl:value-of select="."/&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/xsl:if&gt;
      &lt;/xsl:for-each&gt;
    &lt;/table&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</pre></span>
<p>Here is the generated HTML document:</p>
<span class="PROGRAMLISTING"><pre>
&lt;html&gt;
&lt;head&gt;
&lt;META http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;Albums I've bought recently:&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table width="75%" border="1"&gt;
&lt;tr&gt;
&lt;td align="right" width="100" rowspan="7" bgcolor="lightslategray"&gt;
&lt;p style="font-size: 125%"&gt;Albums I've bought recently:&lt;/p&gt;
&lt;/td&gt;&lt;td&gt;The Sacred Art of Dub&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Only the Poor Man Feel It&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Excitable Boy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Aki Special&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Combat Rock&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Talking Timbuktu&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The Birth of the Cool&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></span>
<p>Notice that the <span class="LITERAL">&lt;td&gt;</span> element had several attributes hardcoded on it; those attributes are combined with the attribute we created with <span class="LITERAL">&lt;xsl:attribute&gt;</span>. You can have as many <span class="LITERAL">&lt;xsl:attribute&gt;</span> elements as you want, but they must appear together as the first thing inside the element to which you add attributes. <link linkend="xslt-appa-a3">Figure A-3</link> shows how our generated HTML document looks.</p>
<figure label="A-3" id="xslt-appa-a3">
        <p class="TITLE">Document with generated Attributes</p>
        <graphic depth="231" width="405" fileref="figs/xslt.aa03.gif"/>
      </figure>
<p>Be aware that in this instance, we could have used an attribute-value template. You could generate the value of the <span class="LITERAL">rowspan</span> attribute like this:</p>
<span class="PROGRAMLISTING"><pre>
&lt;td bgcolor="lightslategray" rowspan="{count(listitem)}"
  width="100" align="right"&gt;</pre></span>
<p>The expression in curly braces (<span class="LITERAL">{}</span>) is evaluated and replaced with whatever its value happens to be. In this case, <span class="LITERAL">count(listitem)</span> returns the number <span class="LITERAL">7</span>, which becomes the value of the <span class="LITERAL">rowspan</span> attribute.</p>
</td>
</tr>
</table>
</div>
</body>
</html>