[Previous] [Contents] [Next]


Numbering


You can automatically number elements in the XML input file by using the xsl:number element.

You can control the numbering using the level, count, and from attributes:

When level="single" is used, the count starts at the nearest ancestor (including the current node as its own ancestor) that matches the count pattern. It constructs a list of length one containing one plus the number of preceding siblings of that ancestor that match the count pattern. If there is no such ancestor, the list is empty. If the from attribute is specified, the only ancestors that are searched are those that are descendants of the nearest ancestor that matches the from pattern.
When level="multi" is used, all the ancestors of the current node in document order followed by the element itself are counted first. The elements that match the count pattern are selected and mapped to one plus the number of preceding siblings of that element that match the count pattern. If the from attribute is specified, then the only ancestors that are searched are those that are descendants of the nearest ancestor that matches the from pattern.
When level="any" is used, the count contains one plus the number of elements at any level of the document that start before this node and that match the count pattern. If the from attribute is specified, then only the elements after the first element that come before this element and match the from pattern are counted.

As an example, the following XSL template numbers the item elements in a list:

<xsl:template match="list/item">
         <fo:block>
          <xsl:number/>
          <xsl:text>. </xsl:text>
          <xsl:process-children/>
         </fo:block>
      <xsl:template>

To illustrate a more practical use of numbering, Listing 20.16 shows two rules for numbering title elements. This is intended for a document that contains a sequence of chapters followed by a sequence of appendices, where both chapters and appendices contain sections with subsections. Chapters are numbered 1, 2, 3. Appendices are numbered A, B, C. Sections in chapters are numbered 1.1, 1.2, 1.3 and sections in appendices are numbered A.1, A.2, A.3.

Listing 20.16 Using xsl:number to Create Multi-Level Numbering


1:  <xsl:template match="title">
2:    <fo:block>
3:       <xsl:number level="multi"
4:            count="chapter | section | subsection"
5:            format="1.1. "/>
6:       <xsl:apply-templates/>
7:    </fo:block>
8:  </xsl:template>
9:
10: <xsl:template match="appendix//title">
11:   <fo:block>
12:      <xsl:number level="multi"
13:         count="appendix | section | subsection"
14:         format="A.1. "/>
15:      <xsl:apply-templates/>
16:   </fo:block>
17: </xsl:template>


[Previous] [Contents] [Next]