I'm trying to run an XSLT transformation from an ant file.
I'm using a XSLT 2.0 stylesheet with a saxon 9 parser (supporting XSLT 2.0).
The problem is that it seems that ant is always calling an XSLT 1.0 parser.
Here's my ant file :
<xslt style="stylesheet.xslt"
basedir="core/"
extension=".xml"
destdir="core/"
classpath="D:\\DevTools\\saxon\\bin\\saxon9.jar">
</xslt>
If I call it directly (without ant), it's working.
Any idea ?
At least in ant 1.8.0, the xslt task with a specified classpath is very slow. The problem seems to be classpath loading. I ran ant under JDB and it spent all of the extra time in org.apache.tools.ant.AntClassLoader.loadClass reading zip files.
I tried this before running ant it it went a lot faster:
The macrodef from Tom Howard works better, and although it has an odd syntax for XSLT params, at least it's possible.
Rather than waiting for this to be fixed in 1.8.2 and then waiting for everyone to eventually upgrade to 1.8.2, you can roll your own XSLT macro (for situations where you explicitly want to use Saxon, rather than a user selected XSLT engine)
you can then invoke it like (assuming xmlns:mydep="com.mycompany.mydepartment" is set on the project element)
You can find the docs for passing parameters to Saxon at http://www.saxonica.com/documentation/using-xsl/commandline.xml
This tutorial seems to give step by step instructions on how to do what you are asking:
http://www.abbeyworkshop.com/howto/xslt/ant-saxon/index.html
From that it appears you are doing the correct thing. Are you sure you need the double back slashes?
Update: The xslt Ant documentation mentions the 'factory' property which may help you get closer:
http://ant.apache.org/manual/Tasks/style.html
If you are having this problem, check that you are not using Ant 1.8.1, because there is a bug in Ant 1.8.1 that prevents this from working. (Though this is not the problem in the original post, because that was before Ant 1.8.1 was released).
Your options are:
CLASSPATH
to Ant before it starts, by either:CLASSPATH
environment variable, or-lib
command line option toant
processor="org.apache.tools.ant.taskdefs.optional.TraXLiaison"
as an attribute of thexslt
task element.I would suggestion using option 1, followed by option 4.
Option 2 will work, but it places the responsibility on the person running ant to set up their environment and run ant properly. I assume you don't want that, which is why you are trying to get the
classpath
attribute on thexslt
task to work.Option 3 has limitations, because SAXON Ant requires downloading and installing its JAR file. Also SAXON Ant does not work with SAXON 9.2 or later (and SAXON Ant has not been updated since it was created in June 2008).
In theory, specifying a
factory
subelement makes the XSLT processor that you want to use explicit -- to prevent the class loader from finding a different XSLT processor earlier in its search, and using it instead of your XSLT processor which is further down in the CLASSPATH. In practice (at least in ant 1.7.0, 1.7.1 and 1.8.0) if thefactory
subelement is specified thexslt
task ignores theclasspath
attribute -- which means you have to resort to explicitly specifying the CLASSPATH (option 2). So it doesn't help solve the original problem. However, this seems to have been fixed in the Ant source code, so could work in releases after 1.8.1.The problem is that while Saxon is added to the classpath, the default JAXP mechanism to determine which TransformerFactory is used and it will use the default that is Xalan. You either need to:
javax.xml.transform.TransformerFactory
system variable tonet.sf.saxon.TransformerFactoryImpl
,CLASSPATH
system variable, or<factory name="net.sf.saxon.TransformerFactoryImpl"/>
inside thexslt
elementEDIT: Dr. Michael Kay has pointed out that the AntTransform is no longer supported, nor recommended.
Create a taskdef from the Saxon AntTransform class:I have begun using the standard
<xslt>
task with the saxon jar specified in a<classpath>
, but had been running into performance issues. It seemed to "hang" for a bit when the task was called. I have found that addingprocessor="trax"
and specifying<factory name="net.sf.saxon.TransformerFactoryImpl"/>
helps it run much faster.