1. Uche Ogbuji
  2. scatter-share

Source

scatter-share / balisage09 / slide28.py

from amara import bindery
from amara.bindery.model import *

MONTY_XML = """<monty>
  <python spam="eggs">What do you mean "bleh"</python>
  <python ministry="abuse">But I was looking for argument</python>
</monty>"""

doc = bindery.parse(MONTY_XML)

#Add a constraint that `python` elements must have a `ministry` attribute
c = constraint(u'@ministry')
try:
    doc.monty.python.xml_model.add_constraint(c, validate=True)
except bindery.BinderyError, e:
    # Exception will be raised because the doc doesn't meet the constraint we added
    pass # ignore and move on

#Update the doc to meet the desired constraint
doc.monty.python.xml_attributes[None, u'ministry'] = u'argument'
doc.monty.python.xml_model.add_constraint(c, validate=True)