Search code examples
xmlspringthymeleaf

Dynamic xsi:schemaLocation in Thymeleaf template not being read (Spring)


I have a Thymeleaf template that is being used to generate an XML document in the type of String in my Java application.

I am using the thymeleaf.Context and thymeleaf.TemplateEngine objects to read in my variables through a Map interface.

I am able to read all the values just fine, except I am having trouble setting the xi:schemaLocation tag near the top of the document.. NOTE - xmlValues is the name of the context variable, which is a map that holds the key "SchemaUrl" which points to a String object.

<?xml version="1.0" encoding="UTF-8"?>
<td:PendingData xmlns:td="https:/www.secret-site.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="@{${xmlValues['SchemaUrl']}}">
    <dataElement>
    ...
    </dataElement>
</td:PendingData>

When the final XML is stored in the database, the xsi:schemaLocation value is being saved as the literal string of the Spring/Thymleaf EL expression. Does anyone know the correct syntax to dynamically populate this tag in the template?


Solution

  • Either of these worked for me in thymeleaf 3.0:

    th:xsi:schemaLocation="@{${xmlValues['SchemaUrl']}}"
    th:attr="'xsi:schemaLocation'=@{${xmlValues['SchemaUrl']}}"
    

    If you want thymeleaf to parse an expression, you have to prefix it with th: (or else use th:attr).