How To Force Elementtree To Keep Xmlns Attribute Within Its Original Element?
Solution 1:
xml.etree.ElementTree
pulls all namespaces into the first element as it internally doesn't track on which element the namespace was declared originally.
If you don't want that, you'll have to write your own serialisation logic.
The better alternative would be to use lxml
instead of xml.etree
, because it preserves the location where a namespace prefix is declared.
Solution 2:
Following @mata advice, here I give an answer with an example with code and xml file attached.
The xml input is as shown in the picture (original and modified)
The python codes check the NtnlCcy Name and if it is "EUR", convert the Price to USD (by multiplying EURUSD: = 1.2) and change the NtnlCcy Name to "USD".
The python code is as follows:
from lxml import etree
pathToXMLfile = r"C:\Xiang\codes\Python\afmreports\test_original.xml"
tree = etree.parse(pathToXMLfile)
root = tree.getroot()
EURUSD = 1.2for Rchild in root:
print ("Root child: ", Rchild.tag, ". \n")
if Rchild.tag.endswith("Pyld"):
for PyldChild in Rchild:
print ("Pyld Child: ", PyldChild.tag, ". \n")
Doc = Rchild.find('{001.003}Document')
FinInstrNodes = Doc.findall('{001.003}FinInstr')
for FinInstrNode in FinInstrNodes:
FinCcyNode = FinInstrNode.find('{001.003}NtnlCcy')
FinPriceNode = FinInstrNode.find('{001.003}Price')
FinCcyNodeText = ""if FinCcyNode isnotNone:
CcyNodeText = FinCcyNode.text
if CcyNodeText == "EUR":
PriceText = FinPriceNode.text
Price = float(PriceText)
FinPriceNode.text = str(Price * EURUSD)
FinCcyNode.text = "USD"
tree.write(r"C:\Xiang\codes\Python\afmreports\test_modified.xml", encoding="utf-8", xml_declaration=True)
print("\n the program runs to the end! \n")
As we compare the original and modified xml files, the namespace remains unchanged, the whole structure of the xml remains unchanged, only some NtnlCcy and Price Nodes have been changed, as desired.
The only minor difference we do not want is the first line. In the original xml file, it is <?xml version="1.0" encoding="UTF-8"?>
, while in the modified xml file, it is <?xml version='1.0' encoding='UTF-8'?>
. The quotation sign changes from double quotation to single quotation. But we think this minor difference should not matter.
The original file context will be attached for your easy test:
<?xml version="1.0" encoding="UTF-8"?><BizDataxmlns="001.001"><Hdr><AppHdrxmlns="001.002"><Fr><Id>XXX01</Id></Fr><To><Id>XXX02</Id></To><CreDt>2019-10-25T15:38:30</CreDt></AppHdr></Hdr><Pyld><Documentxmlns="001.003"><FinInstr><Id>NLENX240</Id><FullNm>AO.AAI</FullNm><NtnlCcy>EUR</NtnlCcy><Price>9</Price></FinInstr><FinInstr><Id>NLENX681</Id><FullNm>AO.ABN</FullNm><NtnlCcy>USD</NtnlCcy><Price>10</Price></FinInstr><FinInstr><Id>NLENX320</Id><FullNm>AO.ING</FullNm><NtnlCcy>EUR</NtnlCcy><Price>11</Price></FinInstr></Document></Pyld>
Post a Comment for "How To Force Elementtree To Keep Xmlns Attribute Within Its Original Element?"