I have to process XML files that have a DTD with a XSLT in Java. The DTD is really needed because it contains the definitions of entities I use. (aside: yes, using entities for stuff that could use unicode is a bad idea ;-)
When I run the transformation it downloads the DTD from the external source every time. I want it to use a XML catalog to cache the DTDs so I gave the TransformerFactory
a CatalogResolver
as URIResolver
:
URIResolver cr = new CatalogResolver();
tf = TransformerFactory.newInstance();
tf.setURIResolver(cr);
Transformer t = tf.newTransformer(xsltSrc);
t.setURIResolver(cr);
Result res = new SAXResult(myDefaultHandler());
t.transform(xmlSrc, res);
But when I run the transformation it still downloads the DTDs over the network. (Using Xalan and Xerces either as part of Java5 or standalone or using Saxon and Xerces.)
What does it take to force the transformation to only use the local copy of the DTDs?
(I'm answering my own question here to save me the next time, or anyone else, the days of tinkering I needed to find the answer.)
What it really needs to change the way DTDs are resolved is an EntityResolver
. Unfortunately it is not possible to set the EntityResolver
to be used by the Transformer
. So you have to create an XMLReader
first with the CatalogResolver
as its EntityResolver
:
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader r = spf.newSAXParser().getXMLReader();
EntityResolver er = new CatalogResolver();
r.setEntityResolver(er);
and use it in for the Transformer
:
SAXSource s = new SAXSource(r, xmlSrc);
Result res = new SAXResult(myDefaultHandler());
transformer.transform(s, res);
You can use this code to disable this kind of functionality in Xerces:
org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader();
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
This code sample uses Dom4j, but similar "setFeature" functionality exists in other java XML libraries such as JDOM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With