[XML-SIG] end element handling with XMLValidator?

Jed Parsons jedp@ilm.com
Thu, 8 May 2003 11:02:31 -0700 (PDT)


I'm unable to get a hold of 'end element' events from the validating
parser.  Can anyone explain to me what I'm doing wrong?

Here is a test to show you what's happening:

 1. I extend the xmlproc.Application class, which is what
    xmlval.ValidatingApp does:

<code>
from xml.parsers.xmlproc import xmlproc
class PleaseOhPleaseWork(xmlproc.Application):
    def handle_data(self, data, start, end):
        print "%d chars of good data: [%s]" % (end-start, data[start:end])

    def handle_ignorable_data(self, data, start, end):
        print "%d chars of trash: [%s]" % (end-start, data[start:end])

    def handle_start_tag(self, name, attrs):
        print "start", name

    def handle_end_tag(self, name):
        print "end", name

</code>

 2. I use this test xml file:

<?xml version='1.0'?>
<!DOCTYPE foo SYSTEM "foo.dtd">
<foo>
 <para>
  Glug is the son of Zeus
 </para>
</foo>

 3. I can run two tests at the python console.  Using
    xmlproc.XMLProcessor, I get my start and end handlers.  Using
    xmlval.XMLValidator, I only get the start handler:

> python
Python 2.1.3 (#1, Apr 22 2002, 18:24:35) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from Test import PleaseOhPleaseWork
>>> from xml.parsers.xmlproc import xmlproc
>>> from xml.parsers.xmlproc import xmlval
>>> p_noval = xmlproc.XMLProcessor()
>>> p_noval.set_application(PleaseOhPleaseWork())
>>> p_noval.parse_resource('test.xml')
start foo
2 chars of good data: [
 ]
start para
28 chars of good data: [
  Glug is the son of Zeus
 ]
end para
1 chars of good data: [
]
end foo
>>> p_val = xmlval.XMLValidator()
>>> p_val.set_application(PleaseOhPleaseWork())
>>> p_val.parse_resource('test.xml')
start foo
2 chars of trash: [
 ]
start para
28 chars of good data: [
  Glug is the son of Zeus
 ]
1 chars of trash: [
]
>>> 

This is starting to drive me a little bit insane.  Can somebody
explain to me what is going on here?

Thanks,

Jed

I wrote yesterday (taking a different approach):
> 
> Hola -
> 
> I'm writing my first handler using the validating parser from
> xml.sax.sax2exts.XMLValParserFactory.make_parser().  I can't figure
> out how to handle end tags for elements.
> 
> I've set up a content handler that extends
> xml.sax.handler.ContentHandler.
> 
> I use the parser's setContentHandler method and parse.
> 
> My startElement, characters, and ignorableWhitespace methods all get
> called, but my endElement is sadly neglected.
> 
> A peek at _xmlplus/sax/drivers/drv_xmlproc_val.py finds no end element 
> handlers for this parser.
> 
> (CVS info: drv_xmlproc_val.py,v 1.9 2001/12/30 12:13:45 loewis)
> 
> What to do?
> 
> Thanks for any help,
> 
> Jed

-- 
Jed Parsons       Industrial Light + Magic  (415) 448-2974 
	     
grep(do{for(ord){(!$_&&print"$s\n")||(($O+=(($_-1)%6+1)and
grep(vec($s,$O++,1)=1,1..int(($_-6*6-1)/6))))}},(split(//,
"++,++2-27,280,481=1-7.1++2,800+++2,8310/1+4131+1++2,80\0.  What!?")));