如何在XSLT中进行注释而不是HTML

53

我正在编写XSL,想在代码中添加注释,但希望在处理代码时被清除,就像PHP一样,但我不确定该怎么做。

我知道有comment对象,但它在处理时会输出HTML注释。 :\

<xsl:comment>comment</xsl:comment>
5个回答

107

您使用了标准的XML注释:

<!-- Comment -->

这些内容不会被XSLT转换器处理。


16

请确保在XML声明之后(如果您使用的话,实际上您不需要)放置<!--注释-->

错误示例:

<!-- a comment -->
<?xml version="1.0"?>

有效:

<?xml version="1.0"?>
<!-- a comment -->

在调试别人的XSLT时,我也因为这个问题纠结了一会,看起来很明显,但很容易被忽视。


4
请注意,根据您的XSLT处理器及其处理空格的设置,注释两侧的空格可能会出现在输出流中。如果这对您的输出有影响,请确保将注释括在xslt标签之间。
例如:
<xsl:for-each select="someTag">
  <xsl:text>"</xsl:text>
    <!-- output the id -->
<xsl:value-of select="@id"/>
<xsl:text>"</xsl:text>
</xsl:for-each>

输出将会是 " someTagID" (在注释标签前的缩进tab/空格会被输出)。 要删除缩进,可以将其与左边缘对齐或使用类似于

的括号。

<xsl:text>"</xsl:text><!-- output the id --><xsl:value-of select="@id"/>

1
这是创建一个在html中不会显示的注释节点的方法。
<xsl:comment>

  <!-- Content:template -->

</xsl:comment>

-1

当然。阅读http://www.w3.org/TR/xslt#built-in-rule,然后你就会明白为什么这个简单的样式表将(应该)做到你想要的:

<?xml version="1.0"?>
<xsl:stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="comment()">
  <xsl:copy/>
</xsl:template>

<xsl:template match="text()|@*"/>

</xsl:stylesheet>

尝试:

<xsl:template match="/">
  <xsl:for-each select="//comment()">
   <SRC_COMMENT>
   <xsl:value-of select="."/>
   </SRC_COMMENT>
  </xsl:for-each>
 </xsl:template>
or use a <xsl:comment ...> instruction for a more literal duplication of the source     document content in place of my <SRC_COMMENT> tag.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接