Converting my python script from lxml to xml.etree

2019-07-27 01:01发布

问题:

I am trying to convert my script (https://github.com/fletchermoore/n2c2) to use the default package xml.etree instead of lxml. This was an oversight on my part, but now I am realizing it would be impossible to get my target audience to set up lxml on their macs.

I think that most of the code should just work by switching out the import, but when I tried it I found out that xml.etree handles namespaces differently (which I do not understand). Specifically, what would be the easiest way to convert the setTagNames function here. xml is created from the .fromstring() method.

def setTagNames(self, xml):
    officens = xml.nsmap['office']
    textns = xml.nsmap['text']
    drawns = xml.nsmap['draw']
    xlinkns = xml.nsmap['xlink']
    stylens = xml.nsmap['style']
    fons = xml.nsmap['fo']

    names = {}
    names['body'] = '{%s}body' % officens
    names['text'] = '{%s}text' % officens
    names['auto-styles'] = '{%s}automatic-styles' % officens
    names['list'] = '{%s}list' % textns
    names['list-item'] = '{%s}list-item' % textns
    names['p'] = '{%s}p' % textns
    names['line-break'] = '{%s}line-break' % textns
    names['tab'] = '{%s}tab' % textns
    names['span'] = '{%s}span' % textns
    names['frame'] = '{%s}frame' % drawns
    names['image'] = '{%s}image' % drawns

    names['href'] = '{%s}href' % xlinkns
    names['name'] = '{%s}name' % stylens
    names['style-name'] = '{%s}style-name' % textns
    names['font-weight'] = '{%s}font-weight' % fons
    names['text-underline-style'] = '{%s}text-underline-style' % stylens
    names['font-style'] = '{%s}font-style' % fons
    names['font-size'] = '{%s}font-size' % fons
    names['background-color'] = '{%s}background-color' % fons
    names['color'] = '{%s}color' % fons

    names['style'] = '{%s}style' % stylens
    names['text-properties'] = '{%s}text-properties' % stylens
    self.names = names
    self.builder.names = names

回答1:

I found the answer in another post: Accessing XMLNS attribute with Python Elementree?

I used the function written by deancutlet to create the nsmap. Everything else in my code seems to work without modification.