Source

scatter-share / balisage09 / slide26.py

from amara import bindery

MONTY_XML = """<quotes>
  <quote skit="1">This parrot is dead</quote>
  <quote skit="2">What do you mean "bleh"</quote>
  <quote skit="2">I don't like spam</quote>
  <quote skit="3">But I was looking for argument</quote>
</quotes>"""

doc = bindery.parse(MONTY_XML)
q1 = doc.quotes.quote # or doc.quotes.quote[0]
print q1.skit
print q1.xml_attributes[(None, u'skit')] # XPath works too: q1.xml_select(u'@skit')

for q in doc.quotes.quote: # The loop will pick up both q elements
    print unicode(q) # Just the child char data

from itertools import groupby #Python stdlib
from operator import attrgetter #Python stdlib

skit_key = attrgetter('skit')
for skit, quotegroup in groupby(doc.quotes.quote, skit_key):
    print skit, [ unicode(q) for q in quotegroup ]