[XML-SIG] chaining sax handlers

Jed Parsons jedp at ilm.com
Wed Feb 2 18:59:49 CET 2005

Hi, all,

I would like to do with sax processors what I can do with the document()
function in xslt, namely include other documents into the one that's being

Here's a sample handler, and three xml files.  This approach seems to work for
simple cases, but appears to break the innards of the handler (described

# ----------------------------------------------------------------------
# chaining handler

    class FooHandler(xml.sax.handler.ContentHandler):
        def characters(self, data):
            print data

        def include_proc(self, href):
            filter = xml.sax.make_parser()
        def startElement(self, name, attrs):
            if name == 'include':

# ----------------------------------------------------------------------
# some xml files to work with:

# file1.xml:
<?xml version='1.0'?>
 <bar>This is file1</bar>
 <include href='file3.xml' />

# file2.xml:
<?xml version='1.0'?>
 <bar>This is file2</bar>
 <include href='file3.xml' />
 <bar>Back in file2 again after include</bar>

# file3.xml:
<?xml version='1.0'?>
 <bar>This is file3.</bar>

# ----------------------------------------------------------------------
# results (with whitespace removed):

>>> filter = xml.sax.make_parser()
>>> handler = FooHandler()
>>> filter.setContentHandler(handler)
>>> filter.parse('file1.xml')
This is file1
This is file3.
>>> filter.parse('file2.xml')
This is file2
This is file3.
Back in file2 again after include

So this seems to work in a simple case.  w00t!  But in a more involved handler,
I get errors like "weakly-referenced object no longer exists" when I try to
access the document locator after re-entering.

Can anyone tell me what I'm doing wrong?

Many thanks for any help,


Jed Parsons                        / Industrial Light + Magic : 415.448.2974

More information about the XML-SIG mailing list