Running an XSLT processor - Page 6
October 26, 2001
The XSLT specification intentionally avoids saying "here is how
you apply stylesheet A to input document B in order to create
output document C." This leaves plenty of flexibility for the
developers who create XSLT processors. The input, output, and
stylesheet filenames might be entered in a dialog box; or they
might be entered at a command line; or they might be read from a
file.
Many XSLT processors are designed to give a range of options when
identifying the inputs and outputs. Some are programming
libraries which you can invoke from a command line or call from
within a program. For example, an XSLT processor supplied as a
Java class library often includes instructions for using it from
a Windows or Linux command line, but its real power comes from
your ability to call it from your own Java code. This way, you
can write a Java program that gets the input, stylesheet, and
output filenames either from a dialog box that you design
yourself, from a file sitting on a disk, or from another XML file
that your application uses to control your production processes.
Your program can then hand this information to the XSLT
processor.
In fact, the input, stylesheet, and output don’t even have to be
files. Your program may create them in memory or read them from
and write them to a process communicating with another computer.
The possibilities are endless, because the input and output
details are separate from the transformation.
One example of a Java library that you can use from the command
line or as part of a larger Java application is Xalan (pronounced
"Zalan"). This XSLT processor was written at IBM and donated to
the Apache XML project (http://xml.apache.org). To run release
2.0 of this particular XSLT processor from the command line with
an input file of winelist.xml, a stylesheet of winesale.xsl, and
an output file of winesale.xml, enter the following as one line
(to fit on this page, the example below is split into two lines):
java org.apache.xalan.xslt.Process -in winelist.xml
-xsl winesale.xsl -out winesale.xml
(This assumes that the appropriate Java libraries and system
paths have been set up; directions come with each Java
processor.) For examples of how to run other XSLT processors, see
appendix A, "XSLT quick reference" on page 259.
Figure 1.6 The XSLT processor reads an
input XML file and an XSLT stylesheet and outputs another file
based on the stylesheet’s instructions.
An Empty Stylesheet
What would an XSLT processor do with a stylesheet that contained
no template rules? In other words, what effect would an empty
stylesheet, such as the following, have on an input document?
<!-- xq21.xsl: converts xq22.xml into xq23.xml -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"/>
XSLT has several built-in default templates that tell the XSLT
processor to output the text content (or, in XML terms, the
PCDATA) of the elements, leaving out the attributes and markup.
For example, the processor would turn this
<winelist date="20010626">
<wine grape="chardonnay">
<product>Carneros</product>
<year>1997</year>
<price>10.99</price>
</wine>
</winelist>
into this:
<?xml version="1.0" encoding="UTF-8"?>
Carneros
1997
10.99
Tip: An XSLT processor’s default behavior adds an XML declaration as well.
This can be overridden with the optional xsl:output element if you
want to create HTML, plain text, or other non-XML output.
The built-in templates also tell the XSLT processor to apply any
relevant templates to the children of the elements being
processed. (Otherwise, when the wine-list element in
the above example gets processed, no reason would exist for the
XSLT processor to do anything with the wine element.) If the
stylesheet has no template for one of the children, the most
appropriate template for that child element may be the same
built-in template that the processor applied to the child’s
parent. The XSLT processor will do the same thing to the child:
add any character data nodes to the result tree and apply the
most appropriate templates to any grandchildren elements.
Template Rules - Page 5
XSLT Quickly
More Element and Attribute Manipulation - Page 7
|