回答
该转化:
<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的设计模式的使用 - 在身份规则的使用和压倒一切的。
+1,用于确定OP的真正含义。 :-) – LarsH 2010-09-14 17:00:24
+1好猜!同样只有'* | @ *'模式:复制元素和属性以及其他节点类型的内置规则。但是我们喜欢这样匹配身份规则......;) – 2010-09-14 18:00:53
那么,是否真的需要name =“identity”? – 2014-09-26 15:37:00
这听起来像你想要复制只从输入XML的评论。你是说你的样式表复制了某些评论,但不是其他人?当你说“父节点中的注释”时,你的意思是根节点的子节点(即所有元素之外)的子节点?
当我尝试这种样式表工作正常。具体的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处理器?
你可能想编辑你的问题。我提供了一个与您的意见中的要求和说明相匹配的解决方案。 – 2010-09-14 16:19:07