[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
parsed.

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
below):

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

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

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

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

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

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

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

# ----------------------------------------------------------------------
# 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

-- 
Jed Parsons                        / Industrial Light + Magic : 415.448.2974
  
grep(do{for(ord){$o+=$_&7;grep(vec($j,+$o++,1)=1,5..($_>>3||print"$j\n"))}},
(split(//,"))*))2+29*2:.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:\7Glug!")));



More information about the XML-SIG mailing list