transform – Transform XML

transform is a simple command-line script to apply XSLT [1] stylesheets to an XML source. If you need a command-line XSLT processor with more options have a look at xsltproc

Examples

Output a transformed XML file with syntax highlighting like ppx:

transform stylesheet.xsl file.xml

Transform an URL:

curl -s https://example.com/path/file.xml | transform stylesheet.xsl

Save transformed XML to a file:

transform stylesheet.xsl source.xml --file new.xml

Options

transform can be used with the following command-line options:

$ transform --help

usage: transform [-h] [-V] [-f FILE | -s] [-o] xslt_source [xml_source]

Transform an XML source with XSLT.

positional arguments:
  xslt_source           XSLT source (file, http://...)
  xml_source            XML source (file, <stdin>, http://...)

options:
  -h, --help            show this help message and exit
  -V, --version         show program's version number and exit
  -f FILE, --file FILE  save result to file

terminal output options:
  -n, --no-syntax       no syntax highlighting
  -o, --omit-declaration
                        omit the XML declaration

Save result to file

-f FILE, --file FILE

Example stylesheet that converts an XML document to UTF-16 encoding:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0" id="utf16"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" version="1.0" encoding="UTF-16" indent="yes" />

  <xsl:template match="/">
   <xsl:copy-of select="." />
  </xsl:template>

</xsl:stylesheet>

Save the transformation result to a little-endian UTF-16 XML file.

transform to_utf16.xsl utf8.xml --file utf16.xml

Save to file will honor the xsl:output element [2].

Output options

transform options for terminal output.

Syntax highlighting

transform will syntax highlight the XML result if you have Pygments installed.

-n, --no-syntax

Output the transformed XML without syntax highlighting:

transform --no-syntax stylesheet.xsl file.xml

XML declaration

XML documents should begin with an XML declaration which specifies the version of XML being used [3].

-o, --omit-declaration

You can omit the XML declaration with the --omit-declaration option.

transform --omit-declaration stylesheet.xsl file.xml

Footnotes