使用Saxon-B 9.1.0.8,您可以获得一个非常干净的解决方案:
源代码.xml
<root>
<KeepMeTag someAttribute="something">
<SomeOtherTag someAttribute="something" />
</KeepMeTag>
</root>
构建.xml
<project name="ant-xslt-comment-out-element" default="run" basedir=".">
<target name="run">
<fail unless="out.dir"/>
<xslt
style="transform.xsl"
includes="source.xml"
destdir="${out.dir}"
>
<factory name="net.sf.saxon.TransformerFactoryImpl"/>
<identitymapper/>
</xslt>
</target>
</project>
变换.xsl
<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://saxon.sf.net/"
>
<xsl:output method="xml" indent="yes" />
<!-- To serialize with saxon:serialize() -->
<xsl:output name="default" indent="yes" omit-xml-declaration="yes"/>
<!-- XSLT identity transformation -->
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="SomeOtherTag">
<xsl:comment>
<xsl:value-of select="saxon:serialize(., 'default')" />
</xsl:comment>
</xsl:template>
</xsl:stylesheet>
蚂蚁命令
ant -lib saxon9.jar
生成的 XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<KeepMeTag someAttribute="something">
<!--<SomeOtherTag someAttribute="something"/>-->
</KeepMeTag>
</root>