XSLT

The name style is a deprecated name for the same task.

Description

Process a set of documents via XSLT.

This is useful for building views of XML based documentation, or for generating code.

Note: If you are using JDK 1.4 or higher, this task does not require external libraries not supplied in the Apache Ant distribution. However, often the built in XSL engine is not as up to date as a fresh download, so an update is still highly recommended in particular since the built-in XSLT processors of Java 5 (and to a certain extent Java 6) are known to have serious issues. See Library Dependencies for more information.

It is possible to refine the set of files that are being processed. This can be done with the includes, includesfile, excludes, excludesfile and defaultexcludes attributes. With the includes or includesfile attribute you specify the files you want to have included by using patterns. The exclude or excludesfile attribute is used to specify the files you want to have excluded. This is also done with patterns. And finally with the defaultexcludes attribute, you can specify whether you want to use default exclusions or not. See the section on directory based tasks, on how the inclusion/exclusion of files works, and how to write patterns.

This task forms an implicit FileSet and supports all attributes of <fileset> (dir becomes basedir) as well as the nested <include>, <exclude> and <patternset> elements.

Note: Unlike other similar tasks, this task treats directories that have been matched by the include/exclude patterns of the implicit fileset in a special way. It will apply the stylesheets to all files contain in them as well. Since the default include pattern is ** this means it will apply the stylesheet to all files. If you specify an excludes pattern, it may still work on the files matched by those patterns because the parent directory has been matched. If this behavior is not what you want, set the scanincludedirectories attribute to false.

Starting with Ant 1.7 this task supports nested resource collections in addition to (or instead of, depending on the useImplicitFileset attribute) the implicit fileset formed by this task.

This task supports the use of a nested <param> element which is used to pass values to an <xsl:param> declaration.

This task supports the use of a nested xmlcatalog element which is used to perform Entity and URI resolution.

Note on XSLT extension functions: when using the default TraX implementation of the Java class library and a SecurityManager is active - e.g. when running from within an IDE - XSLT extension functions cannot be used as "secure processing" is active in Java 7 and above. Ant contains a special "hack" that allows XSLT extensions to be used in Java 7 and 8, but this hack fails for Java 9. If you want to use extensions like the redirect extension that are provided by the Java class library itself, you can allow them by enabling the feature http://www.oracle.com/xml/jaxp/properties/enableExtensionFunctions in Java 9 and above. If you need to use an extension function not provided by Java itself you can set the attribute jdk.xml.transform.extensionClassLoader to a classloader (reference to an Ant PATH-like structure).

Parameters

Attribute Description Required
basedir where to find the source XML file, default is the project's basedir. No
destdir directory in which to store the results. Yes, unless in and out have been specified.
extension desired file extension to be used for the targets. If not specified, the default is ".html". Will be ignored if a nested <mapper> or both in and out have been specified. No
style name of the stylesheet to use - given either relative to the project's basedir or as an absolute path.

Alternatively, a nested element which ant can interpret as a resource can be used to indicate where to find the stylesheet
deprecated variation :
If the stylesheet cannot be found, and if you have specified the attribute basedir for the task, ant will assume that the style attribute is relative to the basedir of the task.
No, if the location of the stylesheet is specified using a nested <style> element
classpath the classpath to use when looking up the XSLT processor. No
classpathref the classpath to use, given as reference to a path defined elsewhere. No
force Recreate target files, even if they are newer than their corresponding source files or the stylesheet. No; default is false
processor name of the XSLT processor to use. Permissible value is :
  • "trax" for a TraX compliant processor (ie JAXP interface implementation such as Xalan 2 or Saxon)
Defaults to trax.
Support for xalan1 has been removed in ant 1.7.
No
includes comma- or space-separated list of patterns of files that must be included. All files are included when omitted. No
includesfile the name of a file. Each line of this file is taken to be an include pattern No
excludes comma- or space-separated list of patterns of files that must be excluded. No files (except default excludes) are excluded when omitted. No
excludesfile the name of a file. Each line of this file is taken to be an exclude pattern No
defaultexcludes indicates whether default excludes should be used or not ("yes"/"no"). Default excludes are used when omitted. No
in specifies a single XML document to be styled. Should be used with the out attribute. No
out specifies the output name for the styled result from the in attribute. No
scanincludeddirectories If any directories are matched by the includes/excludes patterns, try to transform all files in these directories. Default is true No
reloadstylesheet Control whether the stylesheet transformer is created anew for every transform operation. If you set this to true, performance may suffer, but you may work around a bug in certain Xalan-J versions. Default is false. Since Ant 1.5.2. No
useImplicitFileset Whether the implicit fileset formed by this task shall be used. If you set this to false you must use nested resource collections - or the in attribute, in which case this attribute has no impact anyway. Default is true. Since Ant 1.7. No
filenameparameter Specifies a xsl parameter for accessing the name of the current processed file. If not set, the file name is not passed to the transformation. Since Ant 1.7. No
filedirparameter Specifies a xsl parameter for accessing the directory of the current processed file. For files in the current directory a value of '.' will be passed to the transformation. If not set, the directory is not passed to the transformation. Since Ant 1.7. No
suppressWarnings Whether processor warnings shall be suppressed. This option requires support by the processor, it is supported by the trax processor bundled with Ant. Since Ant 1.8.0. No, default is false.
failOnError Whether the build should fail if any error occurs. Note that transformation errors can still be suppressed by setting failOnTransformationError to false even if this attribute is true. Since Ant 1.8.0. No, default is true.
failOnTransformationError Whether the build should fail if an error occurs while transforming the document. Note that this attribute has no effect of failOnError is false. Since Ant 1.8.0. No, default is true.
failOnNoResources Whether the build should fail if the nested resource collection is empty. Note that this attribute has no effect of failOnError is false. Since Ant 1.8.0. No, default is true.

Parameters specified as nested elements

any resource collection

since Ant 1.7

Use resource collections to specify resources that the stylesheet should be applied to. Use a nested mapper and the task's destdir attribute to specify the output files.

classpath

The classpath to load the processor from can be specified via a nested <classpath>, as well - that is, a path-like structure.

xmlcatalog

The xmlcatalog element is used to perform Entity and URI resolution.

param

Param is used to pass a parameter to the XSL stylesheet.

Parameters

Attribute Description Required
name Name of the XSL parameter Yes
expression The value to be placed into the param or an XPath expression (depending on type). Yes
type Data type of the parameter. Possible values are:
  • STRING
  • BOOLEAN
  • INT
  • LONG
  • DOUBLE
  • XPATH_STRING
  • XPATH_BOOLEAN
  • XPATH_NUMBER
  • XPATH_NODE
  • XPATH_NODESET
since Ant 1.9.3
No; default is STRING
if The param will only be passed if this property is set. No
unless The param will not be passed if this property is set. No

The XPATH_* types says that the expression is not just a primitive-type value but an XPath expression. This expression will be evaluated on an empty XML document and the result will be passed to the XSLT transformer as a parameter of given type. In these expressions the declared Ant properties can be used as XPath variables e.g. $someProperty. So you can compute something using standard XPath functions and operators.

If you write ${someProperty} instead of $someProperty, the value will be simply substituted by Ant before evaluating the XPath expression (this substitution works also for primitive types).

outputproperty ('trax' processors only)

Used to specify how you wish the result tree to be output as specified in the XSLT specifications.

Parameters

Attribute Description Required
name Name of the property Yes
value value of the property. Yes

factory ('trax' processors only)

Used to specify factory settings.

Parameters

Attribute Description Required
name fully qualified classname of the transformer factory to use. For example org.apache.xalan.processor.TransformerFactoryImpl or org.apache.xalan.xsltc.trax.TransformerFactoryImpl or net.sf.saxon.TransformerFactoryImpl... No. Defaults to the JAXP lookup mechanism.

Parameters specified as nested elements

attribute

Used to specify settings of the processor factory. The attribute names and values are entirely processor specific so you must be aware of the implementation to figure them out. Read the documentation of your processor. For example, in Xalan 2.x:

  • http://xml.apache.org/xalan/features/optimize (boolean)
  • http://xml.apache.org/xalan/features/incremental (boolean)
  • ...
And in Saxon 7.x:
  • http://saxon.sf.net/feature/allow-external-functions (boolean)
  • http://saxon.sf.net/feature/timing (boolean)
  • http://saxon.sf.net/feature/traceListener (string)
  • http://saxon.sf.net/feature/treeModel (integer)
  • http://saxon.sf.net/feature/linenumbering (integer)
  • ...

Parameters

Attribute Description Required
name Name of the attribute Yes
value value of the attribute. Exactly one of these
valueref value of the attribute is the value of the project reference with the given id. since Ant 1.9.8
classloaderforpath value of the attribute is a classloader that uses the classpath specified by a path that is the project reference with the given id. since Ant 1.9.8

Examples

  <path id="extension-path">
    ...
  </path>


  <xslt ...>
    <factory>
      <attribute name="jdk.xml.transform.extensionClassLoader"
                 classloaderforpath="extension-path"/>
    </factory>
  </xslt ...>

Sets the classloader to use when loading extension functions to a classloader using the path with the id extension-path.

feature

since Ant 1.9.8

Used to specify settings of the processor factory. The feature names are mostly processor specific so you must be aware of the implementation to figure them out. Read the documentation of your processor. The only feature all implementations are required to support is http://javax.xml.XMLConstants/feature/secure-processing.

Parameters

Attribute Description Required
name Name of the feature Yes
value value of the feature. A boolean value (i.e. permitted values are true,false,yes,no,on,off). No, defaults to false

mapper

since Ant 1.6.2

You can define filename transformations by using a nested mapper element. The default mapper used by <xslt> removes the file extension from the source file and adds the extension specified via the extension attribute.

style

Since Ant 1.7

The nested style element can be used to specify your stylesheet in terms of Ant's resource types. With this element, the stylesheet should be specified as a nested resource or single-element collection. Alternatively, use the refid to specify the resource or collection as a reference.

sysproperty

Use nested <sysproperty> elements to specify system properties required by the factory or transformation. These properties will be made available to the VM during the execution of the class. The attributes for this element are the same as for environment variables.

since Ant 1.8.0.

syspropertyset

You can specify a set of properties to be used as system properties with syspropertysets.

since Ant 1.8.0.

Examples

<xslt basedir="doc" destdir="build/doc"
       extension=".html" style="style/apache.xsl"/>

Using an xmlcatalog

<xslt basedir="doc" destdir="build/doc"
      extension=".html" style="style/apache.xsl">
  <xmlcatalog refid="mycatalog"/>
</xslt>

<xslt basedir="doc" destdir="build/doc"
   extension=".html" style="style/apache.xsl">
   <xmlcatalog>
       <dtd
         publicId="-//ArielPartners//DTD XML Article V1.0//EN"
         location="com/arielpartners/knowledgebase/dtd/article.dtd"/>
   </xmlcatalog>
</xslt>

Using XSL parameters

Simple String parameter:

<xslt basedir="doc" destdir="build/doc"
      extension=".html" style="style/apache.xsl">
  <param name="date" expression="07-01-2000"/>
</xslt>

Then if you declare a global parameter "date" with the top-level element <xsl:param name="date"/>, the variable $date will subsequently have the value 07-01-2000.

Various data types and XPath expressions:

<property name="antProperty1" value="ANT_PROPERTY_1"/>
<property name="antProperty2" value="ANT_PROPERTY_2"/>
<property name="antProperty3" value="3"/>
<property name="antProperty4" value="substring-before"/>

<!--
  ${this} is substituted by Ant itself
  and $this is evaluated by XPath as a variable
-->

<xslt in="in.xml" out="out.xml" style="template.xsl">
  
  <!-- Simple String parameter: -->
  <param name="p0" expression="some nice string" type="STRING"/>
  
  <!-- A value substituted by Ant -->
  <param name="p1" expression="some string with ${antProperty1} constructed by Ant" type="STRING"/>
  
  <!-- XPath resulting in: and this is done in XPath: ANT_PROPERTY_2 -->
  <param name="p2" expression="concat('and this is done in XPath: ', $antProperty2)" type="XPATH_STRING"/>
  
  <!-- Some XPath math, result: 42 -->
  <param name="p3" expression="64 * 64 div 128 + 10" type="XPATH_NUMBER"/>
  
  <!-- Some numeric parameter: -->
  <param name="p4" expression="123.45" type="DOUBLE"/>
  
  <!-- XPath expression, result: true boolean -->
  <param name="p5" expression="$antProperty1 = 'ANT_PROPERTY_1'" type="XPATH_BOOLEAN"/>
  
  <!-- First one is an XPath variable, second one is a text substituted by Ant, result: true boolean -->
  <param name="p6" expression="$antProperty2 = '${antProperty2}'" type="XPATH_BOOLEAN"/>
  
  <!-- Some XPath math with a variable, result: 64 -->
  <param name="p7" expression="$antProperty3 * 4 * 5 + 4" type="XPATH_NUMBER"/>
  
  <!-- 
    XPath expression with substituted function name and a variable:
    substring-before($antProperty2, '_')
    result: ANT
  -->
  <param name="p8" expression="${antProperty4}($antProperty2, '_')" type="XPATH_STRING"/>
  
  <!-- Without type attribute: -->
  <param name="p9" expression="default type is String"/>
</xslt>

Using output properties

<xslt in="doc.xml" out="build/doc/output.xml"
      style="style/apache.xsl">
  <outputproperty name="method" value="xml"/>
  <outputproperty name="standalone" value="yes"/>
  <outputproperty name="encoding" value="iso8859_1"/>
  <outputproperty name="indent" value="yes"/>
</xslt>

Using factory settings

<xslt in="doc.xml" out="build/doc/output.xml"
      style="style/apache.xsl">
  <factory name="org.apache.xalan.processor.TransformerFactoryImpl">
    <attribute name="http://xml.apache.org/xalan/features/optimize" value="true"/>
  </factory>
</xslt>

Using a mapper

<xslt basedir="in" destdir="out"
      style="style/apache.xsl">
  <mapper type="glob" from="*.xml.en" to="*.html.en"/>
</xslt>

Using a nested resource to define the stylesheet

<xslt in="data.xml" out="${out.dir}/out.xml">
    <style>
        <url url="${printParams.xsl.url}"/>
    </style>
    <param name="set" expression="value"/>
</xslt>

Print the current processed file name

<project>
  <xslt style="printFilename.xsl" destdir="out" basedir="in" extension=".txt"
        filenameparameter="filename"
        filedirparameter="filedir"
  />
</project>

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:param name="filename"></xsl:param>
    <xsl:param name="filedir">.</xsl:param>

<xsl:template match="/">
  Current file is <xsl:value-of select="$filename"/> in directory <xsl:value-of select="$filedir"/>.
</xsl:template>

</xsl:stylesheet>

Use an XInclude-aware version of Xerces while transforming

<xslt ...>
    <sysproperty key="org.apache.xerces.xni.parser.XMLParserConfiguration"
                 value="org.apache.xerces.parsers.XIncludeParserConfiguration"
     />
<xslt>