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

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

Here is an example that tests to see whether the new XSLT 2.0 facility to produce multiple output documents is available by testing
element-available(‘xsl:result-document’)
. If it isn't available, you can use an alternative approach; for example, you can use the proprietary syntax for this functionality offered by your chosen XSLT 1.0 processor.

Example: Creating Multiple Output Files

This example takes a poem as input, and outputs each stanza to a separate file. A more realistic example would be to split a book into its chapters, but I wanted to keep the files small. The example is written to work with Saxon version 6.x (which implements XSLT 1.0) and also with any processor that conforms to the XSLT 2.0 specification.

Source

The source file is
poem.xml
. It starts:


Rupert Brooke

1912

Song


And suddenly the wind comes soft,

And Spring is here again;

And the hawthorn quickens with buds of green

And my heart with buds of pain.



My heart all Winter lay so numb,

The earth so dead and frore,


Stylesheet

The stylesheet is in file
split.xsl.
The part to look at is the

instruction, where each branch calls
element-available()
to test whether a particular instruction is available, before calling that instruction.

Note that
saxon
is defined as an extension element prefix, so the

element is recognized as an instruction. The

element specifies
version=“2.0”
so that an XSLT 1.0 processor will not reject

as an error.


    version=“2.0”>


    

        

        

        

        

    



    

                  select=“concat(‘verse’, string(position()), ‘.xml’)”/>

    

    

    

        

            

        

    

    

              xmlns:saxon=“http://icl.com/saxon”>

        

                      xsl:extension-element-prefixes=“saxon”>

            

        

    

    

        

             >Cannot write to multiple output files

    

    



Output

Provided the stylesheet is run with one of the processors that support the required capability, the principal output file will contain the following skeletal poem (new lines added for legibility).



Song

Rupert Brooke

1912





Three further output files
verse1.xml
,
verse2.xml
, and
verse3.xml
are created in the same directory as this outline. Here is
verse1.xml
:



And suddenly the wind comes soft,

And Spring is here again;

And the hawthorn quickens with buds of green

And my heart with buds of pain.


To run this using Saxon 9.x (which implements XSLT 2.0), use a command line of the form:

java -jar c:\MyJava\saxon9.jar -t -o:c:\temp\outline.xml poem.xml split.xsl

With this command line, the output files will all be written to the directory
c:\temp
. The

instruction in Saxon 6.x works slightly differently: it will write the files
verseN.xml
to the current directory, not necessarily to the directory containing the
outline.xml
file.

Note that in this stylesheet, all the information needed to evaluate the

conditions is available at compile time. A good XSLT processor will generate code only for the path that is actually going to be executed.

You can't use
element-available()
to test whether the XSLT 2.0 element

is available, because this is a declaration rather than an instruction, and a conformant XSLT 2.0 processor should therefore return
false
(the same result as an XSLT 1.0 processor). If you use this element in an XSLT 1.0 stylesheet, an error will be reported if you specify
version=“1.0”
on the

element, but if you specify
version=“2.0”
, then an XSLT 1.0 processor will ignore the

element, under the rules for forward-compatible processing. Instead you can test whether the XSLT processor you are using is schema-aware using the
system-property()
function (described on page 890 in this chapter) together with the
use-when
attribute described in the entry for

in Chapter 6.

Other books

Ascension by Hannah Youngwirth
The Lost City of Z by David Grann
Delphi by Scott, Michael
Lucinda by Paige Mallory