'Deduplicate and merge memory XML

I have /credit-entity.xml (target)

<credit>
    <entity>
        <quality>Investment Grade</quality>
        <outlook>Positive</outlook>
        <LT-CRR>Aa3</LT-CRR>
        <UUID>207</UUID>
        <issuer>AA Bank NV</issuer>
        <segment>Financial Institutions</segment>
        <sector>Bank</sector>       
    </entity>
    <entity>
        <quality>Investment Grade</quality>
        <outlook>Stable</outlook>
        <LT-CRR>Aa3</LT-CRR>
        <UUID>203</UUID>
        <issuer>GS Bank Europe SE</issuer>
        <segment>Financial Institutions</segment>
        <sector>Securities &amp; Exchanges</sector>
        <date>2022-02-17</date>
        <rating>A2</rating>
    </entity>
    <entity>
        <quality>Investment Grade</quality>
        <outlook>Stable</outlook>
        <ST>(P)P-2</ST>
        <UUID>118</UUID>
        <issuer>RMGS Services GmbH</issuer>
        <segment>Corporates</segment>
        <sector>Pharmaceuticals</sector>
        <date>2021-12-22</date>
        <rating>A2</rating>
    </entity>       
</credit>

/credit-rating.xml (source)

<ratings>
    <rating>
        <UUID>207</UUID>
        <issuer>AA Bank NV</issuer>
        <date>2022-02-07</date>
        <rating>Aa3</rating>
    </rating>
    <rating>
        <UUID>203</UUID>
        <issuer>GS Bank Europe SE</issuer>
        <date>2022-01-31</date>
        <rating>A1</rating>
    </rating>
    <rating>
        <LT>A2</LT>
        <LT-Type>LT Counterparty Risk Rating - Fgn Curr</LT-Type>
        <UUID>233</UUID>
        <issuer>AHR AG - Public sector Pfandbriefe</issuer>
        <segment>Financial Institutions</segment>
        <sector>Covered Bonds</sector>
        <date>2022-01-25</date>
        <rating>A3</rating>
    </rating>
    <rating>
        <UUID>118</UUID>
        <issuer>RMGS Services GmbH</issuer>
        <date>2022-02-24</date>
        <rating>A1</rating>
    </rating>           
</ratings>

My xsl is to:

-) match /ratings/rating/UUID with /credit/entity/UUID. If there is matched UUID, compare and get the latest date, create a new element latest with the UUID’s latest date and rating.

-) If no matched UUID in /credit-rating.xml, move its date and rating to form the new element latest.

-) If UUID is in source /credit-rating.xml but missing in target /credit-entity.xml, e.g. UUID 233. Then copy /ratings/rating to target and create new element latest with its date and rating.

Expected output:

<credit>
   <entity>
      <quality>Investment Grade</quality>
      <outlook>Positive</outlook>
      <LT-CRR>Aa3</LT-CRR>
      <UUID>207</UUID>
      <issuer>AA Bank NV</issuer>
      <segment>Financial Institutions</segment>
      <sector>Bank</sector>
      <latest>
         <date>2022-02-07</date>
         <rating>Aa3</rating>
      </latest>
   </entity>
   <entity>
      <quality>Investment Grade</quality>
      <outlook>Stable</outlook>
      <LT-CRR>Aa3</LT-CRR>
      <UUID>203</UUID>
      <issuer>GS Bank Europe SE</issuer>
      <segment>Financial Institutions</segment>
      <sector>Securities & Exchanges</sector>
      <latest>
         <date>2022-02-17</date>
         <rating>A2</rating>
      </latest>
   </entity>
   <entity>
      <quality>Investment Grade</quality>
      <outlook>Stable</outlook>
      <ST>(P)P-2</ST>
      <UUID>118</UUID>
      <issuer>RMGS Services GmbH</issuer>
      <segment>Corporates</segment>
      <sector>Pharmaceuticals</sector>
      <latest>
         <date>2022-02-24</date>
         <rating>A1</rating>
      </latest>
   </entity>
   <entity>
      <LT>A2</LT>
      <LT-Type>LT Counterparty Risk Rating - Fgn Curr</LT-Type>
      <UUID>233</UUID>
      <issuer>AHR AG - Public sector Pfandbriefe</issuer>
      <segment>Financial Institutions</segment>
      <sector>Covered Bonds</sector>
      <latest>
         <date>2022-01-25</date>
         <rating>A3</rating>
      </latest>
   </entity>
</credit>

But somehow my code didn’t work. Can anyone help to fix the issue?

    <xsl:variable name="ratingEntity" select="doc('/credit-rating.xml')/ratings/rating"/>
    <xsl:variable name="ID">
        <xsl:sequence select="credit/entity/UUID"/>
    </xsl:variable>   
    <xsl:template match="credit">
        <xsl:copy>
            <xsl:for-each select="entity">
                <xsl:variable name="uuid" select="UUID"/>
                <xsl:copy>
                    <xsl:choose>
                        <xsl:when test="not(exists(rating)) or date le $ratingEntity[UUID eq $uuid]/date" >
                            <xsl:copy-of select="./* except (date, rating)"/>
                            <latest>
                                <xsl:call-template name="latest">
                                  <xsl:with-param name="ratingEntity" select="$ratingEntity[UUID eq $uuid]" />
                                </xsl:call-template>                             
                            </latest>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:copy-of select="./* except (date, rating)"/>
                            <latest>
                                <xsl:copy-of select="."/>
                            </latest>                       
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:copy>
            </xsl:for-each>
            <xsl:call-template name="missing">
                <xsl:with-param name="ratingEntity" select="$ratingEntity[$ID ne UUID]" />
        </xsl:copy>
    </xsl:template>   
   
    <xsl:template name="missing">
       <xsl:param name="ratingEntity"/>
        <entity>
          <xsl:copy-of select="$ratingEntity"/>  
        </entity>
    </xsl:template>
   
    <xsl:template name="latest">
      <xsl:param name="ratingEntity"/>
        <xsl:copy-of select="$ratingEntity except (UUID, issuer)"/>     
    </xsl:template>
   


Solution 1:[1]

I think you simply want

<xsl:template match="credit">
  <xsl:copy>
    <xsl:for-each-group select="entity, doc('/credit-rating.xml')/ratings/rating" group-by="UUID">
      <entity>
        <xsl:apply-templates select="* except (date, rating)"/>
        <lastest>
          <xsl:variable name="max-date" select="max(current-group()/xs:date(date))"/>
          <date>{$max-date}</date>
          <rating>{current-group()[date = $max-date]/rating}</rating>
        </lastest>
      </entity>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Honnen