External entities: The entity "pound" was referenced, but not declared

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

External entities: The entity "pound" was referenced, but not declared

Jamie
Hi,

I'm trying to write out in html text representing the external entity for the GBP currency symbol, i.e. £
Assuming: ctx.setVariable("currency", "£");

and in the template:
<p th:text="${currency}">$</p>

the result is:
<p>&amp;pound;</p>

if the template has:
<p th:utext="${currency}">$</p>

the result is a stacktrace:
Caused by: org.xml.sax.SAXParseException: The entity "pound" was referenced, but not declared.

What is the correct way to write out text representing external entities like &euro; and &pound; etc?

Thanks,

jamie
Reply | Threaded
Open this post in threaded view
|

Re: External entities: The entity "pound" was referenced, but not declared

danielfernandez
Administrator
Hi,

The attribute to use is utext (because you don't want your variable to be escaped), like:

    th:utext="${currency}"

...but the problem is that "&pound;" is an entity not defined in any of the output DTDs (including the XHTML ones), so Thymeleaf is not being able to recognise it when creating the template's output.

How can you write it, then? Simple: just use its character code, 163:

    ctx.setVariable("currency", "&#163;");

I'll have a look at a way to include "natural" support for this kind of entities in Thymeleaf's core for next versions...

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: External entities: The entity "pound" was referenced, but not declared

Jamie
Daniel,

Thanks for the quick response.  Using &#163; doesn't have the desired effect either because Thymeleaf unencodes it to the currency symbol itself, i.e. the resulting html is:
<p>£</p>

its the same as doing:
ctx.setVariable("currency", "£");

What I require is the encoded html representation of the currency symbol to be rendered into the html, not the symbol itself.  In other words, I'm after:
<p>&pound;</p>

If thats something that will be possible in a future version then I'll see if I can come up with a hack to work around it until then.

Thanks,

jamie
Reply | Threaded
Open this post in threaded view
|

Re: External entities: The entity "pound" was referenced, but not declared

danielfernandez
Administrator
Ok Jamie, now I understand.

After doing some research and tests, it seems that there are two issues here:

  1st. A bug that makes Thymeleaf raise an exception when an entity reference like "£" is written in a th:utext attribute. This is because when this fragment is parsed, the entity references it needs are not declared in a valid DOCTYPE clause. This will be solved in 1.1.0.

  2nd. What you need --output exactly "£" instead of "&pound;" -- will not be trivial to achieve. The problem is that XML parsers, by definition, resolve XML entities like &pound; into their equivalents (which are declared in the corresponding DTD and that, in this case, is the "£" character). Using the standard Java XML parsing APIs, avoiding this seems quite tricky. I will probably try a preprocessing approach, but I cannot assure 100% I will be successful. If this works, it will be included in 1.1.0 or maybe 1.1.1.

And that's all I can say for now...

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: External entities: The entity "pound" was referenced, but not declared

emilime
Hi Daniel, I think there is some other problem with entities: I'm trying to display "€" symbol using
&#8364;
but it doesn't work, I always get "?" (while pound, like in the previous example, works). The same template page called directly form the browser correctly render "€" so the problem seems related to Thymeleaf processing. Can you help me?
Reply | Threaded
Open this post in threaded view
|

Re: External entities: The entity "pound" was referenced, but not declared

Zemi
Administrator
Hi,

I had a similar problem with euro symbol, as you can see at this post:
   http://forum.thymeleaf.org/Problem-with-euro-symbol-td3156574.html

It happened to be a configuration issue.

Reply | Threaded
Open this post in threaded view
|

Re: External entities: The entity "pound" was referenced, but not declared

emilime
Thank you Zemi! € works now :)
Reply | Threaded
Open this post in threaded view
|

Re: External entities: The entity "pound" was referenced, but not declared

Jamie
In reply to this post by danielfernandez
Daniel,

I just tried out 1.1.0 and the external entity fix works well.  Thanks!

jamie