0

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
  • Character maps were introduced in XSLT 2.0. The default Transformer in most Java JREs/JDKs is based on Xalan, which only supports XSLT 1.0. It is however easy in Java to put a release of Saxon 9 or 10 or (by now) 11 github.com/Saxonica/Saxon-HE/tree/main/11/Java or 12 github.com/Saxonica/Saxon-HE/tree/main/12/Java on the classpath to have XSLT 2 or 3 support Commented May 15, 2023 at 10:51
  • 1
    importing saxon jar to classpath resolved this error. Commented May 15, 2023 at 16:56

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.