Special Character Encoding in XML

Ran into a problem returning an XML data with special characters encoded into it through an AJAX call. The problem was that special characters (i.e. ñ -> ñ) throws an error when being returned through XML and is in the process of being parsed by the browser.

The catch?
XML does not support named entities by default, you have to declare it before hand. Only < > & " and ' are predefined.

Something like:

1
2
3
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#160;"> ]>
<xsl:stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">

Got this from here…

The fix…
Instead of using named entities in the XML (I think this also affects JSON, I could be wrong since I didn’t test this is JSON) like &ntilde;, use character references to support special character encoding.

Example:
white space = &nbsp; = &#160;
ñ = &ntilde; = &#241;

they are all the same… #160 is referenced/mapped to ” ” and #241 is referenced/mapped to ñ

So when forming your XML before returning it through an AJAX call, form the special characters via character reference.

Hope this helps you out there having this problem.

Leave a Reply

Your email address will not be published. Required fields are marked *