使用相同的输入和输出文件使用 ant xslt 任务

Vei*_*koW 2 ant xslt

似乎不可能运行 ant xslt 任务来对 xml 进行排序(例如)使用输入文件作为输出文件(不带任何临时文件)。

我使用以下目标对所有 arxml 文件的内容进行排序。

<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>
Run Code Online (Sandbox Code Playgroud)

但我不能这样做,因为它返回异常

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)
Run Code Online (Sandbox Code Playgroud)

如果我使用不同的扩展名,它可以工作,但我必须将文件复制回原始文件。

有人解决这个问题了吗?如果,如何?是不是变压器的问题?

系统信息:

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
Run Code Online (Sandbox Code Playgroud)

kjh*_*hes 5

XSLT 本身无法写入其输入文件,因此 ant XSLT 任务无法帮助您实现目标。

顺便说一下,创建临时文件或目录并没有什么问题,而且将 XSLT 输入与输出分开也有好处。ant XSLT 任务可以检测何时需要重新运行转换,并且当输入和输出文件不同时可以方便地进行调试。你最好不要试图违背常规。

如果您无法设计满足输出文件与输入文件相同的需求,只需写入单独的输出目录,并在转换后将输入目录替换为输出目录:

<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>
Run Code Online (Sandbox Code Playgroud)