def xml_to_json(root):
j = {}<EOL>if len(root) == <NUM_LIT:0>:  <EOL><INDENT>return _maybe_intify(root.text)<EOL><DEDENT>if len(root) == <NUM_LIT:1> and root[<NUM_LIT:0>].tag.startswith('<STR_LIT:{>' + NS_GML):  <EOL><INDENT>return gml_to_geojson(root[<NUM_LIT:0>])<EOL><DEDENT>if root.tag == '<STR_LIT>':<EOL><INDENT>j['<STR_LIT>'] = {'<STR_LIT:version>': root.get('<STR_LIT:version>')}<EOL><DEDENT>for elem in root:<EOL><INDENT>name = elem.tag<EOL>if name == '<STR_LIT>' and elem.get('<STR_LIT>'):<EOL><INDENT>name = elem.get('<STR_LIT>') + '<STR_LIT>'<EOL>if name == '<STR_LIT>':<EOL><INDENT>name = '<STR_LIT:url>'<EOL><DEDENT>if root.tag == '<STR_LIT>':<EOL><INDENT>j['<STR_LIT>'][name] = elem.get('<STR_LIT>')<EOL>continue<EOL><DEDENT><DEDENT>elif name.startswith('<STR_LIT:{>' + NS_PROTECTED):<EOL><INDENT>name = '<STR_LIT:!>' + name[name.index('<STR_LIT:}>') + <NUM_LIT:1>:] <EOL><DEDENT>elif name[<NUM_LIT:0>] == '<STR_LIT:{>':<EOL><INDENT>name = '<STR_LIT:+>' + name[name.index('<STR_LIT:}>') + <NUM_LIT:1>:]<EOL><DEDENT>if name in j:<EOL><INDENT>continue  <EOL><DEDENT>elif elem.tag == '<STR_LIT>' and not elem.text:<EOL><INDENT>j[name] = elem.get('<STR_LIT>')<EOL><DEDENT>elif len(elem):<EOL><INDENT>if name == '<STR_LIT>':<EOL><INDENT>j[name] = [xml_link_to_json(child, to_dict=False) for child in elem]<EOL><DEDENT>elif name in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>j[name] = [xml_link_to_json(child, to_dict=True) for child in elem]<EOL><DEDENT>elif all((name == pluralize(child.tag) for child in elem)):<EOL><INDENT>j[name] = [xml_to_json(child) for child in elem]<EOL><DEDENT>else:<EOL><INDENT>j[name] = xml_to_json(elem)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if root.tag == '<STR_LIT>' and name.endswith('<STR_LIT:s>') and not elem.text:<EOL><INDENT>j[name] = []<EOL><DEDENT>else:<EOL><INDENT>j[name] = _maybe_intify(elem.text)<EOL><DEDENT><DEDENT><DEDENT>return j<EOL>
Convert an Open511 XML document or document fragment to JSON.

    Takes an lxml Element object. Returns a dict ready to be JSON-serialized.