retain comments post XSLT transform

2019-08-23 01:25发布

问题:

This seems like an easy one, but one that I haven't come across before.

I need to retain the comments in an XML file to the output XML after applying an XSLT.

Does anyone know how/if this is possible? Thanks!

回答1:

In your XSL you can reference comments in your source XML similar to other elements, e.g.:

<xsl:template match="comment()">
    <xsl:value-of select="."/>
</xsl:template>

If it should appear in your transformed XML as comment as well you can do the following:

<xsl:template match="comment()">
    <xsl:comment>
        <xsl:value-of select="."/>
    </xsl:comment>
</xsl:template>