将 XSLT 应用于 XML 文件时是否可以将元素保留在注释中?我想要直到变形:
<div>
<!-- <rs type=”bible”>Mos 1</rs> --><note>Hello World</note>
</div>
转换后的样本结果为:
<newDiv>
<!-- <rs type=”bible”>Mos 1</rs> --><newNote>Hallo World</newNote>
</newDiv>
注释包含字符串,而不是元素。您可以通过复制来保留评论。例如,这个样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<newDiv>
<xsl:apply-templates select="@*|node()"/>
</newDiv>
</xsl:template>
<xsl:template match="note">
<newNote>
<xsl:apply-templates select="@*|node()"/>
</newNote>
</xsl:template>
</xsl:stylesheet>
应该会产生你想要的结果。