Как преобразовать запрос xml в вывод, приведенный ниже, используя XSLT.В основном, xml-ответ должен быть объединен для ReferenceNumber вместе с соответствующим ProductCode и SecondaryDivision. Ниже приведен запрос и ожидаемый ответ.
Я пытался использовать XSLT, но не смог выполнить цикл для ReferenceNumber
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="byVendor" 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('byVendor', 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('byVendor', 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>
Ввод -
<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>
Ожидаемый вывод
<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>