<xsl:attribute-set>

Jean-François Perrot


  1. Principle
  2. Basic Example
  3. Incremental construction
  4. Inheritance
  5. Overriding


  1. Principle

    Used in generating XML output (especially for fo output).
    Allows to declare a bunch of attributes to be reused several times, instead of repeating the individual attributes.
    Also, allows a synthetic way of defining reusable formats.

    Compatible with an incremental construction via imports (widely used for DocBook processing).
  2. Basic Example

    adaptd from [Ref]

    Object document : book.xml

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="attrset.xsl" ?>
    <book>
       <chapter>
          <heading>The First Heading</heading>
       </chapter>
       <chapter>
          <heading>The Next Heading</heading>
       </chapter>
    </book>



    XSLT Transform attr.xsl

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

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <fo:root>
            <xsl:apply-templates />
        </fo:root>
    </xsl:template>
       
    <xsl:template match="//chapter/heading">
      <fo:block quadding="start" xsl:use-attribute-sets="title-style">
    <!-- Note the need of the explicit prefix
    xsl:use... because we are in the fo: namespace -->
        <xsl:apply-templates/>
      </fo:block>
    </xsl:template>

    <xsl:attribute-set name="title-style">
      <xsl:attribute name="font-size">12pt</xsl:attribute>
      <xsl:attribute name="font-weight">bold</xsl:attribute>
    </xsl:attribute-set>

    </xsl:stylesheet>



    Result of 'xsltproc attr.xsl book.xml'

    <?xml version="1.0"?>
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      
          <fo:block quadding="start" font-size="12pt" font-weight="bold">The First Heading</fo:block>
      
          <fo:block quadding="start" font-size="12pt" font-weight="bold">The Next Heading</fo:block>
      
    </fo:root>



  3. Incremental construction

    It is possible to build an attribute-set in several steps.
    The following stylesheet is equivalent to the previous one :

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

    <xsl:output method="xml" indent="yes" />

    <!-- Original definition of 'title-style' -->
    <xsl:attribute-set name="title-style">
      <xsl:attribute name="font-size">12pt</xsl:attribute>
    </xsl:attribute-set>

    <!-- Processing -->
    <xsl:template match="/">
        <fo:root>
            <xsl:apply-templates />
        </fo:root>
    </xsl:template>
       
    <xsl:template match="//chapter/heading">
      <fo:block quadding="start" xsl:use-attribute-sets="title-style">
        <xsl:apply-templates/>
      </fo:block>
    </xsl:template>

    <!-- Addition to 'title-style' -->
    <xsl:attribute-set name="title-style">
      <xsl:attribute name="font-weight">bold</xsl:attribute>
    </xsl:attribute-set>

    </xsl:stylesheet>



  4. Inheritance

    The use-attribute-sets attribute is also available for attribute-sets themselves, which leads to a form of inheritance.
    The following stylesheet is again equivalent to the previous one :

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

    <xsl:output method="xml" indent="yes" />

    <!-- General 'my-style' -->
    <xsl:attribute-set name="my-style">
      <xsl:attribute name="font-size">12pt</xsl:attribute>
    </xsl:attribute-set>

    <!-- Processing -->
    <xsl:template match="/">
        <fo:root>
            <xsl:apply-templates />
        </fo:root>
    </xsl:template>
       
    <xsl:template match="//chapter/heading">
      <fo:block quadding="start" xsl:use-attribute-sets="title-style">
        <xsl:apply-templates/>
      </fo:block>
    </xsl:template>

    <!-- Specialization 'title-style' -->
    <xsl:attribute-set name="title-style" use-attribute-sets="my-style">
    <!-- Note that the xsl: prefix must not be used here, because we are in the xsl namespace -->
      <xsl:attribute name="font-weight">bold</xsl:attribute>
    </xsl:attribute-set>

    </xsl:stylesheet>



  5. Overriding

    As usual, both forms of incremental contruction lend themselves to overriding.

    Imported stylesheet attr1.xsl

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

    <xsl:output method="xml" indent="yes" />

    <!-- General 'my-style' -->
    <xsl:attribute-set name="my-style">
      <xsl:attribute name="font-size">12pt</xsl:attribute>
    </xsl:attribute-set>

    <!-- Processing -->
    <xsl:template match="/">
        <fo:root>
            <xsl:apply-templates />
        </fo:root>
    </xsl:template>
       
    <xsl:template match="//chapter/heading">
      <fo:block quadding="start" xsl:use-attribute-sets="title-style">
        <xsl:apply-templates/>
      </fo:block>
    </xsl:template>

    <!-- Specialization 'title-style' -->
    <xsl:attribute-set name="title-style" use-attribute-sets="my-style">
      <xsl:attribute name="font-weight">bold</xsl:attribute>
      <xsl:attribute name="font-size">21pt</xsl:attribute> <!-- override -->
    </xsl:attribute-set>

    </xsl:stylesheet>



    Operational stylesheet attr2.xsl

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

    <xsl:import href="attr1.xsl" />

    <xsl:attribute-set name="title-style" use-attribute-sets="my-style">
      <xsl:attribute name="font-weight">normal</xsl:attribute> <!-- override -->
    </xsl:attribute-set>

    </xsl:stylesheet>



    Result of 'xsltproc attr2.xsl book.xml'

    <?xml version="1.0"?>
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      
          <fo:block quadding="start" font-weight="normal" font-size="21pt">The First Heading</fo:block>
      
      
          <fo:block quadding="start" font-weight="normal" font-size="21pt">The Next Heading</fo:block>
      
    </fo:root>