DB_NAME = "mgi" client = MongoClient(MONGODB_URI) db = client[DB_NAME] xmldata = db['xmldata'] for cursor in xmldata.find({ "content.amProjectDB.@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance" }): projectIDs.add(cursor["content"]["amProjectDB"]["amProject"]["projectID"]) product_element = SchemaElement() product_element.tag = u'restriction' product_element.value = u"blank" product_element.options = {u'base': u'xs:string'} product_element.children = [] product_element.save() for ProjectID in projectIDs: xsd_element = SchemaElement() xsd_element.tag = u'enumeration' xsd_element.value = str(ProjectID).decode('UTF-8') xsd_element.options = {} xsd_element.children = [] xsd_element.save() product_element.children.append(xsd_element) product_element.save() a = product_element product_element = SchemaElement() product_element.tag = u'simple_type' product_element.value = None