Almoral
Almoral

Reputation: 1

XML - Merging child elements of multiple occurrences of the same node using XSLT

I have the following source file which is being sent by an external system.

<?xml version="1.0" encoding="utf-8"?>
<root>
  <remoteId>id-sent-by-sender</remoteId>
  <payments>
    <payment>
      <amount>383700</amount>
      <remoteId>unique-id-1</remoteId>
      <beneficiary>
        <accountNumber>CustomerAccount_1</accountNumber>
        <country>fr</country>
        <routingNumber>AAABBBBCCC</routingNumber>
        <title>Dale's Shop</title>
        <email>[email protected]</email>
      </beneficiary>
      <feeBearer>
        <party>supplier</party>
        <supplierFeePercent>1.00</supplierFeePercent>
      </feeBearer>
      <currency>eur</currency>
      <dateDue>2021-07-21</dateDue>
      <metaData>
        <data>
          <key>a-key</key>
          <value>a value</value>
        </data>
        <data>
          <key>another-key</key>
          <value>another value</value>
        </data>
      </metaData>
      <reference>
        <row>invoice row 1</row>
      </reference>
      <reference>
        <row>invoice row 2</row>
      </reference>
      <reference>
        <row>invoice row 3</row>
      </reference>
    </payment>
    <payment>
      <amount>100000</amount>
      <remoteId>unique-id-2</remoteId>
      <beneficiary>
        <accountNumber>CustomerAccount_2</accountNumber>
        <country>gb</country>
        <routingNumber>BBBKKKKSSS</routingNumber>
        <title>Ramos Tacos</title>
      </beneficiary>
      <feeBearer>
        <party>supplier</party>
        <supplierFeePercent>1.00</supplierFeePercent>
      </feeBearer>
      <currency>eur</currency>
      <reference>
        <row>invoice row 1</row>
      </reference>
      <reference>
        <row>invoice row 2</row>
      </reference>
      <reference>
        <row>invoice row 3</row>
      </reference>
    </payment>
  </payments>
</root>

I need the file to be changed to something like below.

<?xml version="1.0" encoding="utf-8"?>
<root>
  <remoteId>id-send-by-sender</remoteId>
  <payments>
    <payment>
      <amount>383700</amount>
      <remoteId>unique-id-1</remoteId>
      <beneficiary>
        <accountNumber>CustomerAccount_1</accountNumber>
        <country>fr</country>
        <routingNumber>AAABBBBCCC</routingNumber>
        <title>Dale's Shop</title>
        <email>[email protected]</email>
      </beneficiary>
      <feeBearer>
        <party>supplier</party>
        <supplierFeePercent>1.00</supplierFeePercent>
      </feeBearer>
      <currency>eur</currency>
      <dateDue>2021-07-21</dateDue>
      <metaData>
        <data>
          <key>a-key</key>
          <value>a value</value>
        </data>
        <data>
          <key>another-key</key>
          <value>another value</value>
        </data>
      </metaData>
      <reference>
        <row>invoice row 1</row>
        <row>invoice row 2</row>
        <row>invoice row 3</row>
      </reference>
    </payment>
    <payment>
      <amount>100000</amount>
      <beneficiary>
        <accountNumber>CustomerAccount_2</accountNumber>
        <country>gb</country>
        <routingNumber>BBBKKKKSSS</routingNumber>
        <title>Ramos Tacos</title>
      </beneficiary>
      <feeBearer>
        <party>supplier</party>
        <supplierFeePercent>1.00</supplierFeePercent>
      </feeBearer>
      <currency>eur</currency>
      <reference>
        <row>invoice row 1</row>
        <row>invoice row 2</row>
        <row>invoice row 3</row>
      </reference>
      <remoteId>unique-id-2</remoteId>
    </payment>
  </payments>
</root>

In short the difference between the 2 XMLs is that the first one has more than one occurrence of the element <reference> which I need to merge into a single occurrence while keeping the child elements for all those occurrences intact.

Could this be done using XSLT? If yes, could someone please help? I am quite novice in this and while I searched through the posts, don't understand how to go about it.

Thanks

Upvotes: 0

Views: 58

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

I would expect to see a stylesheet containing the standard identity template, plus the rules:

<xsl:template match="payment">
  <xsl:copy>
    <xsl:apply-templates/>
    <reference>
      <xsl:copy-of select="reference/row"/>
    </reference>
  </xsl:copy>
</xsl:template>

<xsl:template match="reference"/>

Upvotes: 2

Related Questions