Home > Blockchain >  Remove ^ char from XML file in XSLT transformation
Remove ^ char from XML file in XSLT transformation

Time:04-01

I have the sample XML file which contains "^^" i need to remove it from all over places.

There is 'n' number of possibility to find "^^" in XML so i can't select based on XPATH.

Thanks in advance.

    Input

    <CATALOG>         
    <CD>         
    <TITLE>^^Empire Burlesque</TITLE>         
    <ARTIST>Bob Dylan</ARTIST>         
    <COUNTRY>USA</COUNTRY>         
    <COMPANY>Columbia^^</COMPANY>         
    <PRICE>10.90</PRICE>         
    <YEAR>1985</YEAR>         
    </CD>         
    <CD>         
    <TITLE>Hide your heart</TITLE>         
    <ARTIST>^^Bonnie Tyler</ARTIST>         
    <COUNTRY>UK</COUNTRY>         
    <COMPANY>^^CBS Records</COMPANY>         
    <PRICE>9.90</PRICE>         
    <YEAR>1988</YEAR>         
    </CD>         
    </CATALOG>

    Desire Output

    <CATALOG>         
    <CD>         
    <TITLE>Empire Burlesque</TITLE>         
    <ARTIST>Bob Dylan</ARTIST>         
    <COUNTRY>USA</COUNTRY>         
    <COMPANY>Columbia</COMPANY>         
    <PRICE>10.90</PRICE>         
    <YEAR>1985</YEAR>         
    </CD>         
    <CD>         
    <TITLE>Hide your heart</TITLE>         
    <ARTIST>Bonnie Tyler</ARTIST>         
    <COUNTRY>UK</COUNTRY>         
    <COMPANY>CBS Records</COMPANY>         
    <PRICE>9.90</PRICE>         
    <YEAR>1988</YEAR>         
    </CD>         
    </CATALOG>

I tried something like this.

   <xsl:template match="@*|*|processing-instruction()">
    <xsl:copy>
        <xsl:apply-templates select="*|@*|translate(text(),'^^',' ')|processing-instruction()"/>            
    </xsl:copy>
</xsl:template>

But it is not working

CodePudding user response:

Use the translate() function to remove all occurrences of the ^ character from all text nodes.


Added:

As explained in the comments, you should use the identity transform template alongside an overriding template matching any text node:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text()" priority="0">
    <xsl:value-of select="translate(., '^', '')"/>
</xsl:template>

</xsl:stylesheet>
  • Related