I have some XMLs that contain character hex codes, e.g. it like this:
<char hex="AB"/>
Now I want to XSLT transform to display in the browser:
<xsl:value-of select="concat('&#x', /char/@hex, ';', '')"/>
However, the output in the browser is &#xAB;
and not as I expected «
so the browser does not display the character for that code (which would be «
) but only the literal string «
How can I get the XSL not to escape the ampersand?
I think your problem is larger than just the escaping, but how about the
disable-output-escaping
attribute?This can be accomplished if you set the
output method
totext
.This transformation:
When applied on the provided XML document:
produces the wanted result:
Of course, with the
text
output method one needs to produce the individual characters of starting and ending tags (<xsl:copy>
,<xsl:copy-of>
,<xsl:element>
and literal result elements do not produce any tags in this output method), but with some patience, everything is possible.One could also use DOE (Disable Output Escaping), but this "feature" is not mandatory in the XSLT Spec. and some XSLT processors (including, I think, the one used by FF) don't implement DOE.
Probably the best solution (not using
method="text"
) is the following:When this transformation is applied on the provided XML document:
the wanted result is produced:
This assumes that the values of the
hex
attribute are hexadecimals in the range x80 to xFF. If it is necessary to have values in a wider range, such as x00 to XFF, morecode
elements need to be accordingly added to themy:hex
element