重复数据删除和合并内存 XML

Deduplicate and merge memory XML

我有/credit-entity.xml(目标)

<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(来源)

<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>

我的 xsl 是:

-) 将 /ratings/rating/UUID 与 /credit/entity/UUID 匹配。如果有匹配的UUID,比较得到最新的date,用UUID最新的daterating.

创建一个新元素latest

-) 如果在 /credit-rating.xml 中没有匹配的 UUID,移动其 daterating 以形成新元素 latest.

-) 如果 UUID 在源 /credit-rating.xml 中但在目标 /credit-entity.xml 中丢失,例如UUID 233。然后将 /ratings/rating 复制到目标并创建新元素 latest 及其 daterating.

预期输出:

<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>

但不知何故我的代码不起作用。谁能帮忙解决这个问题?

    <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>
   

我想你只是想要

<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>