Vedo che ci sono domande simili qui, ma nulla che mi abbia totalmente aiutato. Ho anche guardato la documentazione ufficiale sui namespace ma non riesco a trovare nulla che mi stia davvero aiutando, forse sono solo troppo nuovo per la formattazione XML. Capisco che forse ho bisogno di creare il mio dizionario del namespace? In entrambi i casi, ecco la mia situazione:Salvataggio XML tramite ETree in Python. Non mantiene gli spazi dei nomi e aggiunge ns0, ns1 e rimuove i tag xmlns
Sto ottenendo un risultato da una chiamata API, mi dà un XML che viene memorizzato come una stringa nella mia applicazione Python.
Quello che sto cercando di ottenere è catturare questo XML, scambiare un valore minuscolo (Il valore b: stringa valore ConditionValue/Default ma non è rilevante per questa domanda) e quindi salvarlo come stringa da inviare in seguito attivo in una chiamata POST Rest.
La sorgente XML assomiglia a questo:
<Context xmlns="http://Test.the.Sdk/2010/07" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<xmlns i:nil="true" xmlns="http://schema.test.org/2004/07/Test.Soa.Vocab" xmlns:a="http://schema.test.org/2004/07/System.Xml.Serialize"/>
<Conditions xmlns:a="http://schema.test.org/2004/07/Test.Soa.Vocab">
<a:Condition>
<a:xmlns i:nil="true" xmlns:b="http://schema.test.org/2004/07/System.Xml.Serialize"/>
<Identifier>a23aacaf-9b6b-424f-92bb-5ab71505e3bc</Identifier>
<Name>Code</Name>
<ParameterSelections/>
<ParameterSetCollections/>
<Parameters/>
<Summary i:nil="true"/>
<Instance>25486d6c-36ba-4ab2-9fa6-0dbafbcf0389</Instance>
<ConditionValue>
<ComplexValue i:nil="true"/>
<Text i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<Default>
<ComplexValue i:nil="true"/>
<Text xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<b:string>NULLCODE</b:string>
</Text>
</Default>
</ConditionValue>
<TypeCode>String</TypeCode>
</a:Condition>
<a:Condition>
<a:xmlns i:nil="true" xmlns:b="http://schema.test.org/2004/07/System.Xml.Serialize"/>
<Identifier>0af860f6-5611-4a23-96dc-eb3863975529</Identifier>
<Name>Content Type</Name>
<ParameterSelections/>
<ParameterSetCollections/>
<Parameters/>
<Summary i:nil="true"/>
<Instance>6364ec20-306a-4cab-aabc-8ec65c0903c9</Instance>
<ConditionValue>
<ComplexValue i:nil="true"/>
<Text i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<Default>
<ComplexValue i:nil="true"/>
<Text xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<b:string>Standard</b:string>
</Text>
</Default>
</ConditionValue>
<TypeCode>String</TypeCode>
</a:Condition>
</Conditions>
Il mio lavoro è quello di scambiare uno dei valori, mantenendo l'intera struttura della sorgente, e utilizzare questo per inviare un post successivo su nell'applicazione.
Il problema che sto avendo è che quando si salva in una stringa o in un file, si scombina totalmente gli spazi dei nomi:
<ns0:Context xmlns:ns0="http://Test.the.Sdk/2010/07" xmlns:ns1="http://schema.test.org/2004/07/Test.Soa.Vocab" xmlns:ns3="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:xmlns xsi:nil="true" />
<ns0:Conditions>
<ns1:Condition>
<ns1:xmlns xsi:nil="true" />
<ns0:Identifier>a23aacaf-9b6b-424f-92bb-5ab71505e3bc</ns0:Identifier>
<ns0:Name>Code</ns0:Name>
<ns0:ParameterSelections />
<ns0:ParameterSetCollections />
<ns0:Parameters />
<ns0:Summary xsi:nil="true" />
<ns0:Instance>25486d6c-36ba-4ab2-9fa6-0dbafbcf0389</ns0:Instance>
<ns0:ConditionValue>
<ns0:ComplexValue xsi:nil="true" />
<ns0:Text xsi:nil="true" />
<ns0:Default>
<ns0:ComplexValue xsi:nil="true" />
<ns0:Text>
<ns3:string>NULLCODE</ns3:string>
</ns0:Text>
</ns0:Default>
</ns0:ConditionValue>
<ns0:TypeCode>String</ns0:TypeCode>
</ns1:Condition>
<ns1:Condition>
<ns1:xmlns xsi:nil="true" />
<ns0:Identifier>0af860f6-5611-4a23-96dc-eb3863975529</ns0:Identifier>
<ns0:Name>Content Type</ns0:Name>
<ns0:ParameterSelections />
<ns0:ParameterSetCollections />
<ns0:Parameters />
<ns0:Summary xsi:nil="true" />
<ns0:Instance>6364ec20-306a-4cab-aabc-8ec65c0903c9</ns0:Instance>
<ns0:ConditionValue>
<ns0:ComplexValue xsi:nil="true" />
<ns0:Text xsi:nil="true" />
<ns0:Default>
<ns0:ComplexValue xsi:nil="true" />
<ns0:Text>
<ns3:string>Standard</ns3:string>
</ns0:Text>
</ns0:Default>
</ns0:ConditionValue>
<ns0:TypeCode>String</ns0:TypeCode>
</ns1:Condition>
</ns0:Conditions>
ho ristretto il codice fino alla forma più elementare e sto ancora ottenere gli stessi risultati, quindi non è nulla a che fare con il modo sto manipolando il file normalmente:
import xml.etree.ElementTree as ET
import requests
get_context_xml = 'http://localhost/testapi/returnxml' #returns first XML example above.
source_context_xml = requests.get(get_context_xml)
Tree = ET.fromstring(source_context_xml)
#Ensure the original namespaces are intact.
for Conditions in Tree.iter('{http://schema.test.org/2004/07/Test.Soa.Vocab}Condition'):
print "success"
with open('/home/memyself/output.xml','w') as f:
f.write(ET.tostring(Tree))
Hai taggato la domanda con "lxml". Hai provato? Penso che la maggior parte se non tutti i problemi andranno via se lo fai. lxml è simile a ElementTree, ma lascia da solo gli spazi dei nomi. – mzjn