对 XML 应用 XSLT 时是否可以在 XML 中保留注释?
示例(来源):
<rootNode>
  <!-- My comment --><childElement>5</childElement>
</rootNode>
转换后的样本结果为:
<newRoot>
  <!-- My comment --><newChildElement>5</newChildElement>
</newRoot>
你会如何编写样式表?
这在您的示例中不是正确的 XML 注释语法,但您可以保留所有节点
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
然后为要转换的节点添加模板
<xsl:template match="childElement">
  <newChildElement>
    <xsl:apply-templates select="@* | node()"/>
  </newChildElement>
</xsl:template>
我的建议假设样本为
<rootNode>
  <!-- My comment--><childElement>5</childElement>
</rootNode>
结果为
<rootNode>
  <!-- My comment--><newChildElement>5</newChildElement>
</rootNode>
如果childElement是在评论中,那就更难了。