This error comes up when trying to use a stylesheet to generate a HTML 4.x document and trying to output certain HTML character codes that are illegal in the HTML 4.x specification. The cause of the error is due to HTML 4.x not defining any legal characters for ASCII codes 127-159 (inclusive) - as documented in this character table.
From the SO question I linked above, the solution is fairly simple...define a character-map for all of the illegal characters and map them to a space character for example...
XSLT
<xsl:character-map name="no-control-characters">
<xsl:output-character character="" string=" "/>
<xsl:output-character character="€" string=" "/>
...
<xsl:output-character character="Ÿ" string=" "/>
</xsl:character-map>
Then to apply the map, add a use-character-maps attribute to the output element, like so...
XSLT
<xsl:output ... use-character-maps="no-control-characters"/>
The above works well if you're OK with mapping all illegal characters to a space, but what if you want to map each one to something more meaningful like a UTF-8 character code?
That is also easy to do but you have to consider the fact that XSLT by default will perform output escaping on all text nodes. This can be disabled by setting disable-output-escaping to "yes" but I would not advise doing so as that would disable this escaping on all codes, not just the ones you're trying to map.
So if we want to map the HTML code € to € via the character-map, we can't do this...
Incorrect XSLT
<xsl:character-map name="no-control-characters">
...
<xsl:output-character character="€" string="€"/>
...
</xsl:character-map>
The reason for that is because XSLT will interpret that as map any string "€" to "€", i.e. it will use the escaped literal for the mapped character instead of the code specified in the stylesheet.
The workaround is to escape the escape code i.e. the & needs to be changed to & like so...
Incorrect XSLT
<xsl:character-map name="no-control-characters">
...
<xsl:output-character character="€" string="&#8364;"/>
...
</xsl:character-map>
The same approach can now be applied to the rest of the illegal HTML codes. In my case, we were able to map 21 out of the 32 characters to something meaningful and the rest were mapped to a question mark character. A good outcome overall.
-i