使用XSLT取消注释XML内容

viv*_*075 4 xslt

我有XML和XSLT的问题.我有一个带有一些注释的XML文件,我想取消注释.

例如:

<my-app>
 <name>
 </name>
  <!-- <class>
         <line></line>
   </class>-->
</my-app>
Run Code Online (Sandbox Code Playgroud)

我想取消注释这个注释标签.

Tom*_*lak 11

<!-- the identity template copies everything 
     (unless more specific templates apply) -->
<xsl:template match="node() | @*">
  <xsl:copy>
    <xsl:apply-templates select="node() | @*" />
  </xsl:copy>
</xsl:template>

<!-- this template matches comments and uncomments them -->
<xsl:template match="comment()">
  <xsl:value-of select="." disable-output-escaping="yes" />
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

请注意,这disable-output-escaping="yes"意味着评论内容应该是格式良好的.

  • 将`match ="comment()"改为`match ="comment()[contains(.,'</')]"`.这使得它仅适用于看起来有点像HTML的注释.不完美,但可能足以满足此应用. (2认同)