Parameterizing ENTITY definitions for use in XHTML

It sometimes makes sense to parameterize localizable strings.  For example, in the 404 error page, I need to display a string that looks like:

 "Search {site} for {search-phrase}"

If this string were in a .properties file, it might actually look like:

  "Search %S for %P"

However, since I need to reference the string from a non-privileged XHTML page, I have to use an  ENTITY definition in a ".dtd" file.  The "%" character is not legal in an entity definition, at least not as a literal.  And, if we want to parameterize an entity, we have to roll our own parameterization scheme anyway.  For XHTML files, it turns out the simplest way to do this is to embed XHTML markup in the entity definition, for example:

  "Search <span id='site'/> for <span id='searchPhrase'/>"

It's then trivial to look these elements up using JavaScript and inject the appropriate contents at runtime.  This makes for ugly looking strings, but it's dirt simple to implement.

It occurred to me the other day, that since you can embed references to other entities inside, you could replace the SPAN markup with something like:

  "Search &; for &param.searchPhrase;"

A more complete example might look like:

  <!ENTITY httpFileNotFound.searchSiteFor
      "Search &httpFileNotFound.paramSite; for &httpFileNotFound.paramSearchPhrase;">

where the parameter entity definitions look something like:

  <!ENTITY httpFileNotFound.paramSite "<span id='site'/>">
  <!ENTITY httpFileNotFound.paramSearchPhrase "<span id='searchPhrase'/>">

This looks better to me than having the SPAN elements inlined.  It would look even better if we could dispense with the "httpFileNotFound." qualification and just say:

  <!ENTITY httpFileNotFound.searchSiteFor "Search &paramSite; for &paramSearchPhrase;">

or even:

  <!ENTITY httpFileNotFound.searchSiteFor "Search &SITE; for &SEARCH_PHRASE;">

What do you think?
5 responses
I am not sure but in this example of yours


the double "<" and ">" (I mean the angle brackets embedment) will simply make the DTD file fail...

I meant in this example (first attempt erased by comment)

< ! ENTITY httpFileNotFound.paramSite " < span / > " >


You can definitely embed literal angle brackets in an entity definition, it's just that they will be parsed as markup. I was surprised the first time I saw this too.

Or are you seeing some other syntax error that I missed?

Using IDs here for spans has only one issue : you can't use the same entity more than once in a given document. A class attribute is better.
@Daniel Glazman:

Indeed. That's just a symptom of an oversimplified example. In my actual production code, I'm actually using macro="..." rather than "id" or "class". The reason for that is a subject for another time.