1

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
  • QName objects? Commented Oct 22, 2012 at 19:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.