Search code examples
xmlxsltxslt-2.0xpath-2.0

How to transform change comment using xslt


<article>
    <articleInfo>
        <journalCode>ABC</journalCode>
        <articleNo>456</articleNo>
    </articleInfo>
    <body>
        <heading><!--This article contain 1 heading-->Heading 1</heading>
        <p>α-index</p>
        <p>This is para 2</p>
        <p>This is para 3</p>
        <p>This is para 4</p>
        <p>This is para 5</p>
    </body>
</article>

how can i change the comment text using xslt also i want to replace the a-index to α thanks in advance


Solution

  • You can use the following XSLT:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="@* | *">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="text()[contains(.,'α-index')]">
        <xsl:value-of select="replace(., 'α-index', 'index 1')"/>
    </xsl:template>
    
    <xsl:template match="comment()">
        <xsl:comment>This heading was the only 1 heading in article</xsl:comment>
    </xsl:template>
    </xsl:stylesheet>
    

    And you can change the second template(matching text()) as per your requirement.