XSLT to put existing !ENTITY refs inside <!DOCTYPE > of XML

43 Views Asked by At

I've reviewed existing questions/answers, but none are quite the same ask, or no answer is given. I have an existing XML document with this

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE thing PUBLIC "XXX//DTD XML DTD for things//EN" "C:\work\thing.dtd" [<!ENTITY abreve "&#x103;"><!ENTITY agr "&#945;"><!ENTITY amacr "&#x101;"><!ENTITY bgr "&#946;">]>
    <thing>
    [...]
    </thing>

I use this XSLT to alter the document in another unrelated way, which works fine:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/asset" priority="10">
    [...]
    <output method="xml" omit-xml-declaration="no" indent="no" encoding="UTF-8" doctype-public="XXX//DTD XML DTD for things//EN" doctype-system="C:\work\thing.dtd"/>
    [...]

but the output doesn't give the ENTITY items, just this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE thing PUBLIC "XXX//DTD XML DTD for things//EN" "C:\work\thing.dtd">
    <thing>
    [...]
    </thing>

The solution can read the ENTITY stuff from the file if need be, but doesn't have to, as it is the same in all instances, so it could just feed the ENTITY stuff in anew. Thanks.

1

There are 1 best solutions below

0
Michael Kay On

I'm afraid XSLT doesn't play well with DTDs. There's no way in standard XSLT to read the DTD of the input document or write the DTD of the output document, and there's no way to prevent entity and character references in the input being expanded (and therefore lost). There are a few tricks to work around some of these limitations, but none of them are very satisfactory.