Search code examples
xsltant

Use ant xslt task using same input and output files


It seems not possible to run the ant xslt task to do sorting on xml (for example) using the input file as output file w/o any temporary file.

I am using the following target to sort the content of all arxml files.

<target name="sort_arxml" depends="init" description="Do a XLST on all arxml files to sort their content">
    <tempfile property="sort.xslt" suffix=".xslt" deleteonexit="true" />
    <echo file="${sort.xslt}">
        <![CDATA[
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:template match="@*|node()">
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()">
                        <xsl:sort select="child::*"/>
                    </xsl:apply-templates>
                </xsl:copy>
            </xsl:template>
        </xsl:stylesheet>
        ]]></echo>
    <xslt style="${sort.xslt}" basedir="../Config" destdir="../Config" extension=".arxml" includes="**/*.arxml" excludes="**/AUTOSAR*"/>
</target>

But I cannot do this as it returns a an Exception

java.io.FileNotFoundException: xxxx\Config\Developer\ComponentTypes\yyyy.arxml (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
    at org.apache.tools.ant.taskdefs.optional.TraXLiaison.transform(TraXLiaison.java:185)
    at org.apache.tools.ant.taskdefs.XSLTProcess.process(XSLTProcess.java:816)

If I use a different extension it works but then I have to copy the files back over the original ones.

Does someone fixed this? and if, how? Is it a matter of the transformer?

System info:

Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Detected Java version: 1.7 in: C:\Program Files\Java\jdk1.7.0_45\jre
Detected OS: Windows 7

Solution

  • XSLT itself cannot write to its input file, so there is nothing that the ant XSLT task can do to help you with your goal.

    By the way, there is nothing wrong with creating a temporary file or directory, and there are advantages to keeping XSLT input separate from the output. The ant XSLT task can detect when it is necessary to re-run the transformation, and debugging is facilitated when input and output files are distinct. You probably would be better off not trying to go against the grain.

    If you can't design around the need for the output files to be the same as the input files, just write to a separate output directory and replace the input directory with the output directory after the transformation:

    <target name="sort_arxml" depends="init" 
            description="Do a XLST on all arxml files to sort their content">
    
        <!-- [ same as in question ] -->
    
        <xslt style="${sort.xslt}" basedir="../Config" destdir="../ConfigTMP" 
              extension=".arxml" includes="**/*.arxml" excludes="**/AUTOSAR*"/>
    
        <delete dir="../Config"/>
        <move file="../ConfigTMP" tofile="../Config"/>
    
    </target>