javathymeleaftemplate-engine

How to escape colon in the attribute key with Thymeleaf


I want to generate the following entry in .xml file with Thymeleaf

<postalCode xsi:type="extPL:adxp.postalCode"> </postalCode>

to do so, I have the following template:

<postalCode th:attr="xsi:type='extPL:adxp.postalCode' </postalCode>

but the phrase xsi:type contains colon which breaks the syntax of the expression. How can I escape the colon to make it work? I tried various approaches with substituting &#58; for colon or &quot; for ', but nothing worked for me.


Solution

  • You could use single quotes ' to mask the colon like so

    <postalCode th:attr="'xsi:type'='extPL:adxp.postalCode' </postalCode>
    

    or the whole attribute argument

    <postalCode th:attr="'xsi:type=extPL:adxp.postalCode'" </postalCode>