Using XSLT from the Command Line

Before we learn how to embed XSLT transformations in Python programs, we need to concentrate on learning more about XSLT itself. As we’re learning, it will generally be easier to run our transformations from the command line than from a Python script. Many of the processors provide a command-line tool for performing transformations. We use the 4XSLT package provided as part of 4Suite; if you choose to use a different tool, please consult its documentation to determine how to use it.

4XSLT includes a script that performs transformations from the command line. On Windows, the 4xslt.bat script is installed in the Scripts directory of your Python installation by the 4Suite installer. To make the script more easily usable, either add the Scripts directory to your PATH environment variable or copy the 4xslt.bat file to a directory that is already included in the PATH.

The basic operation of 4xslt simply requires two parameters: the XML document to transform, and the stylesheet to apply. This example was used to apply the stylesheet from Example 6-3 to produce the output shown in Example 6-4:

C:\my-dir> 4xslt ships.xml ships.xsl > ships2.html

Output redirection is used to save the result of the transformation to a file.

4XSLT and the 4xslt script support the use of both simplified and standalone stylesheets. Embedded stylesheets are not supported.

Get Python & XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.