06 September 2016

// // 1 comment

How To Fix XML Parsing Errors In Blogger and Wordpress?

fix XML errors in blogger editorMost often when you insert a Facebook JavaScript or AdSense JavaScript in your blogger template editor, you often come across XML Parsing errors that prompts and says "The reference to entity "version" must end with the ';' delimiter." Blogger blogs are coded in XHTML and  XML is quite strict in following correct syntax formatting than HTML. XML is surely unforgiving in this case. Blogger interprets all your document as XML rather than as HTML. XML is PCDATA (Parsed Character Data) by default which means that XML parsers will normally parse all the text in a document. As a result when we insert JavaScript inside blogspot templates, all script inside the JavaScript tags is treated as a text and due to the presence of some illegal special characters like "<" , ">" and "&" , you often face the following error:


Error parsing XML, line 1458, column 64: The reference to entity "version" must end with the ';' delimiter.


error parsing xml in blogger

What are the special characters in XML that causes error?

XML is a human readable form of textual data. It parses all the text in a document and gives errors for the following 5 special characters:
  1. (<)  - less than
  2. (&) - ampersand
  3. (>) - greater than
  4. (")  - double-quote
  5. (')  - apostrophe or single-quote
The following three are more crucial:
  • "<" will generate an error because the parser interprets it as the start of a new element.
  • ">" will generate an error because the parser interprets it as the end of a start-tag or an end-tag
  • "&" will generate an error because the parser interprets it as the start of a character entity.

Two ways to fix XML errors in Blogger

XML errors can be avoided by using two methods:
  1. Use CDATA
  2. Escape HTML entities

1. Use CDATA
The term CDATA refers to (Unparsed) Character Data which is used about text data that should not be parsed by the XML parser. To avoid errors, all script code is enclosed inside a CDATA section. Everything inside a CDATA section including the 5 special characters is ignored by the parser.
A CDATA section starts with "<![CDATA[" and ends with "]]>":
Next time when you add your Facebook code, enclose it inside CDATA as shown below:
 
<script>      
//<![CDATA[
(function(d, s, id) {      
  var js, fjs = d.getElementsByTagName(s)[0];       
  if (d.getElementById(id)) return;       
  js = d.createElement(s); js.id = id;       
  js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.3&appId=";       
  fjs.parentNode.insertBefore(js, fjs);       
}(document, 'script', 'facebook-jssdk')); 

//]]>          
</script>


All your script code will exists inside the CDATA and blogger editor will not parse the script code and thus wont give any errors.
The reason why Facebook script gives errors in blogger is due to the presence of a special character (&) that is used before the variable "version" as highlighted in the code above. "&" will generate an error because the XML parser interprets it as the start of a character entity and thus it prompts an error that says:

The reference to entity "version" must end with the ';' delimiter.



2. Escape HTML entities
Another simple method is to escape all 5 special characters and instead use the legal characters as alternatives to the above 5.
  • Replace < with &lt;
  • Replace  > with &gt;
  • Replace  "  with &quot;
  • Replace  '  with &#039;
  • Replace  & with &amp;
PS: Note how every alternative character set ends with a delimiter (;)
This method will help you encode your AdSense code and paste it inside your template without errors. It will also help you paste HTML code inside blogger comments!
This method is also called as HTML encoding and we have built a Special tool for this purpose to help you encode HTML characters with ease.To use the tool simply scroll down to comment box and click conversion, a box will appear paste all your code there and click convert. Your code will be converted.

Need Help?

I hope now you will be able to add social media plugins and third party scripts inside your blogger templates without any errors. Do let me know if you need any further assistance or clarification on any point. Would love to give a hand buddies! =)

Share:

1 comment: