Fusionner les nœuds XML en fonction de la valeur ReferenceNumber

Comment fusionner la demande XML avec la sortie donnée ci-dessous à l'aide de XSLT en fonction de la valeur Key. Fondamentalement, la réponse XML doit être fusionnée pour ReferenceNumber, avec les ProductCode et SecondaryDivision respectifs. Vous trouverez ci-dessous la demande et la réponse attendue.

J'ai essayé d'utiliser XSLT suivant mais cela n'a pas donné le résultat souhaité.

Ci-dessous se trouve le XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="refNo" match="Envelope/Body/DocumentMetadataResponse" use="ReferenceNumber" />
    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Envelope/Body/DocumentMetadataResponse/DocumentMetadata" />
    <xsl:template match="Envelope/Body/DocumentMetadataResponse/DocumentMetadata[generate-id()=generate-id(key('refNo', DocumentMetadata/ReferenceNumber))]">
        <xsl:call-template name="identity"/>
    </xsl:template>
    <xsl:template match="DocumentMetadata">
        <xsl:copy>
            <xsl:apply-templates select="@*|ReferenceNumber" />
            <divisionses>
                <xsl:apply-templates select="key('refNo', ReferenceNumber)/DocumentMetadata" mode="divisions" />
            </divisionses>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="DocumentMetadata" mode="divisions">
        <Divisions>
            <xsl:apply-templates select="*[not(self::ReferenceNumber)]" />
        </Divisions>
    </xsl:template>
</xsl:stylesheet>

XML d'entrée

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:DocumentMetadataResponse xmlns:ns2="http://www.example.com/mService">
         <PageNumber>1</PageNumber>
         <PageSize>1000</PageSize>
         <TotalDocumentsAvailable>4919</TotalDocumentsAvailable>
         <DocumentMetadata>
            <ReferenceNumber>Ref-01</ReferenceNumber>
            <SecondaryDivision>AT</SecondaryDivision>
            <ProductCode>Product-AT-01</ProductCode>
         </DocumentMetadata>
         <DocumentMetadata>
            <ReferenceNumber>Ref-01</ReferenceNumber>
            <SecondaryDivision>AT</SecondaryDivision>
            <ProductCode>Product-AT-02</ProductCode>
         </DocumentMetadata>
         <DocumentMetadata>
            <ReferenceNumber>Ref-01</ReferenceNumber>
            <SecondaryDivision>BE</SecondaryDivision>
            <ProductCode>Product-BE-01</ProductCode>
         </DocumentMetadata>
         <DocumentMetadata>
            <ReferenceNumber>Ref-02</ReferenceNumber>
            <SecondaryDivision>BS</SecondaryDivision>
            <ProductCode>Product-BS-01</ProductCode>
         </DocumentMetadata>
      </ns2:DocumentMetadataResponse>
   </soap:Body>
</soap:Envelope>

XML de sortie attendu

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:DocumentMetadataResponse xmlns:ns2="http://www.example.com/mService">
         <PageNumber>1</PageNumber>
         <PageSize>1000</PageSize>
         <TotalDocumentsAvailable>4919</TotalDocumentsAvailable>
         <DocumentMetadata>
            <ReferenceNumber>Ref-01</ReferenceNumber>
            <Divisions>
               <Division>
                  <SecondaryDivision>AT</SecondaryDivision>
                  <Products>
                     <Product>
                        <ProductCode>Product-AT-01</ProductCode>
                     </Product>
                     <Product>
                        <ProductCode>Product-AT-02</ProductCode>
                     </Product>
                  </Products>
               </Division>
               <Division>
                  <SecondaryDivision>BE</SecondaryDivision>
                  <Products>
                     <Product>
                        <ProductCode>Product-BE-01</ProductCode>
                     </Product>
                  </Products>
               </Division>
            </Divisions>
         </DocumentMetadata>
         <DocumentMetadata>
            <ReferenceNumber>Ref-02</ReferenceNumber>
            <Divisions>
               <Division>
                  <SecondaryDivision>BS</SecondaryDivision>
                  <Products>
                     <Product>
                        <ProductCode>Product-BS-01</ProductCode>
                     </Product>
                  </Products>
               </Division>
            </Divisions>
         </DocumentMetadata>
      </ns2:DocumentMetadataResponse>
   </soap:Body>
</soap:Envelope>
0
Vinu 27 sept. 2019 à 21:56

1 réponse

Meilleure réponse

Vous pouvez utiliser ce modèle XSLT-2.0 pour faire le travail. xsl:for-each-group effectue les deux niveaux de regroupement :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns2="http://www.example.com/mService">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ns2:DocumentMetadataResponse ">
        <xsl:copy>
            <xsl:apply-templates select="@*|* except DocumentMetadata" />
            <xsl:for-each-group select="DocumentMetadata" group-by="ReferenceNumber">
                <DocumentMetadata>
                    <ReferenceNumber><xsl:value-of select="current-grouping-key()" /></ReferenceNumber>
                    <Divisions>
                        <xsl:for-each-group select="current-group()" group-by="SecondaryDivision">
                            <Division>
                                <SecondaryDivision><xsl:value-of select="current-grouping-key()" /></SecondaryDivision>
                                <Products>
                                    <xsl:for-each select="current-group()">
                                        <Product>
                                            <ProductCode><xsl:value-of select="ProductCode" /></ProductCode>
                                        </Product>
                                    </xsl:for-each>
                                </Products>
                            </Division>
                        </xsl:for-each-group>
                    </Divisions>
                </DocumentMetadata>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Le résultat est:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:DocumentMetadataResponse xmlns:ns2="http://www.example.com/mService">
         <PageNumber>1</PageNumber>
         <PageSize>1000</PageSize>
         <TotalDocumentsAvailable>4919</TotalDocumentsAvailable>
         <DocumentMetadata>
            <ReferenceNumber>Ref-01</ReferenceNumber>
            <Divisions>
               <Division>
                  <SecondaryDivision>AT</SecondaryDivision>
                  <Products>
                     <Product>
                        <ProductCode>Product-AT-01</ProductCode>
                     </Product>
                     <Product>
                        <ProductCode>Product-AT-02</ProductCode>
                     </Product>
                  </Products>
               </Division>
               <Division>
                  <SecondaryDivision>BE</SecondaryDivision>
                  <Products>
                     <Product>
                        <ProductCode>Product-BE-01</ProductCode>
                     </Product>
                  </Products>
               </Division>
            </Divisions>
         </DocumentMetadata>
         <DocumentMetadata>
            <ReferenceNumber>Ref-02</ReferenceNumber>
            <Divisions>
               <Division>
                  <SecondaryDivision>BS</SecondaryDivision>
                  <Products>
                     <Product>
                        <ProductCode>Product-BS-01</ProductCode>
                     </Product>
                  </Products>
               </Division>
            </Divisions>
         </DocumentMetadata>
      </ns2:DocumentMetadataResponse>
   </soap:Body>
</soap:Envelope>
0
zx485 27 sept. 2019 à 19:21