6

I want to use the em dash in a value attribute for an h:link component.

Here is my attempt (currently not working):

<h:link value="#{somethingHere} &mdash; #{anotherHere}">
    <f:param name="identifier" value="#{somethingHere.identifier}" />
</h:link>

This results in a FaceletsException:

FaceletException: Error Parsing /index.xhtml: Error Traced[line: 13]
                The entity "mdash" was referenced, but not declared.
at com.sun.faces.facelets.compiler.SAXCompiler.doCompile(SAXCompiler.java:394)
...

I know I can use an HTML anchor instead, but is there a way to do it inside an Expression Language (EL) expression? What is the correct way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107

1 Answers1

11

Facelets is XML based and processed by a XML parser. The &mdash; is a HTML entity and not recognized in XML. Only the five listed in this Wikipedia page, &quot;, &amp;, &apos;, &lt; and &gt;, are recognized in XML.

Facelets/XML uses by default already UTF-8, and HTML entities are basically a leftover of pre-UTF-8 era and not necessary in UTF-8 documents, so you could just put the actual character plain/unencoded in the template (provided that the editor is able to save the file as UTF-8).

In other words, simply adjust

<h:link value="#{somethingHere} &mdash; #{anotherHere}">

to

<h:link value="#{somethingHere} — #{anotherHere}">

If this isn't an option for some reason, then you could instead use a numeric character reference in the format &#nnnn;, like as one would use &#160; to represent a &nbsp; in XML. You can find the numeric character reference in fileformat.info: Unicode Character 'EM DASH' (U+2014)

Encodings

HTML Entity (decimal) &#8212;

So, this should do for you:

<h:link value="#{somethingHere} &#8212; #{anotherHere}">

An alternative, which should satisfy the exact error message more, is to declare the entity reference explicitly yourself in the doctype.

<!DOCTYPE html [
    <!ENTITY mdash "&#8212;"> 
]>

But this isn't the general recommendation/approach as you'd need to repeat this over every single XML file wherein the character is been used.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Could you expand about why isn't recommended the entity declaration in the doctype? – Elias Dorneles Aug 16 '12 at 17:59
  • 3
    It would explicitly need to be placed in every single XML template using this character, you can't just put it in master template or so. I'd personally just put the `—` character plain/unencoded in the template. If it fails for you (Mojibake, etc), perhaps you'd need to reconfigure your editor to save files as UTF-8. Facelets uses by itself already by default UTF-8 to parse/serve XML files. – BalusC Aug 16 '12 at 18:03
  • Understood, thanks again! I'll use the numeric reference, to prevent Mojibake for other developers. – Elias Dorneles Aug 16 '12 at 18:54