使用 XSLT,我如何评论单个节点而不评论其子节点?
我有这个html:
<html>
<body>
<div class="blah" style="blahblah">
<span>
<p>test</p>
</span>
</div>
</body>
</html>
我想要这个输出:
<html>
<body>
<!-- div class="blah" style="blahblah" -->
<span>
<p>test</p>
</span>
<!-- /div -->
</body>
</html>
复制子节点并复制注释节点的任何属性是关键。
以下是我最好的尝试,但不起作用。XSLT 处理器喊道:“在添加了文本、注释、pi 或子元素节点后,不能将属性和命名空间节点添加到父元素。”
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- IdentityTransform -->
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<xsl:text disable-output-escaping="yes"><!--</xsl:text>
<xsl:copy>
<xsl:text disable-output-escaping="yes">--></xsl:text>
<xsl:apply-templates select="@* | node()"/>
<xsl:text disable-output-escaping="yes"><!--</xsl:text>
</xsl:copy>
<xsl:text disable-output-escaping="yes">--></xsl:text>
</xsl:template>
</xsl:stylesheet>