6

I got a page which uses thymeleaf template, and I'm getting the following error on page load when using inline scripts:

org.xml.sax.SAXParseException; lineNumber: 270; columnNumber: 85; The content of elements must consist of well-formed character data or markup.

Code at line 270

<script type="text/javascript" >
    window.jQuery || document.write("<script src='assets/js/jquery-2.0.3.min.js'>"+"<"+"/script>");
</script>

I have tried replacing "<", ">" symbols from document.write with &lt; &gt;, the exception doesn't occur anymore but the script is not loaded anymore

Alexandru Severin
  • 6,021
  • 11
  • 48
  • 71

1 Answers1

17

You need to add CDATA tags for the script like this:

<script type="text/javascript">
    //<![CDATA[
     window.jQuery || document.write("<script src='assets/js/jquery-2.0.3.min.js'>"+"<"+"/script>");
    //]]>
</script>

The tags are telling thymeleaf's xml parser that the code between should not be interpreted as XML markup.

This is no longer necessary since Thymeleaf 3.0

Alexandru Severin
  • 6,021
  • 11
  • 48
  • 71
  • For more information , [Thymeleaf templating engine parsing tweaks] (https://anwaarlabs.wordpress.com/2017/02/19/thymeleaf-whitelabel-error-page-convert-html-to-thymeleaf-template) can helpful. – Digital Alchemist Feb 19 '17 at 23:18
  • It works. Took a lot of time to figure it out. Thanks. – Cristian Mar 09 '17 at 20:43
  • Yes, this worked for 1.5.2! This issue has been resolved in 2.0.0.BUILD-SNAPSHOT. Hopefully they release 2.0 soon. – Martin Erlic Apr 11 '17 at 11:18
  • 1
    Just as an update, Thymeleaf 3.x uses the AttoParser and CDATA isn't needed anymore, as long as you do not want to validate your markup against xml. – RiZKiT Aug 02 '18 at 09:33