<xsl:attribute> |
Allows you to create an attribute in the output document. The advantage of <xsl:attribute> 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.
Category
Instruction
Required Attributes
name The name attribute defines the name of the attribute created by the < xsl:attribute > element. (No matter how you try to say this, talking about the attributes of the < xsl:attribute > element is confusing, isn't it?)
Optional Attributes
namespace The namespace 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 namespace attribute is the URI of the namespace.
Content
An XSLT template. In other words, you can build the contents of an attribute with <xsl:choose> elements, <xsl:text>, and <xsl:value-of> elements.
Appears in
<xsl:attribute> appears inside a template.
Defined in
XSLT section 7.1.3, Creating Attributes with xsl:attribute.
Example
For this example, we want to create an HTML table from the following XML document:
    <?xml version="1.0"?>
    <list xml:lang="en">
      <title>Albums I've bought recently:</title>
      <listitem>The Sacred Art of Dub</listitem>
      <listitem>Only the Poor Man Feel It</listitem>
      <listitem>Excitable Boy</listitem>
      <listitem xml:lang="sw">Aki Special</listitem>
      <listitem xml:lang="en-gb">Combat Rock</listitem>
      <listitem xml:lang="zu">Talking Timbuktu</listitem>
      <listitem xml:lang="jz">The Birth of the Cool</listitem>
    </list>
We'll create a table that has each <listitem> in a separate row in the right column of the table, and a single cell with rowspan equal to the number of <listitem> elements in the XML document on the left. Clearly we can't hardcode a value for the rowspan attribute because the number of <listitem>s can change. This stylesheet uses <xsl:attribute> to do what we want:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:output method="html"/>

      <xsl:template match="/">
        <html>
          <head>
            <title><xsl:value-of select="list/title"/></title>
          </head>
          <body>
            <xsl:apply-templates select="list"/>
          </body>
        </html>
      </xsl:template>

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

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