Zmodyfikuj przestrzenie nazw w danym dokumencie xml za pomocą lxml

Mam dokument xml, który wygląda tak:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://someurl/Oldschema"
     xsi:schemaLocation="http://someurl/Oldschema Oldschema.xsd"
     xmlns:framework="http://someurl/Oldframework">
   <framework:tag1> ... </framework:tag1>
   <framework:tag2> <tagA> ... </tagA> </framwork:tag2>
</root>

Wszystko, co chcę zrobić, to zmienićhttp://someurl/Oldschema dohttp://someurl/Newschema ihttp://someurl/Oldframework dohttp://someurl/Newframework i pozostaw pozostały dokument bez zmian. Z pewnymi spostrzeżeniami z tego wątkulxml: dodaj przestrzeń nazw do pliku wejściowego, Spróbowałem:

def fix_nsmap(nsmap, tag):
    """update the old nsmap-dict with the new schema-urls. Example:
    fix_nsmap({"framework": "http://someurl/Oldframework",
               None: "http://someurl/Oldschema"}) ==
      {"framework": "http://someurl/Newframework",
       None: "http://someurl/Newschema"}"""
    ...

from lxml import etree
root = etree.parse(XMLFILE).getroot()
root_tag = root.tag.split("}")[1]
nsmap = fix_nsmap(root.nsmap)
new_root = etree.Element(root_tag, nsmap=nsmap)
new_root[:] = root[:]
# ... fix xsi:schemaLocation
return etree.tostring(new_root, pretty_print=True, encoding="UTF-8",
    xml_declaration=True) 

Daje to odpowiednie „atrybuty” w znaczniku głównym, ale całkowicie kończy się niepowodzeniem w pozostałej części dokumentu:

<network xmlns:framework="http://someurl/Newframework"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://someurl/Newschema"
    xsi:schemaLocation="http://someurl/Newschema Schema.xsd">
<ns0:tag1 xmlns:ns0="http://someurl/Oldframework"> ... </ns0:information>
<ns1:tag2 xmlns:ns1="http://someurl/Oldframework"
          xmlns:ns2="http://someurl/Oldschema">
    <ns2:tagA> ... </ns2:tagA>
</ns1:tag2>

Co jest nie tak z moim podejściem? Czy istnieje inny sposób zmiany przestrzeni nazw? Może mógłbym użyć xslt?

Dzięki!

Denis

questionAnswers(1)

yourAnswerToTheQuestion