6.4. The <xsl:attribute-set> Top-Level Element

The <xsl:attribute-set> top-level element provides the ability to set up a group of <xsl:attribute> elements that generate attributes that can be inserted collectively into an element in the result tree.

The <xsl:attribute-set> element has two attributes: the name attribute, which is used to name and call the attribute set created, and the use-attribute-set attribute. The allowed content of the <xsl:attribute-set> element is zero or more <xsl:attribute> elements, as shown in the following element model definition:

<!-- Category: top-level-element -->

<xsl:attribute-set

  name = qname

  use-attribute-sets = qnames>

  <!-- Content: xsl:attribute* -->

</xsl:attribute-set>

Having a content of zero or more elements means that the <xsl:attribute-set> element can be an empty element, but it has the ability to call other <xsl:attribute-set> elements and include their content as its own. Note that an empty <xsl:attribute-set> element with no use-attribute-sets attribute is perfectly valid, but not very useful. A sample DTD element and attribute declaration detailing the structure of <xsl:attribute-set> is shown below.

<!ELEMENT xsl:attribute-set (xsl:attribute)*>
<!ATTLIST xsl:attribute-set
    name %qname; #REQUIRED
    use-attribute-sets %qnames; #IMPLIED >

6.4.1. The name Attribute

The value of the name attribute for <xsl:attribute-set> is a QName that is used to name the group of attributes. The value of the name attribute is the same as the value used for use-attribute-sets when the attribute set is being called.

ATTRIBUTE:  name NMTOKEN #REQUIRED
VALUE = QName
					

6.4.2. The use-attribute-sets Attribute

The attribute use-attribute-sets is used to access the content of an <xsl:attribute-set> and send the resulting attributes and their values to the result tree. This attribute is also found on two other XSLT elements, <xsl:element> and <xsl:copy>, and can be used by LRE elements if prefixed with the xsl namespace prefix (xsl:use-attribute-sets):

ATTRIBUTE:  use-attribute-sets NMTOKENS #IMPLIED
VALUE = QNames
					

When used on an <xsl:attribute-set> element, the use-attribute-sets attribute serves to sort of nest attribute sets. Any attribute set called by another <xsl:attribute-set> is placed before the ones that the calling set defines, if any. For example, if an <xsl:attribute-set> named “calling-set”called an <xsl:attribute-set> named “boilerplate-set,” the attributes in the “boilerplate-set” would appear before the attributes in “calling-set,” as shown below.

<xsl:attribute-set name="calling-set" use-attribute-sets="boilerplate-
set" >
       <xsl:attribute name="att1">Attribute1</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="boilerplate-set">
       <xsl:attribute name="att2">Attribute2</xsl:attribute>
</xsl:attribute-set>

The result of combining these two attribute sets (to the processor) is a single <xsl:attribute-set>, with the <xsl:attribute> elements placed in the calling set as if they were in the same set, but the called attributes appear first, as shown below.

<xsl:attribute-set name="calling-set">
      <xsl:attribute name="att2">Attribute2
      </xsl:attribute>
<xsl:attribute name="att1">Attribute1
</xsl:attribute>
</xsl:attribute-set>

Of course, you would not see the above combined set because it is an example of how the XSLT processor sees the two sets during processing.

6.4.3. Using Attribute Sets with <xsl:attribute-set>

Often in the course of writing an XSLT stylesheet, it is necessary to create additional elements and attributes in the output result tree. There are many times when the same attributes are needed over and over again, although usually with different values. It is therefore convenient to declare such groups of attributes that are likely to be repeatedly utilized in a group, so that they can be inserted all at once by a single named reference, not unlike a macro or subroutine.

A common use for <xsl:attribute-set> would be to have a set of attributes defined for different types of table styles, one with borders and one without in typical HTML style. With our table formatting example for Markup City, it is now possible to call the defined set of attributes by name, as shown in Example 6-8.

The <table> element that is created contains the new attributes for border, cellpadding and cellspacing, as selected with the use-attribute-sets. The table can easily be converted from a table with borders to a table with no borders by changing the attribute value of use-attribute-sets from bordered_table to plain_table.

Example 6-8. Two attribute sets for bordered and unbordered HTML tables, called by the use-attribute-sets attribute.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/
Transform"
             version="1.0">
<!-- attribute set definitions -->
<xsl:attribute-set name="plain_table">
      <xsl:attribute name="border">0</xsl:attribute>
      <xsl:attribute name="cellpadding">3</xsl:attribute>
      <xsl:attribute name="cellspacing">4</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="bordered_table">
      <xsl:attribute name="border">1</xsl:attribute>
      <xsl:attribute name="cellpadding">2</xsl:attribute>
      <xsl:attribute name="cellspacing">3</xsl:attribute>
</xsl:attribute-set>

<!-- template rules -->
      <xsl:template match="block" >
             <xsl:call-template name="table-cell"/>
</xsl:template>
<xsl:template match="boulevard">
      <xsl:call-template name="table-row"/>
</xsl:template>

<xsl:template match="thoroughfare">
      <xsl:call-template name="table-row"/>
</xsl:template>

<xsl:template match="sidestreet">
      <xsl:call-template name="table-cell"/>
</xsl:template>
<xsl:template match="main">
<xsl:element name="table" use-attribute-
sets="bordered_table">
                 <xsl:apply-templates/>
      </xsl:element>
      </xsl:template>
      <xsl:template match="/">
            <html>
            <head><title>Table Example</title>
            <head>
            <body>
                   <xsl:apply-templates/>
            </body>
            </html>
      </xsl:template>

<xsl:template name="table-row">
      <tr>
      <xsl:apply-templates/>
      </tr>
</xsl:template>
<xsl:template name="table-cell">
      <td>
      <xsl:apply-templates/>
      </td>
</xsl:template>
            </xsl:stylesheet>

Example 6-9 shows the use of attribute sets which applies to LREs: you can apply attribute sets, using the use-attribute-set attribute in the LRE itself, by adding the xsl prefix.

If a series or set of attributes is to be repeatedly used throughout an XSLT stylesheet, you can declare it once and invoke it by a convenient name. This decreases the likelihood of error in cutting and pasting or, worse, retyping. Further, the values and the attribute names themselves can be generated based on the input XML data instance, adding flexibility to the output.

Example 6-9. Using the attribute xsl:use-attribute-set in an LRE
<xsl:template match="main">
    <table xsl:use-attribute-sets="plain_table">
          <xsl:apply-templates/>
    </table>
</xsl:template>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset