简体   繁体   中英

Remove namespace with xmltodict in Python

xmltodict converts XML to a Python dictionary. It supports namespaces. I can follow the example on the homepage and successfully remove a namespace. However, I cannot remove the namespace from my XML and cannot identify why? Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<status xmlns:mystatus="http://localhost/mystatus">
<section1
    mystatus:field1="data1"
    mystatus:field2="data2" />
<section2
    mystatus:lineA="outputA"
    mystatus:lineB="outputB" />
</status>

And using:

xmltodict.parse(xml,process_namespaces=True,namespaces={'http://localhost/mystatus':None})

I get:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'@http://localhost/mystatus:field1', u'data1'), (u'@http://localhost/mystatus:field2', u'data2')])), (u'section2', OrderedDict([(u'@http://localhost/mystatus:lineA', u'outputA'), (u'@http://localhost/mystatus:lineB', u'outputB')]))]))])

instead of:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'field1', u'data1'), (u'field2', u'data2')])), (u'section2', OrderedDict([(u'lineA', u'outputA'), (u'@lineB', u'outputB')]))]))])

Am I making some simple mistake, or is there something about my XML that prevents the process_namespace modification from working correctly?

xmltodict is based on expat , so namespaces should applied to the class name, not attribute names:

<?xml version="1.0" encoding="UTF-8"?>
<status xmlns:mystatus="http://localhost/mystatus">
    <mystatus:section1 field1="data1" field2="data2" />
    <mystatus:section2 lineA="outputA" lineB="outputB" />
</status>

When parsed with:

foo = xmltodict.parse(xml,
                      process_namespaces=True,
                      namespaces={'http://localhost/mystatus':None})

outputs:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'@field1', u'data1'), (u'@field2', u'data2')])), (u'section2', OrderedDict([(u'@lineA', u'outputA'), (u'@lineB', u'outputB')]))]))])

Accessing it is easy:

# Get attribute 'lineA' from class 'section2' from class 'status'
>>> foo.get('status').get('section2').get('@lineA')
u'outputA'

Attribute namespaces are only required when you have multiple attributes of the same name (eg multiple id's or multiple prices, etc), in which case, I couldn't get expat or xmltodict to parse it correctly. YMMV though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM