XSLT Transformations in XProc
XSLT Transformations with p:xslt
XProc already offers a variety of steps that can be used to perform simple XML manipulations, such as renaming elements, adding attributes or replacing text nodes. However, if the transformations become too complicated or too numerous, you should consider using XSLT. Extensible Stylesheet Language Transformations is a functional programming language that is primarily used to transform XML documents into other XML documents but can handle plain text and JSON files as well. The XProc 3.0 spec leaves it up to the XProc processor which XSLT version is supported. MorganaXProc supports XSLT 3.0 with the Saxon XSLT processor.
Note on MorganaXProc
For licensing reasons, MorganaXProc does not include an XSLT processor, so you must install it yourself. For example, if you want to install the Saxon XSLT processor, you must download Saxon for Java and copy the JAR file to the MorganaXProc-IIIse_lib
directory in the MorganaXProc program directory.
A Simple Example
First, we simply create a minimal pipeline that contains a p:xslt
step. The stylesheet is declared inline, but like other XProc steps, you can also reference external documents. The XSLT includes only a xsl:mode
instruction which works like an „identity template“ and copies an element node that is not matched by other templates and does apply-templates to its attributes and children. To put it more briefly, the input is passed on as output.
<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">
<p:input port="source">
<doc/>
</p:input>
<p:output port="result"/>
<p:xslt>
<p:with-input port="stylesheet">
<p:inline>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
</xsl:stylesheet>
</p:inline>
</p:with-input>
</p:xslt>
</p:declare-step>
Output
<?xml version="1.0" encoding="UTF-8"?> <doc/>
Passing Parameters to XSLT
Parameters can be passed with the parameters
option. The option expects a map that contains one or more parameters. Please be aware that the parameters must be declared in XSLT as well.
<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">
<p:input port="source">
<doc/>
</p:input>
<p:output port="result"/>
<p:xslt parameters="map{'my-param':'hello!'}">
<p:with-input port="stylesheet">
<p:inline>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:param name="my-param"/>
<xsl:template match="/doc">
<xsl:copy>
<xsl:value-of select="$my-param"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
</p:inline>
</p:with-input>
</p:xslt>
</p:declare-step>
Output
<?xml version="1.0" encoding="UTF-8"?> <doc>hello!</doc>