Handle special character in XSLT

The combination of xml and XSLT is great. I personally like to use it in all my projects . Its working great . Use of xslt helps to reduce development and time and super in maintainability .

In general screnario, we have a xml document which has some node and value . We write a xslt to transform it into some other format (xml,html ). This works great till the time it does not meet the real devil..

 I  have written a simple xslt for it

<?xml version=”1.0″ encoding=”UTF-8″?>

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform“>
<xsl:output method=”html”/>
<xsl:template match=”/”>
  <html>
  <body>
    <h2>List</h2>
    <table border=”1″>
      <tr bgcolor=”Blue”>
        <th>Company Name</th>
      </tr>
      <xsl:for-each select=”catalog/Rec”>
        <tr>
         <xsl:value-of select=”CompanyName”/>
        </tr>
      xsl:for-each>
    </table>
  </body>
  </html>
xsl:template>

xsl:stylesheet>

and I have a simple below mentioned xml

<?xml version=”1.0″ encoding=”utf-8″?>

<catalog>
 <Rec>
  <CompanyName>A B</CompanyName>
 </Rec>
 <Rec>
  <CompanyName>C & D</CompanyName>
 </Rec>
</catalog>

Now if we run this transformation by addingxml-stylesheet type=”text/xsl” href=”test.xslt”?>

This will crash my xslt .

Fix:

Replace CompanyName  tas as   < ! [ CDATA[ C &D ]]>

Thats it !!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s