[Twisted-web] domish.elementStream()

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


Hello,

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:
                self.currElem.children.append(e)
                e.parent = self.currElem
            self.currElem = e

        # New document
        else:
            self.documentStarted = 1
            self.DocumentStartEvent(e)

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">
     somecontent
     <bar>
        <foo>
         <gar>DEF</gar>
        </foo>
     </bar>
     somemorecontent
     <bar attrib2="value2">
        <bar>
          <foo/>
          <gar>ABC</gar>
        </bar>
     </bar>
 </foo>
"""

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

Thanks,
Ross




More information about the Twisted-web mailing list