resolving relative paths for external entities with xml.sax

Uche Ogbuji uche at ogbuji.net
Fri Aug 22 11:55:51 EDT 2003


Bart <bart_vanhaute_ at hotmail.com> wrote in message news:<pan.2003.08.17.12.42.56.153674 at hotmail.com>...
> On Fri, 15 Aug 2003 06:24:02 -0700, Uche Ogbuji wrote:
> 
> >> From what I understand of XML specs, relative paths in external entities
> >> are relative to the location of the document *in which they are defined*.
> >> However, using xml.sax (which calls expat, I presume), the paths are
> >> resolved relative to the current working directory. So the parser can not
> >> find the test.inc when the current working directory is e.g. dir1.
> > 
> > I, for one, need to see your Python code to tell, because if you're
> > using the APIs rightly parsing the first file as a URI rather than,
> > say, reading it in as a string first), then you should not have the
> > problem you report.
> 
> something like:
> 	from xml.sax import make_parser
> 	from xml.sax.saxutils import XMLGenerator
> 	reader = make_parser()
> 	reader.setContentHandler(XMLGenerator())
> 	reader.parse('test.xml')
> 
> this is run in the test1 dir. 
> 
> I also tried using absolute paths, xml.sax.InputSource, 'file:' URLs and
> combinations of these. 
> 
> The result is either:
> OSError: [Errno 2] No such file or directory: 'test.inc'
> or (when using 'file://tmp/xmltest/test1/test.xml'): 
> OSError: [Errno 2] No such file or directory: '/tmp/xmltest/test1/test.inc'
> or (when using 'file:test.xml'):
> OSError: [Errno 2] No such file or directory: '/test.inc'

I'm not sure what's up.  This works for me (on PyXML 0.8.3):

[uogbuji at borgia uogbuji]$ cat test.py
#!/usr/bin/env python
 
from xml.sax import make_parser
from xml.sax.saxutils import XMLGenerator
reader = make_parser()
reader.setContentHandler(XMLGenerator())
reader.parse('dummy.xml')
 
[uogbuji at borgia uogbuji]$ python test.py
<?xml version="1.0" encoding="iso-8859-1"?>
<dummy xmlns:pre="" pre:foo="bar"></dummy>

--Uche
http://uche.ogbuji.net




More information about the Python-list mailing list