I have an XML file with multiple Shape
elements each with a child Material
element that contains a Code
attribute. I want to update the Code
attribute for each Material
element based on a value that is obtained from a separate XML file. The problem I have is that the update source file also have multiple elements.
Here is the main XML file - main.xml:
<?xml version="1.0"?>
<!--Top-->
<Top>
<Shapes>
<!--Shape-->
<Shape Code="Penisola" Description="Rectangle">
<!--Material-->
<Material Code="MAT01000257" ></Material>
</Shape>
<!--Shape-->
<Shape Code="Penisola" Description="Rectangle">
<!--Material-->
<Material Code="MAT01000258" ></Material>
</Shape>
</Shapes>
<Texts></Texts>
</Top>
...and my update source file - updatesource.xml:
<?xml version="1.0"?>
<Job>
<Job_Number>B90512</Job_Number>
<Job_Address>2nd Floor/ 28-32 Albert Road VIC 3205</Job_Address>
<Benchtops>
<Benchtop>
<Material>Material 1</Material>
</Benchtop>
<Benchtop>
<Material>Material 2</Material>
</Benchtop>
</Benchtops>
</Job>
The desired outcome is - output.xml:
<?xml version="1.0"?>
<!--Top-->
<Top>
<Shapes>
<!--Shape-->
<Shape Code="Penisola" Description="Rectangle">
<!--Material-->
<Material Code="Material 1" ></Material>
</Shape>
<!--Shape-->
<Shape Code="Penisola" Description="Rectangle">
<!--Material-->
<Material Code="Material 2" ></Material>
</Shape>
</Shapes>
<Texts></Texts>
</Top>
I imagine I need to iterate through both the main XML and the update source XML, but I am unsure how to do this. My attempts so far have only succeeded in updating both Code
attributes with the value from the first Material
element in the update source file.
Some extra information; both the main.xml file and the updatesource.xml file will have matching number of Shape
and Benchtop
elements respectively, however, the number of each will change with each job I process, so the solution will need to dynamically work through the number of elements present.