Getting namespace dictionary using nsmap to parse different files with same python script

Viewed 124

I have 2 different xml files with the following name spaces f1 has the namespace {'ns1': 'http://www.internaltext.com/document/informationtable'}

f2 has the namespace

{'abc': 'http://www.externaltest.org/2016/XMLSchema-instance', None: 'http://www.internaltext.com/document/informationtable'}

As can be seen, the namesapce have different keys for the same values.

I am able to parse both the files using the same python script if I create a namespace dictionary and use it:

    from lxml import etree
    d1 = etree.parse(f1)
    r1 = d1.getroot()
    ns = {'abc': 'http://www.externaltest.org/2016/XMLSchema-instance', 'miss': 'http://www.sec.gov/edgar/document/thirteenf/informationtable'}
    for i in r1.findall('miss:infoTable',ns):
         name = i.find('miss:name',ns)

But is there a way to do this, without defining the namespace dictionary and using the nsmap function to extract the namespace dictionary and use that? Like

    ns = r1.nsmap
0 Answers
Related