Parsing xsl error- javax.xml.transform.TransformerException: use-character-maps attribute is not allowed on the xsl:output element

217 views Asked by At

While parsing one of the XML files using XML Serializer in Java, the HTML entities are converted into their corresponding hex code values(like for mdash output is "hexcode value-#x2014;") and hence reflected into the final output file. In order to convert the hex code value to a normal entity, we tried an xsl transformation which throws an error as "javax.xml.transform.TransformerException: use-character-maps attribute is not allowed on the xsl:output element".

Below is the xsl used:-

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output omit-xml-declaration="yes" />
<xsl:character-map name="mdash">
<xsl:output-character character="&#x2014;" string="&amp;mdash;" />
</xsl:character-map>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

We used a transformer factory object to transform the final XML to convert hex code into normal entities via xsl file.

2

There are 2 answers

0
Michael Kay On

The error message suggests to me that you are trying to run an XSLT 2.0 stylesheet using an XSLT 1.0 processor.

Technically if the stylesheet says version="2.0", an XSLT 1.0 processor is supposed to run in "forwards compatibility mode", which is supposed to ignore any elements and attributes it doesn't understand (like use-character-maps) rather than raising an error. But I'm not sure that's always implemented correctly.

0
Rahul On

As mentioned in the comments by Martin. Importing saxon jar to classpath and using the TransformerFactory object as below fixed this error:-

TransformerFactory tfactory = new net.sf.saxon.TransformerFactoryImpl();