我不知道有什么方法可以让xmlstarlet 直接执行此操作,但据我所知,您可以调用 XSLT 转换:
<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:copy>
</xsl:template>
<xsl:template match="@bad"/>
</xsl:stylesheet>
使用类似于(取自here的信息)的命令应用样式表:
xml tr stylesheet.xsl input.xml
生成的 XML 文件:
<root>
<a href="."/>
<a/>
</root>
来自所有元素(或给定类型的所有元素)
如果您只想从某些元素中删除bad 属性而不是全部,请使用
<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:copy>
</xsl:template>
<xsl:template match="element/@bad"/>
</xsl:stylesheet>
其中“元素”是不应再带有bad 属性的元素的名称。