Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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:
…
Stylesheet
The stylesheet is in file
split.xsl.
The part to look at is the
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
version=“2.0”
so that an XSLT 1.0 processor will not reject
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).
Three further output files
verse1.xml
,
verse2.xml
, and
verse3.xml
are created in the same directory as this outline. Here is
verse1.xml
:
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
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
You can't use
element-available()
to test whether the XSLT 2.0 element
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
version=“2.0”
, then an XSLT 1.0 processor will ignore the
system-property()
function (described on page 890 in this chapter) together with the
use-when
attribute described in the entry for