Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: Relative URI "my.dtd" can not be resolved without a base URI

Reply
Thread Tools

XSLT: Relative URI "my.dtd" can not be resolved without a base URI

 
 
Pavel
Guest
Posts: n/a
 
      08-02-2004
Greetings to all -

I'm having a problem processing xml with relative dtd URI specified
using XSLT:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mydoc SYSTEM "my.dtd">
<mydoc>
....
</mydoc>

I do not have a control over the original xml but I have my.dtd in the
app classpath. I'm getting the TransformerException: Relative URI
"my.dtd" can not be resolved without a base URI.

How do I tell to javax.xml.transform.Transformer where to find the dtd
file w/out specifying the base URI (or any other modifications of the
original xml)?

Here is how I call the xsl transformer:

// 1. Instantiate a TransformerFactory.
javax.xml.transform.TransformerFactory tFactory =
javax.xml.transform.TransformerFactory.newInstance ();

// 2. Use the TransformerFactory to process the stylesheet Source and
// generate a Transformer.
javax.xml.transform.Transformer transformer =
tFactory.newTransformer(new
javax.xml.transform.stream.StreamSource(xsl));

//transformer.setURIResolver(new RelativeURIResolver());

// 3. Use the Transformer to transform an XML Source and send the
// output to a Result object.
transformer.transform(
new javax.xml.transform.stream.StreamSource(xml),
new javax.xml.transform.stream.StreamResult(out));

As you can see above, I've tried to use URIResolver() but it does not
appear to be used by the transformer to resolve the DTD.

I'm using Java(TM) 2 Runtime Environment, Standard Edition (build
1.4.2_03-b02) and default xslt processor and xml parser from there
("org.apache.xalan.processor.TransformerFactoryImp l" and
"org.apache.crimson.jaxp.DocumentBuilderFactoryImp l" I assume).

Of course, I could use java.util.regex to either remove
<!DOCTYPE[a-zA-Z0-9\". ]*> completely or to add the base url there.
But I wonder if there is more elegant solution to this.

Any suggestions are very appreciated.
Thanks
Pavel
 
Reply With Quote
 
 
 
 
Daniel Parker
Guest
Posts: n/a
 
      08-03-2004
"Pavel" <olgt-> wrote in message
news: om...
> Greetings to all -
>
> I'm having a problem processing xml with relative dtd URI specified
> using XSLT:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <!DOCTYPE mydoc SYSTEM "my.dtd">
> <mydoc>
> ...
> </mydoc>
>
> I do not have a control over the original xml but I have my.dtd in the
> app classpath. I'm getting the TransformerException: Relative URI
> "my.dtd" can not be resolved without a base URI.
>
> How do I tell to javax.xml.transform.Transformer where to find the dtd
> file w/out specifying the base URI (or any other modifications of the
> original xml)?
>

Hi Pavel,

Setting a custom URIResolver on the transformer won't help, since this URI
resolver is used only for resolving relative URI's passed to the XSLT
document function.

What you need to do instead is, on an XMLReader that is reading the XML
file, call the setEntityResolver method to set a custom EntityResolver.
Your custom EntityResolver needs to provide a resolveEntity method that will
do the job of substituting the absolute URL for the relative URL, see the
example of an EntityResolver in the java api docs.

So, instead of using a StreamSource to read the document, create an
XMLReader using an XMLReaderFactory, and set the EntityResolver to your
custom resolver, also create an InputSource object with the file URL, and
finally create a SAXSource object with the XMLReader and the InputSource.
That should do it.

Regards,
Daniel Parker
http://servingxml.sourceforge.net/


 
Reply With Quote
 
 
 
 
Peter Flynn
Guest
Posts: n/a
 
      08-03-2004
Pavel wrote:

> Greetings to all -
>
> I'm having a problem processing xml with relative dtd URI specified
> using XSLT:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <!DOCTYPE mydoc SYSTEM "my.dtd">
> <mydoc>
> ...
> </mydoc>
>
> I do not have a control over the original xml but I have my.dtd in the
> app classpath. I'm getting the TransformerException: Relative URI
> "my.dtd" can not be resolved without a base URI.
>
> How do I tell to javax.xml.transform.Transformer where to find the dtd
> file w/out specifying the base URI (or any other modifications of the
> original xml)?


The default is usually to look for the DTD in the same place as the document
instance. If you can work out where Java thinks the document is coming from,
and put the DTD in the same place (directory/pipe/stream) then it should
find it.

///Peter
--
"The cat in the box is both a wave and a particle"
-- Terry Pratchett, introducing quantum physics in _The Authentic Cat_
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get absolute uri by combining the baseuri and the relative uri in an html page? Turbo Javascript 2 11-01-2006 01:08 AM
java.net.URI.relativize(java.net.URI) not really working Stanimir Stamenkov Java 1 08-17-2005 06:24 PM
Help with error: "Invalid URI: The format of the URI could not be determined." Simon Harris ASP .Net 0 05-10-2005 04:33 PM
Re: XSLT: Relative URI "my.dtd" can not be resolved without a base URI etheriau XML 1 08-23-2004 01:54 PM
Invalid URI: The format of the URI could not be determined. Joe Curry ASP .Net Web Services 0 10-08-2003 04:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57