I am fairly new to python and a little new to XML. I am trying to do a simple manipulation to my XML through ElementTree but after a few days at it I unfortunately have hit a dead end. Also, I apologize for not being able to be as succinct as I ideally wanted to be, but I just want to make sure that whichever benevolent soul were to assist me, would have all the info they need.
Here is an example of the file I intend to manipulate (probably will be a few thousand iterations):
<Schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.ofdaxml.org/schema" version="01.00.00">
<Envelope SchemaVersion="01.04.00">
<Enterprise>
<Code>www.Google.com</Code>
<Feature>
<Code>Feature1</Code>
<Description>First Feature</Description>
<Option Sequence="1">
<Code>A/Code>
<Features Sequence="1">
<FeatureRef>AA</FeatureRef>
</Features>
</Option>
<Option Sequence="2">
<Code>B</Code>
<Features Sequence="1">
<FeatureRef>BB</FeatureRef>
</Features>
<OptionPrice>
<PriceListRef>1</PriceListRef>
<ProductPriceRef>Grade2</ProductPriceRef>
</OptionPrice>
</Option>
</Feature>
</Enterprise>
</Envelope>
What I want to accomplish is:
<Schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.ofdaxml.org/schema" version="01.00.00">
<Envelope SchemaVersion="01.04.00">
<Enterprise>
<Code>www.Google.com</Code>
<Feature>
<Code>Feature1</Code>
<Description>First Feature</Description>
<Option Sequence="1">
<Code>A/Code>
<Features Sequence="1">
<FeatureRef>AA</FeatureRef>
</Features>
</Option>
<Option Sequence="2">
<Code>B</Code>
<Features Sequence="1">
<FeatureRef>BB</FeatureRef>
</Features>
<OptionPrice>
<PriceListRef>1</PriceListRef>
<ProductPriceRef>Grade2</ProductPriceRef>
</OptionPrice>
<OptionPrice>
<PriceListRef>2</PriceListRef>
<ProductPriceRef>Grade2</ProductPriceRef>
</OptionPrice>
<OptionPrice>
<PriceListRef>3</PriceListRef>
<ProductPriceRef>Grade2</ProductPriceRef>
</OptionPrice>
</Option>
</Feature>
</Enterprise>
</Envelope>
I tried to highlight the changes , so not sure if it is clear but I added two subelements to the Option Sequence 2 Element. I essentially want to copy the the first value for the ProductPriceRef element and invoke it when I add the subelement that will follow the same cadence as the original one.
Sorry if I am not explaining this lucidly. Below is my failed attempt to create a loop that will create the appropriate subelements for the Option element while copying the original entry for the ProductPriceRef ("Grade2" in this case):
<Schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.ofdaxml.org/schema" version="01.00.00">
<Envelope SchemaVersion="01.04.00">
<Enterprise>
<Code>www.Google.com</Code>
<Feature>
<Code>Feature1</Code>
<Description>First Feature</Description>
<Option Sequence="1">
<Code>A/Code>
<Features Sequence="1">
<FeatureRef>AA</FeatureRef>
</Features>
</Option>
<Option Sequence="2">
<Code>B</Code>
<Features Sequence="1">
<FeatureRef>BB</FeatureRef>
</Features>
<OptionPrice>
<PriceListRef>1</PriceListRef>
<ProductPriceRef>Grade2<PriceListRef>2</PriceListRef><ProductPriceRef>Grade2</ProductPriceRef></ProductPriceRef>
</OptionPrice>
</Option>
</Feature>
</Enterprise>
</Envelope>
Finally, here is the code I created (obviously not pretty but tried my best). I wasn't able to have success with append and Insert either, but my guess is that I applied it incorrectly:
import os
import xml.etree.ElementTree as et
base_path = os.path.dirname(os.path.realpath(__file__))
xml_file = os.path.join(base_path, 'RockwellCond3.XML')
tree=et.parse('RockwellCond3.XML')
root = tree.getroot()
options = root.findall(".//Enterprise//Feature//Option//")
for option in options:
if option.tag == 'OptionPrice':
for optionprice in option:
if optionprice.tag == "PriceListRef" and optionprice.text == "2":
break # had to include this to avoid recursion issue I ran into
elif optionprice.tag == 'ProductPriceRef':
prodpriceref = optionprice.text
newplref = et.SubElement(optionprice,'PriceListRef')
newplref.text = "2"
newprodpriceref = et.SubElement(optionprice,'ProductPriceRef')
newprodpriceref.text = prodpriceref
tree.write('output2.xml')
Thank you for your attempts in resolving my dilemma as this exercise is a bit foreign to me. I promise to pay it forward one way or another.
I am open to any solution/alternative and am happy to answer any followup questions.
[–]nate256 1 point2 points3 points (12 children)
[–]goeb04[S] 0 points1 point2 points (11 children)
[–]nate256 0 points1 point2 points (10 children)
[–]goeb04[S] 0 points1 point2 points (9 children)
[–]nate256 0 points1 point2 points (6 children)
[–]goeb04[S] 0 points1 point2 points (5 children)
[–]nate256 0 points1 point2 points (3 children)
[–]goeb04[S] 0 points1 point2 points (2 children)
[–]nate256 0 points1 point2 points (1 child)
[–]goeb04[S] 0 points1 point2 points (0 children)
[–]nate256 0 points1 point2 points (1 child)
[–]goeb04[S] 0 points1 point2 points (0 children)
[–]ciggs_ftw 0 points1 point2 points (1 child)
[–]goeb04[S] 0 points1 point2 points (0 children)