5.6. Associating an Instance File to a Stylesheet

Transforming an instance file with a stylesheet is most often performed by externally specifying the transformation stylesheet file to apply to a given instance file. This can be achieved using an XML editor in which we can associate an XML file with a stylesheet (and vice versa). Some editors also allow the definition of many transformation scenarios. The transformation can also be done in batch mode by specifying a stylesheet to a transformation engine. For example, in the companion website of this report, we show a simple Java program which can be used as a Unix filter to standard input. We can get on the standard output the compact text output of the wine catalog with the following call:

java Transform compact.xsl < WineCatalog.xml

This program also allows to specify run-time parameters to the stylesheet (declared with top-level xsl:param elements as shown in Section 5.2.1). To get an HTML file with the table of the white wines of the catalog (similar to Example 5.1), we can use the following:

java Transform WineCatalog.xsl color white < WineCatalog.xml > whites.html

It is also possible to use other transformation program directly from the command line xsltproc (but it currently deals only with XSL 1.0 stylesheets), SAXON or XALAN for which it is necessary to set your CLASSPATH appropriately.

xsltproc --stringparam color white WineCatalog.xsl WineCatalog.xml > whites.html
java net.sf.saxon.Transform -s WineCatalog.xml WineCatalog.xsl color=white \
    > whites.html
java org.apache.xalan.xslt.Process -XSL WineCatalog.xsl -IN WineCatalog.xml \
    -OUT whites.html -PARAM color white

Because stylesheets can also be interpreted by web browsers, it is also possible to link an instance file directly to a stylesheet by means of the xml-stylesheet processing instruction. For example, if one adds the following at the beginning of Example 2.3

<?xml-stylesheet type="text/xsl" href="compactHTML.xsl"?>

then, upon loading the XML file WineCatalog.xml into a web browser, the catalog will be displayed after the transformation defined by Example 5.7. But one must be careful before relying on such automatic transformations, because not all browsers implement all XSL transformations, especially those of XSLT 2.0.

<?xml-stylesheet type="text/css" href="compact.css"?>

is the way to make link with a CSS in an XML or HTML page. Even though all browsers implement CSS formatting, there are still some inconsistencies between platforms.