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

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

Nine lines of code to generate “Appendix” or “Appendices”! In XSLT 2.0, that becomes

                      then ‘Appendix’ else ‘Appendices’”/>

Let's see how the table of contents is produced. The structure of the

element, and also of

, consists of a sequence of

elements representing top-level sections, like this:


   First-level heading

   

Some text


   

      Second-level heading

      

Some more text


      

         Third-level heading

         

Lots more text


      

   


Each

element contains a

element giving its section title, paragraphs of immediate content, and zero or more

elements containing level-2 subsections. The

elements similarly contain a

and zero or more

elements for level-3 subsections, and so on.

In the

section, a non-normative appendix is represented by an

element instead of the usual

, but otherwise the structure is the same.

Non-normative is jargon meaning “for information only, not officially part of the specification.”

The template rule that generates an entry for a top-level section in the table of contents looks like this:

This starts by applying templates to itself with mode
divnum
; this invokes a template to calculate the section number. We'll take a look at this template rule shortly. The rule then generates an HTML

element to produce a hyperlink. The
href
attribute is generated by calling a named template
href.target
, with the current node (the

element) as a parameter. The content of the

element (the displayed text that the user clicks on) is produced by applying templates to the

element, with the special mode
text
. The stylesheet doesn't actually contain a template rule for this mode; it is used simply to invoke the built-in template rule, which returns the textual content of the

element, minus any markup.

The
href.target
template looks like this:

To my mind this is crying out to be replaced by an XSLT 2.0 function, something like:

This would allow the code:

to be rewritten as:


  


If the global parameter
$toc.level
is greater than one, then the

elements that are children of this

are processed in mode
toc
to generate another level in the table of contents.

The template rule for

elements in mode
toc
and the template rules for further levels, such as

, are very similar to the

rule. They differ only in that they apply templates to the next level down (the

template processes the

children, and so on) and in the amount of indentation added before the section number—this is added crudely in the form of four nonbreaking spaces per level, using the instruction:

    

Hexadecimal
a0
(decimal 160) is the Unicode code for the nonbreaking space character, better known to HTML authors as the entity reference
 
. This is not available as a built-in entity in XML. It is possible to define it as an entity in the DTD, but the authors of this stylesheet chose to write it explicitly as a numeric character reference.

Other books

The Turning Tide by Brooke Magnanti
The Hidden by Jo Chumas
Shana Abe by A Rose in Winter
Mesmerized by Julia Crane
Out of the Dust by Karen Hesse
Whirlwind by Robin DeJarnett
The Dark Reaches by Kristin Landon
Raising Hell by Robert Masello
Masks by Laurie Halse Anderson