Apache Ant/Passing Parameters to XSLT

From Wikibooks, open books for an open world
Jump to: navigation, search


You want to call a transform with a set of parameters. You want to be able to set these parameters from a build file.

Build File Target[edit]

<!-- sample target to demonstrate the use of passing parameters from an ant file to a XSL tranform -->
<target name="Parameterized XSLT Test">
        <echo>Running conditional XSLT test</echo>
        <xslt in="null.xml" 
           <factory name="net.sf.saxon.TransformerFactoryImpl"/>
           <param name="Parameter1" expression="true"/>
           <param name="Parameter2" expression="Hello World"/>
           <param name="Parameter3" expression="1234567"/>
           <fileset dir="tmp" file="param-output.xml"/>

Input null.xml[edit]

XSLT must have an input, but this example does not use it.



<?xml version="1.0" encoding="UTF-8"?>
    <xsl:param name="Parameter1" select="true()" as="xs:boolean"/>
    <xsl:param name="Parameter2" required="yes" as="xs:string"/>
    <xsl:param name="Parameter3" required="yes" as="xs:integer"/>
    <xsl:output method="xhtml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
                <title>Test of Passing Three Parameters (boolean, string, integer)</title>
                <h1>Test of Passing Three Parameters (boolean, string, integer)</h1>
                <p>The following parameters have been set by the Apache Ant build file.</p>
                    <li><b>Parameter1: </b><xsl:value-of select="$Parameter1"/>
                    <li><b>Parameter2: </b><xsl:value-of select="$Parameter2"/>
                    <li><b>Parameter3: </b><xsl:value-of select="$Parameter3"/>