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

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

It wouldn't be difficult to write a parameterized template that handled all the

elements in one rule, but the alternative approach of writing five separate rules is perfectly defensible.

The templates to calculate section numbers have one variant for each level of heading, and also vary depending on whether the section is in the

(a main section) or in the

matter (an appendix). Here are some of them:



  



  



  



  



  



  

    count=“div1 | div2 | div3 | inform-div1” format=“A.1.1 ”/>


All these templates work by calling

with appropriate parameters. The default
level=“single”
is used for the top-level headings, and
level=“multiple”
for all other levels, with a
count
attribute that matches that level and all ancestor levels. The format of the numbering is adjusted for appendices (sections with

as an ancestor) to use alphabetic identifiers (A, B, C, . . . ) for the first component of the number.

Giving a list of alternatives in the
count
attribute is a common way of doing multilevel numbering. It means, in effect, outputting a sequence number for each ancestor element that is either an

or a

or a

and so on. Like most template rules in a rule-based stylesheet, it doesn't attempt to do any validation: if the input structure is wrong, it will produce some sort of output nevertheless, and it's up to the document author to work out what the problem is. This raises an interesting question that you need to consider when designing your own stylesheets: Is it the job of the stylesheet to detect and report on errors in the source document?

The use of

as a separate tag for non-normative appendices was a pretty clumsy design decision, and the stylesheet author has to pay the price here. It would have been much cleaner to give the

element an attribute
normative=“no”
. Sadly, it is often the case that stylesheet authors have to cope with XML structures that could have been designed better. In XSLT 2.0, if this stylesheet were schema aware, it's likely that

would be in the substitution group of

, and it would then be possible to replace all references to
div1
in these template rules by
schema-element(div1)
, which would pick up the

elements automatically.

Other books

Code Zero by Jonathan Maberry
Night of the Vampires by Heather Graham
ACV's 1 Operation Black Gold by J Murison, Jeannie Michaud