LuvKS
LuvKS

Reputation: 53

how to convert unicode to iso using an xml data source in xslt?

Need to convert all unicode(~) of an XML to iso(˜) from an XML(entities.xml) where all these values are stroed....

entities.xml:-

<entities>
<entity iso="eacute" unicode="x00e9"/>
<entity iso="iacute" unicode="x00ed"/>
</entities>

Input:-

<?xml version="1.0" encoding="UTF-8"?>
<chapter>
<title>Chapt&#x00e9;r Tilt&#x00e9;</title>
<body>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
</body>    
</chapter>

Output should be :-

<chapter>
<title>Chapt&eacute;r Tilt&eacute;</title>
<body>
    <p>Th&iacute;s is t&eacute;xt...</p>
    <p>Th&iacute;s is t&eacute;xt...</p>
    <p>Th&iacute;s is t&eacute;xt...</p>
    <p>Th&iacute;s is t&eacute;xt...</p>
    <p>Th&iacute;s is t&eacute;xt...</p>
</body>    

There are many other values are also in entities.xml like....

    <entity iso="nbsp" unicode="x00a0"/>
<entity iso="ordf" unicode="x00aa"/>
<entity iso="ordm" unicode="x00ba"/>
<entity iso="para" unicode="x00b6"/>
<entity iso="plusmn" unicode="x00b1"/>
<entity iso="pound" unicode="x00a3"/>
<entity iso="raquo" unicode="x00bb"/>
<entity iso="reg" unicode="x00ae"/>
<entity iso="sect" unicode="x00a7"/>

Upvotes: 2

Views: 728

Answers (1)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

You can achieve that using xslt-2.0 using character-map.

Try the following stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

    <xsl:character-map name="CharMap">
        <xsl:output-character character="&#x00ed;" string="&amp;iacute;"/>
        <xsl:output-character character="&#x00e9;" string="&amp;eacute;"/>
    </xsl:character-map>

    <xsl:output indent="yes" use-character-maps="CharMap"/>

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

</xsl:stylesheet>

Upvotes: 2

Related Questions