XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (212 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
7.45Mb size Format: txt, pdf, ePub

level = “any”

This option is useful when numbering objects within a document that have a numbering sequence of their own, independent of their position within the hierarchic structure. Examples are figures and illustrations, tables, equations, footnotes, and actions from a meeting.

The
count
attribute can usually be defaulted. For example, to number quotations within a document, you can write a template rule such as:


  


  

     

  


  



Again, the
count
attribute is useful when several different element types are included in the same numbering sequence, for example there might be a single sequence that includes both diagrams and photographs.

Note that each evaluation of

is quite independent of any previous evaluations. The result depends only on the relative position of the selected element in the source document, and not on how many times the

element has been evaluated. So there is no guarantee that the numbers in the output document will be consecutive. In fact, if the output order is different from the input order then the numbers definitely won't be consecutive. If you want to number things based on their position in the output document, you can often achieve this by using the
position()
function. If this isn't adequate, the alternative is to perform a second pass, to add the sequence numbers. You can do this by writing the result of the first pass to a variable. The following example extracts all the

elements from a document, sorts them alphabetically, and numbers them in their output sequence. The variable
glossary
is used to hold the temporary results. Note that if you want to use

for the numbering, this needs to be a tree rather than a flat sequence of parentless elements, because

always numbers nodes in terms of their position in a tree.

Imagine a source document that contains glossary definitions scattered throughout the document, in the form:


     XML

     Extensible Markup Language


The relevant template looks like this:


  

    

      

          

  

  


    

     


       


       


       


     


    

  



In this example, however, the numbers could have been generated equally well on the first pass using the
position()
function.

The
from
attribute is useful for indicating where numbering should restart:


   

    

   


The above code would number footnotes consecutively within a chapter, starting again at 1 for each chapter.

Example: Numbering the Lines of a Poem

The following example numbers the lines of a poem, showing the number to the right of every fourth line. Assume the input structure contains a

element, a

element, and a

element: The lines are to be numbered within the poem as a whole, not within each stanza.

Source

This stylesheet can be used with the source file
theHill.xml,
shown in Chapter 4 on page 167.

Stylesheet

This stylesheet is
poem.xsl
. It uses

to get the number of every line but displays it only every fourth line, using the
mod
operator to get the remainder when the line number is divided by 4 (or some other number supplied as a parameter).

     xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

     xmlns:xs=“http://www.w3.org/2001/XMLSchema”

     xpath-default-namespace=“http://poetry.org/ns”>

    

    



  <xsl:value-of select=“/poem/title”/>

  

    

    


  




  




  

    

    

       

          

       

       

          

       

    

  



Output

See
Figure 6-9

level = “multiple”

This option is typically used to produce the hierarchic sequence numbers often found in technical or legal documents; for example, 1.12.3, or A2(iii).

Note that an alternative way to produce such numbers is to use several calls on

with
level=“single”
and different
count
attributes, for example:

.

   ()

Another technique, which might be marginally faster, is to evaluate the chapter number once and pass it as a parameter to the template handling the section, and then pass both the chapter number and section number (or their concatenation) as parameters to the template handling each clause.

However, using
level=“multiple”
is convenient, and in some cases—particularly with recursive structures, where


elements are contained within

elements—may be the only way of achieving the required effect.

Other books

The Elusive Bride by Stephanie Laurens
The Bride Wore Denim by Lizbeth Selvig
Sizzle All Day by Geralyn Dawson
3. A Second Chance by Jodi Taylor
CHIP OFF THE OLD BLOCK by Sahara Foley
A Changing Land by Nicole Alexander
Storm Warning by Kadi Dillon
Billionaire's Revenge by Kelly, Marie