Diemauerdk
Diemauerdk

Reputation: 5938

how to add comments to an xml file using xslt or xpath

I have just learned xpath and xsl and i have a question.

How can i add e.g. the comment <!-- foo --> to the beginning of each xml element named foo?

Can i do it using xpath or xslt?

Upvotes: 4

Views: 3472

Answers (1)

Pablo Pozo
Pablo Pozo

Reputation: 1920

Yes, there is an XSLT element for that: <xsl:comment>.

For doing what you describe, you can use the following:

<!-- Match foo elements in the XML -->
<xsl:template match="foo">
    <xsl:comment>foo</xsl:comment>
    <foo>
        <xsl:apply-templates />
    </foo>
</xsl:template>

Upvotes: 5

Related Questions