[XML-SIG] Choosing SAX parsers

Lars Marius Garshol larsga@garshol.priv.no
25 Jun 2001 10:28:49 +0200


* Graham Ashton
| 
| This is fine, and xml.sax works nicely with expat now that I've
| recompiled, but I was a bit surprised that expat was the only parser
| availble after the docs had been telling me how I could choose between
| parsers, etc. Am I missing some stuff?

You are. expat is the only parser with a SAX driver that comes with
Python 2.x, but the XML-SIG package contains more.
 
| What I'd really like is a parser that knows how to enforce DTDs,
| which I know expat-1.x doesn't. Speed isn't an issue for me as my
| documents are very small.

Then you want to use xmlproc. xmlproc can validate XML documents
against a DTD if you ask it to, by setting the feature
xml.sax.handler.feature_validation to true.

[larsga@pc36 larsga]$ python2.1 
Python 2.1 (#1, May  5 2001, 06:49:59) 
[GCC 2.95.1 19990816/Linux (release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from xml.sax import make_parser
>>> make_parser()
<xml.sax.expatreader.ExpatParser instance at 0x814090c>
>>> make_parser("xml.sax.drivers2.drv_xmlproc")
<xml.sax.drivers2.drv_xmlproc.XmlprocDriver instance at 0x82e9164>
>>> p = make_parser("xml.sax.drivers2.drv_xmlproc")
>>> from xml.sax.handler import feature_validation
>>> p.setFeature(feature_validation, 1)
 
| What's my best bet? Installing PyXML? 

Yes.

| If I do, will it overwrite the xml.* module tree that I've got
| already? Are there any issues installing it over the top of the
| stuff in the standard library (some of which I believe is the same
| codebase)?

This should work just fine. The XML-SIG package puts itself in a
package called _xmlplus and does some import trickery to make sure
that it is imported when you say import xml.

--Lars M.