0

There have some userdefined entities in the input xml like &key; and ‐. We have defined these entites as DOCTYPE in the below xsl:-

<!DOCTYPE xsl:stylesheet [
<!ENTITY key "&amp;key;">
<!ENTITY hyphen "&amp;hyphen;">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">
<xsl:output method="xml" omit-xml-declaration="no" use-character-maps="mdash" />
<xsl:character-map name="mdash">
<xsl:output-character character="&#x2014;" string="&amp;mdash;"/>
<xsl:output-character character="&amp;" string="&amp;amp;" />
<xsl:output-character character="&quot;" string="&amp;quot;" />
<xsl:output-character character="&apos;" string="&amp;apos;" />
<xsl:output-character character="&sect;" string="&amp;sect;" />
<xsl:output-character character="&key;" string="&amp;key;" />
<xsl:output-character character="&hyphen;" string="&amp;hyphen;" />
</xsl:character-map>
<xsl:mode on-no-match="shallow-copy"/>
</xsl:stylesheet>

Now in output as well, the entites should remain same, i.e. &key; and &hyphen;. But while using the userdefined entites defined under DOCTYPE in output character, the below error occurs:-

Static error at xsl:output-character
XTSE0020: character attribute must be a single XML character

Is there a way to use it or unescape the entities in output xml for &key; and &hyphen;?

Rahul
  • 21
  • 6
  • If you declare e.g. `<!ENTITY key "&key;">` then the attribute value of e.g. `character="&key;"` is e.g. `&key;`, that is the string with five characters `&`, `k`, `e`, `y`, `;`. So obviously the requirement for `character` to be a single character is not met. – Martin Honnen Jul 10 '23 at 10:00
  • is there any alternate to using something else instead of output character for these user defined entities? – Rahul Jul 10 '23 at 11:40
  • It would really help to stand back from this and explain what you are trying to achieve. Generally, DTD-declared entities don't play well with XSLT. Could you solve the business need in a completely different way? – Michael Kay Jul 10 '23 at 11:57
  • As said in the past, the commercial editions of Saxon have some extensions to deal with entity references/DTDs, like https://www.saxonica.com/html/documentation12/extensions/instructions/entity-ref.html or `saxon:internal-dtd-subset` on `xsl:output` in https://www.saxonica.com/html/documentation12/extensions/output-extras/serialization-parameters.html. But I agree with Michael Kay, judged by all the related questions with all the entity stuff it seems you are trying to achieve something with XSLT that is not really supported and probably not necessary (in terms of pure XSLT/XML processing). – Martin Honnen Jul 10 '23 at 12:05
  • Related/duplicate question: https://stackoverflow.com/q/5985615/317052 – Daniel Haley Jul 10 '23 at 14:17

1 Answers1

0

Is there a way to use it or unescape the entities in output xml for &key; and ‐?

Character maps provide a way to change the serialization of individual characters in the XSLT output. They don't provide a way to change the serialization of arbitrary strings. So the simple answer is "no".

If you really want to generate entity references in your serialized output, my recommendation would be that the XSLT processor should generate something like §key; and you should convert this to &key; in a post-processing phase (using non-XML-aware tools).

However, I would also consider whether using entity references is the best way to meet your business requirements. Using XInclude elements or processing instructions can give you more flexibility.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164