Attribute Set Example

Figure 22. attrset.xsl

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="html"/>

<xsl:attribute-set name="table-cell-attrs">
  <xsl:attribute name="align">left</xsl:attribute>
  <xsl:attribute name="valign">top</xsl:attribute>
  <xsl:attribute name="bordercolor">blue</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="doc">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="table">
  <xsl:if test="@id"><a name="{@id}"/></xsl:if>
  <xsl:comment>Attributes created with xsl:attribute-set</xsl:comment>
  <table border="1">
    <xsl:apply-templates/>
  </table>
</xsl:template>

<xsl:template match="head|cell">
  <xsl:variable name="output-element">
    <xsl:choose>
      <xsl:when test="local-name(.)='head'">th</xsl:when>
      <xsl:otherwise>td</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:element name="{$output-element}" use-attribute-sets="table-cell-attrs">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="row">
  <tr>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </tr>
</xsl:template>

<xsl:template match="link">
  <a>
    <xsl:attribute name="href">
      <xsl:text>#</xsl:text>
      <xsl:value-of select="@linkend"/>
    </xsl:attribute>
    <xsl:apply-templates/>
  </a>
</xsl:template>

<xsl:template match="para">
  <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="emphasis">
  <i><xsl:apply-templates/></i>
</xsl:template>

</xsl:stylesheet>


Figure 23. attrset.html

<p>This is some paragraph of text. See 
<a href="#foo">the table</a>.</p>

<a name="foo"></a><!--Attributes created with xsl:attribute-set-->
<table border="1">
<tr>
<th align="left" valign="top" bordercolor="blue">Head 1</th><th align="left" valign="top" bordercolor="blue">Head 2</th>
</tr>
<tr>
<td align="center" valign="top" bordercolor="blue">Value 1</td><td align="left" valign="top" bordercolor="blue">Value 2</td>
</tr>
<tr>
<td align="left" valign="top" bordercolor="blue">Value 3</td><td align="left" valign="top" bordercolor="blue">Value 3</td>
</tr>
<tr>
<td align="left" valign="top" bordercolor="blue">Value 5</td><td align="left" valign="top" bordercolor="blue">Value 4</td>
</tr>
</table>