[Twisted-web] domish.elementStream()

Ross Jekel ross at sourcelabs.com
Tue Jun 21 15:44:18 MDT 2005


Neither or the two possible element stream classes when parsing
correctly hook the root element to its children. This is pretty easy to
see why from the code (below) but thought I would check to see if this
is on purpose or something. What I wanted to do was just save off the
root element passed to DocumentStartEvent from either class and be able
to do XPathQueries on it. Since the root element returned doesn't have
any children though, it fails.

The code for both classes is similar to this:

        # Construct the new element
        e = Element(qname, self.defaultNsStack[-1], attrs)

        # Document already started
        if self.documentStarted == 1:
            if self.currElem != None:
                e.parent = self.currElem
            self.currElem = e

        # New document
            self.documentStarted = 1

Since self.currElem isn't set to e for the first element, e never gets
it's children list filled in.

Below is a sample program that shows what I'm talking about. It prints
"0 []" when it should print "1 [<<twisted.xish.domish.Element
object...>, ...]".

from twisted.xish import domish
from twisted.xish import xpath

# Taken from twisted/xish/test/test_xpath.py
xmlBlob = """
<foo xmlns='testns' attrib1='value1' attrib3="user at host/resource">
     <bar attrib2="value2">

p = domish.SuxElementStream()
elem = list()
p.DocumentStartEvent = lambda e: elem.append(e)
p.ElementEvent = lambda e: None
p.DocumentEndEvent = lambda: None
print xpath.matches("/foo/bar", elem[0]), elem[0].children


More information about the Twisted-web mailing list