我正在使用xsl脚本从xml的所有部分中删除注释。它实际上是在删除父节点中的注释,而不是其他内部节点中的注释。
编辑过的
正在更新问题。我的要求是删除整个XML文档中的所有注释。
发布于 2010-09-15 00:17:41
此转换
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="comment()"/>
</xsl:stylesheet>当应用于任何包含注释节点的XML文档时,使用,如下面的
<!-- foo -->
<a>
<!-- bar -->
<b>
<c><!-- baz --></c>
</b>
</a>会产生想要的结果(去掉注释节点的同一文档):
<a>
<b>
<c/>
</b>
</a>注意:使用最基本和最强大的XSLT设计模式--使用和覆盖标识规则。
发布于 2010-09-14 23:47:10
听起来您似乎只想对输入中的注释进行复制。您是说您的样式表复制了某些注释,而不是其他注释?当你说“父节点中的注释”时,你是指根节点的子节点(即所有元素之外)的注释吗?
当我尝试这个样式表时,它工作得很好。特别是XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<comments>
<xsl:for-each select="//comment()">
<comment><xsl:value-of select="."/></comment>
</xsl:for-each>
</comments>
</xsl:template>
</xsl:stylesheet>对输入运行
<?xml version="1.0" encoding="UTF-8"?>
<!-- foo -->
<a>
<!-- bar -->
<b>
<c><!-- baz --></c>
</b>
</a>给出输出
<?xml version="1.0" encoding="utf-8"?>
<comments>
<comment> foo </comment>
<comment> bar </comment>
<comment> baz </comment>
</comments>如果这不是您想要的行为,或者如果您的行为仍然不能处理您的输入,您是否可以发布整个样式表和输入XML的示例,并显示当前的输出是什么?另外,您使用的是什么XSLT处理器?
https://stackoverflow.com/questions/3710220
复制相似问题