小智 23
你可以使用整洁
$ tidy -quiet -asxml -xml -indent -wrap 1024 --hide-comments 1 tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<user username="qwerty" password="ytrewq" roles="manager-gui" />
</tomcat-users>
Run Code Online (Sandbox Code Playgroud)
Mad*_*sen 11
通过身份转换 XSLT 运行XML ,并使用空模板进行注释.
除注释外,所有XML内容都将传递到输出.
为了扼要地格式化输出,设置输出@ indent ="yes":
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!--Match on Attributes, Elements, text nodes, and Processing Instructions-->
<xsl:template match="@*| * | text() | processing-instruction()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--Empty template prevents comments from being copied into the output -->
<xsl:template match="comment()"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
您可能想要查看该xmllint工具.它有几个选项(其中一个--format将打印漂亮),但我无法弄清楚如何使用此工具删除注释.
另外,请查看XMLStarlet,这是一组命令行工具,可以使用xml执行任何操作.然后做:
xml c14n --without-comments # XML file canonicalization w/o comments
Run Code Online (Sandbox Code Playgroud)
编辑:OP最终使用了这一行:
xmlstarlet c14n --without-comments old.xml > new.xml
Run Code Online (Sandbox Code Playgroud)