Python xml schema parsing for simpleContent and simpleTypes I am writing a few python functions to parse through an xml schema for reuse later to check and create xml docs in this same pattern. Below are two functions I wrote to parse out data from simpleContent and simpleType objects. After writing this, it looked pretty messy to me and I'm sure there is a much better (more pythonic) way to write these functions and am looking for any assistance. I am use lxml for assess to the etree library.

def get_simple_type(element):
    simple_type = {}
    ename = element.get("name")
    simple_type[ename] = {}
    simple_type[ename]["restriction"] = element.getchildren()[0].attrib
    elements = element.getchildren()[0].getchildren()
    simple_type[ename]["elements"] = []
    for elem in elements:
        simple_type[ename]["elements"].append(elem.get("value"))
    return simple_type  

def get_simple_content(element):
    simple_content = {}
    simple_content["simpleContent"] = {}
    simple_content["simpleContent"]["extension"] = element.getchildren()[0].attrib
    simple_content["attributes"] = []
    attributes = element.getchildren()[0].getchildren()
    for attribute in attributes:
        simple_content["attributes"].append(attribute.attrib)
    return simple_content


Examples in the schema of simpleContent and simpleTypes (they will be consistently formatted so no need to make the code more extensible for the variety of ways these elements could be represented in a schema):

&lt;xs:simpleContent&gt;
    &lt;xs:extension base="xs:integer"&gt;
        &lt;xs:attribute name="sort_order" type="xs:integer" /&gt;
    &lt;/xs:extension&gt;
&lt;/xs:simpleContent&gt;

&lt;xs:simpleType name="yesNoOption"&gt;
    &lt;xs:restriction base="xs:string"&gt;
      &lt;xs:enumeration value="yes"/&gt;
      &lt;xs:enumeration value="no"/&gt;
      &lt;xs:enumeration value="Yes"/&gt;
      &lt;xs:enumeration value="No"/&gt;
    &lt;/xs:restriction&gt;
&lt;/xs:simpleType&gt;


The code currently creates dictionaries like those show below, and I would like to keep that consistent:

{'attributes': [{'type': 'xs:integer', 'name': 'sort_order'}], 'simpleContent': {'extension': {'base': 'xs:integer'}}}


{'yesNoOption': {'restriction': {'base': 'xs:string'}, 'elements': ['yes', 'no', 'Yes', 'No']}}
Do more with literals:

def get_simple_type(element):
    return {
        element.get("name"): {
            "restriction": element.getchildren()[0].attrib,
            "elements": [ e.get("value") for e in element.getchildren()[0].getchildren() ]
        }
    }

def get_simple_content(element):
    return { 
        "simpleContent": {
            "extension": element.getchildren()[0].attrib,
            "attributes": [ a.attrib for a in element.getchildren()[0].getchildren() ]
         }
    }