【发布时间】:2017-08-06 14:12:44
【问题描述】:
使用 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>
【问题讨论】:
标签: xslt