[xsl] resolving DTDs while transforming

Subject: [xsl] resolving DTDs while transforming
From: "Pramodh Peddi" <peddip@xxxxxxxxxxxxxxxx>
Date: Wed, 17 Dec 2003 11:26:29 -0500
I posted a requestion and I did not get any response back. I thought it was
difficult to understand what I asked in my previous post.
Here is a better way of explaining my question(s):
I have an xml source, to be transformed, which has a relative DTD reference
(just the dtd filename is specified), like:
<?xml version="1.0" encoding="iso-8859-1"?>

The problem is myDTD.dtd is available NEITHER in the classpath NOR where the
class file is located. But, it is available in a http-accessible location,
like: http://myserver.com/dtd/myDTD.dtd.

So, I would like to tell the parser to consider
"http://myserver.com/dtd/myDTD.dtd"; instead of myDTD.dtd to validate the
source xml. (I know thats a stupid way of referring to a DTD, but thats the
customer's feed and they cannot change it).

Can any one help me get an approach.

Here is what I am doing: I am using Java1.4 API to transform. I am using
SAXSource to provide the xml source to the transformer. I am using
EntityResolver interface to resolve the DTD (to let the parser consider the
http://myserver.com/dtd/myDTD.dtd in place of myDTD.dtd, mentioned in the
source xml). I just have an idea how to do, but it does not work and no
wonder because I do not yet clearly understand how EntityResolver works.
Questions Like: What is the role of systemID, publicID, when does the parser
try to resolve entities (like DTDs), etc. Can anyone give a clear
understanding of how it works.

I hesitate giving the code, because the email becomes too large and
incognizable. But, in case it helps you help me better, here is the code:

public class SELTransformationService{

private String stylesheet;
private String fileName = null;
private String dtdURL = null;

private void transform(String metadata, String stylesheet,
ServiceRequest req, ServiceResponse res){

//System.out.println("TRANSFORM USING STRING");

try {

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(new StreamSource(new

InputStream inputStream =

OutputStream outputStream =

SAXParserFactory pfactory= SAXParserFactory.newInstance();


// Get an XMLReader.

XMLReader reader = pfactory.newSAXParser().getXMLReader();

//create a resolver to resolve the DTD in the source xml

//EntityResolver resolver = new DTDResolver();

reader.setEntityResolver(new DTDResolver());

DTDResolver resolver = (DTDResolver)reader.getEntityResolver();


SAXSource source = new SAXSource(reader,

new InputSource(new InputStreamReader(inputStream)));


transformer.transform(source, new StreamResult(new
OutputStreamWriter(outputStream, "iso-8859-1")));




log.info(fileName + " - OBJECT SENT OUT OF TRANSFORMATION-2");

} catch (Exception ex) {

res.error(req.getContentObject(), "Exception sending message to bus.");



}//end SELTransformationService classclass DTDResolver implements
EntityResolver {

String publicId = null;

public void setPublicId(String publicId){

this.publicId = publicId;

System.out.println("Setting publicID");


public String getPublicId(){

System.out.println("Getting publicId");

return this.publicId;


public InputSource resolveEntity (String publicId, String systemId){

InputStream inputStream = null;

InputSource source = null;


System.out.println("publicID is: " + this.publicId);


URL url = new URL(this.publicId);

inputStream = url.openStream();

System.out.println("got the inputstream");

source = new InputSource(new InputStreamReader(inputStream));


System.out.println("publicId is not specified!!!");


}catch(Exception e){


return source;


}//end DTDResolver class

When I do what is given above, the transformed string has empty xml (has
only xml header). I am sure I am doing something wrong. Can any one tell me
what is wrong in that. And making me understand how EntityResolver works
makes me feel lot better.



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread