'Thymeleaf in pure xml : how to generate a self closing tag when the th:text is null?

I would like to generate XML with a self-closing tag when the content (that came from a variable 'myVar') is empty or null, by using thymeleaf.

I currently use :

<myTag th:text="${myVar}"/>

But I get the following result :

<myTag></myTag>

I expect :

<myTag/>

Is there a way to do this by using thymeleaf?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source