Firstly, I suck at XSLT. Secondly, I know there are several articles on how to perform merges with XSLT, but I didn’t find anything on my particular challenge.
I have 2 XML files. One is new Customer Information and the other is the Current/Previous information below. I need the resultant XML to merge all of the Customer/Addresses and add attributes (NoChange, Updated, Deleted, New) to the attribute of the final XML:
Input 1
Current Customer Information.
<Customer>
<CustId>1</CustId>
<CustName>Acme</CustName>
<Addresses>
<Address>
<AddressesId>1</AddressesId>
<Street>123 Main</Street>
</Address>
<Address>
<AddressesId>2</AddressesId>
<Street>345 Main</Street</Street>
</Address>
<Address>
<AddressesId>4</AddressesId>
<Street>888 Goner St.</Street>
</Address>
</Addresses>
</Customer>
Input 2
Updates Information.
<Customer>
<CustId>1</CustId>
<CustName>Acme</CustName>
<Addresses>
<Address>
<AddressesId>2</AddressesId>
<Street>999 Updated St.</Street>
</Address>
<Address>
<AddressesId>3</AddressesId>
<Street>3999 New St.</Street>
</Address>
</Addresses>
</Customer>
Result
<Customer>
<CustId>1</CustId>
<CustName>Acme</CustName>
<Addresses>
<Address>
<Address status="NoChange">
<AddressesId>1</AddressesId>
<Street>123 Main</Street>
</Address>
<Address>
<Address status="Updated">
<AddressesId>2</AddressesId>
<Street>999 Updated St.</Street>
</Address>
<Address status="New">
<AddressesId>3</AddressesId>
<Street>3999 New St.</Street>
</Address>
<Address status="Deleted">
<AddressesId>4</AddressesId>
<Street>888 Goner St.</Street>
</Address>
</Addresses>
</Customer>
How can I do the merge I want?
I. This XSLT 1.0 transformation (a corresponding XSLT 2.0 solution is shorter and easier):
when applied on the provided XML document:
and having at
c:/temp/delete/CustomersUpdates.xmlthis XML document (slightly changed from the provided in order to have the first address end up with status “NoChange”):produces the wanted, correct result:
II. XSLT 2.0 solution:
when this transformation is applied on the same documents, the same correct result is produced.