Page 1 of 1

How to add internal DTD subset to the newly created xml file using javax.xml

Posted: Sun Oct 02, 2022 8:19 am
by vishwavaranasi
Hello Team , I know this is a general java question

am using the API

import javax.xml.parsers.DocumentBuilder;

import javax.xml.transform.Transformer;

to create a xml

I am trying to add the DOCTYPE and DTD declaration as below

Code: Select all

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformer = transformerFactory.newTransformer();
        
        transformer.setOutputProperty(
                  OutputKeys.DOCTYPE_PUBLIC, "-//XXX//DTD XXX YYY XXX//EN");
        transformer.setOutputProperty(
                  OutputKeys.DOCTYPE_SYSTEM, "XXX.dtd");
but my final doctype declaration should be

Code: Select all

DOCTYPE XXX PUBLIC "-//XXX//DTD XXX YYY XXX//EN" "XXX.dtd"[]
I want add at the end an empty internal subset [] ..how to do this in java ?

Re: How to add internal DTD subset to the newly created xml file using javax.xml

Posted: Mon Oct 03, 2022 7:51 am
by Radu
Hi,
You cannot set an internal DTD subset just by setting properties on the transformer.
You could try to force outputting the DOCTYPE along with its subset directly from the XSLT template:

Code: Select all

 <xsl:template match="/">
        <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE abc[&lt;!ENTITY ent 'test'>]></xsl:text>
or as a workaround after applying the XSLT and producing the content, apply some regular expression on the content and replace the empty DOCTYPE string with a DOCTYPE which contains an internal subset.

Regards,
Radu

Re: How to add internal DTD subset to the newly created xml file using javax.xml

Posted: Mon Oct 03, 2022 8:57 am
by vishwavaranasi
Thanks Radu :)