1

I am using thymeleaf and Spring Boot to parse an entity.

I'm trying to change a boolean field value to String like 'yes' or 'No' but as simple as it seems, I am getting the following error:

12:54:30.046 [XNIO-2 task-17] ERROR o.t.templateparser.ErrorHandler - [THYMELEAF][XNIO-2 task-17] Fatal error during parsing org.xml.sax.SAXParseException: The entity name must go inmediately after the '&' in the entity reference.

Exception parsing document: template="scenariosList", line 401 - column 21

This is my last try in javascript:

var modificat = $("#modificatEscenari").val();
var calculat = $("#calculatEscenari").val();

if (calculat) {
    $("#calculat").val("Sí");
} else {
    $("#calculat").val("No");
    $("#calculPendent").val("Sí");
}

if (modificat) {
    $("#calculPendent").val("Sí");
}

if (!modificat && calculat) {
    $('#calculPendent').val("No");
}

Can anyone tell me what's wrong in this code if (!modificat && calculat) {... I have read the use of operators in if statements and it looks good to me.

EDIT

It's wrapped like

<script>
    $(document).ready( function () {
        ....
        doing my stuff and the piece of code that trigger the exception
</script>

Thank you.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
JM.Palomo
  • 13
  • 4

2 Answers2

2

The exception makes it clear that you're embedding JavaScript code in an XML document. So naturally, && fails, because & indicates the beginning of a numbered or named character entity (like &nbsp; or &#1234;).

Possible solutions:

  1. Don't embed JavaScript code in XML. Use a separate JavaScript file that you refer to from the XML. (This would be what I'd do if I were you.)

  2. Mark it up in a CDATA section. This question's answers talk about how to do that, for instance this one says it should be:

    <script xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
    // Your code here
    ]]></script>
    
  3. Laborious ensure that the text of the JavaScript code is valid XML text, which would be A) Painful, and B) A maintenance nightmare. But for instance, the && operator would have to be written as &amp;&amp;.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

Related: The entity name must immediately follow the '&' in the entity reference

To summarize: The & character is a special character used by the XML Parser inside Thymeleaf. Use &amp; for the & character instead.

Your line would look like this:

if(!modificat &amp;&amp; calculat){
Sand3r205
  • 26
  • 1
  • 4
  • This actually leeds to a syntax error caused by the ';' after &amp – JM.Palomo Jul 13 '17 at 11:33
  • 1
    @JM.Palomo: It shouldn't, the `;` is part of the named character entity. The XML text `if(!modificat && calculat){`, when read by the parser, is `if(!modificat && calculat){` which works just fine when it's given to the JavaScript engine. – T.J. Crowder Jul 13 '17 at 11:45