Thymeleaf and inline scripts SAXParseException
Asked Answered
C

1

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

Comb answered 10/4, 2014 at 10:49 Comment(0)
C
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

Comb answered 10/4, 2014 at 13:33 Comment(4)
For more information , [Thymeleaf templating engine parsing tweaks] (anwaarlabs.wordpress.com/2017/02/19/…) can helpful.Concertmaster
It works. Took a lot of time to figure it out. Thanks.Hargett
Yes, this worked for 1.5.2! This issue has been resolved in 2.0.0.BUILD-SNAPSHOT. Hopefully they release 2.0 soon.Perseid
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.Osteoplastic

© 2022 - 2024 — McMap. All rights reserved.