resolving relative paths for external entities with xml.sax

Uche Ogbuji uche at ogbuji.net
Fri Aug 15 09:24:02 EDT 2003


Bart <bart_vanhaute_ at hotmail.com> wrote in message news:<pan.2003.08.07.22.10.34.610124 at hotmail.com>...
> Dear python XML'ers,
> 
> I have a set of XML documents that use external entity definitions for
> which I am having problems parsing them using python and xml.sax. The 
> problem is with the resolution of relative paths in these. 
> An example:
> 
> dir1/test.xml:
> 	<?xml version="1.0"?>
> 	<!DOCTYPE test SYSTEM " ../dir2/test.dtd">
> 	<test>&ent;</test>
> 
> dir2/test.dtd:
> 	<!ENTITY ent SYSTEM "test.inc">
> 	<!ELEMENT test (#PCDATA)>
> 
> dir2/test.inc:
> 	external entity test
> 
> 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 do not see how creating an EntityResolver would help here: how do I 
> know against what base path I have to resolve the entity? 
> 
> (the same thing can be read without problems with other xml parser, for
> instance in Java).

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.

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




More information about the Python-list mailing list