XSLT 按唯一参数分组

XSLT Group by unique parameter

我正在尝试使用基于 RAPID_ID

的 for-each-group 逻辑的 xslt 将输入 xml 值转换为输出 xml

Input.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
    <Output>
        <ID>1234</ID>
        <CustomerName>KUMAR</CustomerName>
        <BranchName>HARBOUR</BranchName>
        <SchemeName>GOLD</SchemeName>
        <MobileNumber>123456789</MobileNumber>
        <CustomerType>PRIMARY</CustomerType>
        <DedupeFound>NO</DedupeFound>
    </Output>
    <Output>
        <ID>1234</ID>
        <CustomerName>SEAN</CustomerName>
        <BranchName>HARBOUR</BranchName>
        <SchemeName>GOLD</SchemeName>
        <MobileNumber>123456789</MobileNumber>
        <CustomerType>SECONDARY</CustomerType>
        <DedupeFound>YES</DedupeFound>
    </Output>
    <Output>
        <ID>5678</ID>
        <CustomerName>MARK</CustomerName>
        <BranchName>CANTONMENT</BranchName>
        <SchemeName>DIAMOND</SchemeName>
        <MobileNumber>123456789</MobileNumber>
        <CustomerType>PRIMARY</CustomerType>
        <DedupeFound>NO</DedupeFound>
    </Output>
    <Output>
        <ID>5678</ID>
        <CustomerName>STEVE</CustomerName>
        <BranchName>CANTONMENT</BranchName>
        <SchemeName>DIAMOND</SchemeName>
        <MobileNumber>123456789</MobileNumber>
        <CustomerType>SECONDARY</CustomerType>
        <DedupeFound>YES</DedupeFound>
    </Output>
</Response>

我的预期输出是

Output.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
    <Output>
        <ID>1234</ID>
        <CustomerName>KUMAR</CustomerName>
        <BranchName>HARBOUR</BranchName>
        <SchemeName>GOLD</SchemeName>
        <MobileNumber>123456789</MobileNumber>
        <DedupeDetails>
            <CustomerType>PRIMARY</CustomerType>
            <CustomerName>KUMAR</CustomerName>
            <DedupeFound>NO</DedupeFound>
        </DedupeDetails>
        <DedupeDetails>
            <CustomerType>SECONDARY</CustomerType>
            <CustomerName>SEAN</CustomerName>
            <DedupeFound>YES</DedupeFound>
        </DedupeDetails>
    </Output>
    <Output>
        <ID>5678</ID>
        <CustomerName>MARK</CustomerName>
        <BranchName>CANTONMENT</BranchName>
        <SchemeName>DIAMOND</SchemeName>
        <MobileNumber>123456789</MobileNumber>
        <DedupeDetails>
            <CustomerType>PRIMARY</CustomerType>
            <CustomerName>MARK</CustomerName>
            <DedupeFound>NO</DedupeFound>
        </DedupeDetails>
        <DedupeDetails>
            <CustomerType>SECONDARY</CustomerType>
            <CustomerName>STEVE</CustomerName>
            <DedupeFound>YES</DedupeFound>
        </DedupeDetails>
    </Output>
</Response>

我从类似的事情开始,但无法继续进行。 我试图首先对 ID 参数进行分组,在其中以主要客户详细信息开始。 在主要客户详细信息之后,我必须迭代每个客户(这里的主要和次要客户)

实现此目标的任何建议/更正。

我的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <Response>
     <xsl:for-each-group select="/Response/Output" group-by="ID">
      <xsl:sort select="ID"/>


    </xsl:for-each-group>
            
        </Response>
    </xsl:template>

</xsl:stylesheet>

我会采取不同的方法:

XSLT 1.0

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

<xsl:key name="op" match="Output" use="ID"/>

<xsl:template match="/Response">
    <xsl:copy>
        <xsl:for-each select="Output[CustomerType='PRIMARY']">
            <xsl:copy>
                <xsl:copy-of select="ID|CustomerName|BranchName|SchemeName"/>
                <xsl:for-each select="key('op', ID)">
                    <DedupeDetails>
                        <xsl:copy-of select="CustomerType|CustomerName|DedupeFound"/>
                    </DedupeDetails>
                </xsl:for-each>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

请注意,这假设每个组只有一个主要客户和零个或多个次要客户。如果输入总是有一个主要客户,其次是一个次要客户,如您的示例所示,那么这可能会更简单。