xml.sax feature question

christof hoeke csad7 at yahoo.com
Sun Oct 26 07:02:44 EST 2003


Martin v. Löwis wrote:

> christof hoeke <csad7 at yahoo.com> writes:
> 
> 
>>the problem i have is that if the xmlfile has a doctype declaration
>>the sax parser tries to load it and fails (IOError if course).
>>partly because the path to the DTD is just a simple name in the same
>>dir e.g. <!DOCTYPE contacts SYSTEM "contacts.dtd"> and i guess the
>>parser does not use the path os.path.walk uses (can i somehow give the
>>parser this information?). but it also could be a DTD which should be
>>loaded over a network which is not available at the time.
> 
> 
> In XML, the SYSTEM identifier is a URI reference; in your case, it is
> a relative URL. An XML processor must interpret this relative to the
> URL of the main document. If you have the main document on a local
> disk, the relative URL will be intepreted relative to the file name.
> So you should put the DTD along with the document (in the same
> directory).

this is what i did but still i get the exception for example for 
xmltest\contacts.xml "[Errno 2] No such file or directory: 
'contacts.dtd'" if xmltest contains contacts.xml with the SYSTEM 
identifier "contacts.dtd" and contacts.dtd is in the same directory.


>>i guess to simply set a feature of the sax parser to not try to load
>>any external DTDs should work. 
> 
> You need to turn off resolution of general entities:
> 
> p.setFeature("http://xml.org/sax/features/external-general-entities",False)


exactly what i was looking for, thanks a lot. still i wonder why the 
above error happens.

> Alternatively, you can install an entity handler which then uses a
> different mechanism of resolving the DTD (and other external entities).

i think i get a copy of the sax2 book to look into that a bit more...

thanks
christof





More information about the Python-list mailing list