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

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

It would again be possible to make these template rules generic across levels. In fact, the template rule shown above for

elements would produce exactly the right output if it were applied to a

or

element, because of the way that

is defined. The
format
attribute of

can also be parameterized using an attribute value template: in XSLT 2.0 one could write:


   

      count=“div1 | div2 | div3 | inform-div1”

      format=“{if (ancestor::back) then ‘A.1.1’ else ‘1.1.1’}”/>


However, it does no harm for the stylesheet author to spell things out more explicitly.

The templates for producing section numbers in the table of contents are reused, of course, when producing the section numbers in the body of the document. I'll describe how this is done in the next section.

Creating Section Headers

We'll now look at the template rules used to format the section headers. These all have the same structure, and they reuse components we have already seen: the named
anchor
template that generates the target of a hyperlink, and the
divnum
mode that produces the section number for any given section. Here are the first two:


  

    

      

      

    

    

    

  



  

    

      

      

    

    

    

  


It would be entirely possible to use a single generic template by replacing the literal result element
N
>
with the construct:


This uses the XPath 2.0
replace()
function, but the same logic could be written almost as easily by using XPath 1.0 functions such as
concat()
and
substring-after()
. Another way to avoid repetition of code between these templates would be to write separate template rules at the top level and call a common component to produce the inner content:


    

       

    



    

       

    



    

        

        

    

    

    


Yet another approach would be for the common template rule to be invoked using

rather than by using a separate mode.

Formatting the Text

The bulk of the stylesheet is taken up with template rules to process simple textual markup within the body of the document. Most of these are very straightforward, and to avoid tedious repetition I will show only a small sample of them.

Probably the most common element is the


element, which marks a paragraph, as in HTML:


  


    

      

        

      

    

    

      

        

      

    

    

  



You've probably got the message by now that I don't much like unnecessary verbosity. The first thing I notice about this template rule is that the five lines:


   

     

   


are equivalent to the single line:


The next is that

renames the
role
attribute as
class
, so it's less easy to simplify, though with XSLT 2.0 you can reduce it to:


  


But the essential structure of this template rule is typical of many others: it translates one element in the source document into one element in the result document, making minor adjustments to the attributes, and then calls

to process the content of the element. This is the typical style of a rule-based stylesheet. Here are some other simple examples of such rules:



  

    

  




  




  




  




  






  •   

      

      




      

      


    Other books

    All the Way Home by Patricia Reilly Giff
    Sobre héroes y tumbas by Ernesto Sabato
    The Kite Fighters by Linda Sue Park
    A History of Money: A Novel by Alan Pauls, Ellie Robins
    The Stolen Suitor by Eli Easton