-1

要求;

我必须发送独特的组作为高层的响应。

唯一性条件:

  1. 如果两个<partyfund>组进入一个<request>组,并且每个<partyfund>组具有相同的元素和子组,则删除重复的<partyfund>组。

  2. 如果一个<fund>组中有多个<partyfund>组,并且每个<fund>组具有相同的元素和子组,则删除重复<fund>组。

  3. 如果一个<fundcollection>组中有多个<fund>组,并且每个<fundcollection>组具有相同的元素和子组,则删除重复<fundcollection>组。

  4. 如果一个<valuewithcode>组中有多个<fundcollection>组,并且每个<valuewithcode>组具有相同的元素,则删除重复的组。

  5. 组内<fundcollection>假设如果<valuewithcode>出现 2 次,<valuecodeA>并且组<valuecodeB>之间<valuecodeC>的任何一个有差异,<valuewithcode>那么两者都<valuewithcode>应该作为输出发送。

  6. 为了唯一性,请确保它 <partyfundcode>及其关联组应该是唯一的。

<fundclass>并且<funddate>其关联组<fund>应该是唯一的。

<fundcode>并且它的关联组<fundcollection>应该是唯一的

<valuecodeA> <valuecodeB> <valuecodeC>并且它的关联组<valuewithcode>应该是唯一的

  1. 应计算重复节点的数量。

    我试图解释这个要求。我也在示例 xml 上尝试了下面的代码并得到了结果,但它没有在<fund>级别上对元素进行分组。请帮助解决这个问题。

输入 XML:

<request>
    <party>
        <partyfund>
            <partyfundcode>PFC</partyfundcode>
            <fund>
                <fundclass>1</fundclass>
                <funddate>2001-01-01</funddate>
                <fundcollection>
                    <fundcode>CFL</fundcode>
                    <valuewithcode>
                        <amount>10</amount>
                        <valuecodeA>200012DS</valuecodeA>
                        <valuecodeB>200011GH</valuecodeB>
                        <valuecodeC>200014UT</valuecodeC>
                    </valuewithcode>
                </fundcollection>
                <fundcollection>
                    <fundcode>CFL</fundcode>
                    <valuewithcode>
                        <amount>10</amount>
                        <valuecodeA>200012DS</valuecodeA>
                        <valuecodeB>200011GH</valuecodeB>
                        <valuecodeC>200014UT</valuecodeC>
                    </valuewithcode>
                </fundcollection>
            </fund>
            <fund>
                <fundclass>1</fundclass>
                <funddate>2001-01-01</funddate>
                <fundcollection>
                    <fundcode>CFL</fundcode>
                    <valuewithcode>
                        <amount>10</amount>
                        <valuecodeA>200012DS</valuecodeA>
                        <valuecodeB>200011GH</valuecodeB>
                        <valuecodeC>200014UT</valuecodeC>
                    </valuewithcode>
                </fundcollection>
                <fundcollection>
                    <fundcode>CFL</fundcode>
                    <valuewithcode>
                        <amount>10</amount>
                        <valuecodeA>200012DS</valuecodeA>
                        <valuecodeB>200011GH</valuecodeB>
                        <valuecodeC>200014UT</valuecodeC>
                    </valuewithcode>
                </fundcollection>
            </fund>
            <fund>
                <fundclass>1</fundclass>
                <funddate>2001-01-01</funddate>
                <fundcollection>
                    <fundcode>CFL</fundcode>
                    <valuewithcode>
                        <amount>10</amount>
                        <valuecodeA>44444</valuecodeA>
                        <valuecodeB>200011GH</valuecodeB>
                        <valuecodeC>200014UT</valuecodeC>
                    </valuewithcode>
                </fundcollection>
                <fundcollection>
                    <fundcode>CFL</fundcode>
                    <valuewithcode>
                        <amount>10</amount>
                        <valuecodeA>200012DS</valuecodeA>
                        <valuecodeB>200011GH</valuecodeB>
                        <valuecodeC>200014UT</valuecodeC>
                    </valuewithcode>
                </fundcollection>
                <fundcollection>
                    <fundcode>CFL</fundcode>
                    <valuewithcode>
                        <amount>10</amount>
                        <valuecodeA>200012DS</valuecodeA>
                        <valuecodeB>200011GH</valuecodeB>
                        <valuecodeC>200014UT</valuecodeC>
                    </valuewithcode>
                </fundcollection>
            </fund>
        </partyfund>
    </party>
</request>

我的代码的实际输出

    <request>
      <partyfund>
        <partyfundcode>PFC</partyfundcode>
        <fund>
          <fundclass>1</fundclass>
          <funddate>2001-01-01</funddate>
          <fundcollection>
            <fundcode>CFL</fundcode>
            <valuewithcode>
              <amount>40</amount>
              <valuecodeA>200012DS</valuecodeA>
              <valuecodeB>200011GH</valuecodeB>
              <valuecodeC>200014UT</valuecodeC>
            </valuewithcode>
          </fundcollection>
          <fundcollection>
            <fundcode>CFL</fundcode>
            <valuewithcode>
              <amount>10</amount>
              <valuecodeA>44444</valuecodeA>
              <valuecodeB>200011GH</valuecodeB>
              <valuecodeC>200014UT</valuecodeC>
            </valuewithcode>
          </fundcollection>
        </fund>
        <fund>
          <fundclass>1</fundclass>
          <funddate>2001-01-01</funddate>
        </fund>
        <fund>
          <fundclass>1</fundclass>
          <funddate>2001-01-01</funddate>
        </fund>
      </partyfund>
    </request>

预期结果应该是

<request>
    <partyfund>
        <partyfundcode>PFC</partyfundcode>
        <fund>
            <fundclass>1</fundclass>
            <funddate>2001-01-01</funddate>
            <fundcollection>
                <fundcode>CFL</fundcode>
                <valuewithcode>
                    <amount>40</amount>
                    <valuecodeA>200012DS</valuecodeA>
                    <valuecodeB>200011GH</valuecodeB>
                    <valuecodeC>200014UT</valuecodeC>
                </valuewithcode>
            </fundcollection>
        </fund>
        <fund>
            <fundclass>1</fundclass>
            <funddate>2001-01-01</funddate>
            <fundcollection>
                <fundcode>CFL</fundcode>
                <valuewithcode>
                    <amount>10</amount>
                    <valuecodeA>44444</valuecodeA>
                    <valuecodeB>200011GH</valuecodeB>
                    <valuecodeC>200014UT</valuecodeC>
                </valuewithcode>
            </fundcollection>
            <fundcollection>
                <fundcode>CFL</fundcode>
                <valuewithcode>
                    <amount>20</amount>
                    <valuecodeA>200012DS</valuecodeA>
                    <valuecodeB>200011GH</valuecodeB>
                    <valuecodeC>200014UT</valuecodeC>
                </valuewithcode>
            </fundcollection>
        </fund>
    </partyfund>
</request>

用于转换的代码

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

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="partyfund-check" match="partyfund" use="concat(partyfundcode,'+',fund)"/>
    <xsl:key name="fund-check" match="fund" use="concat(../partyfundcode,'+',funddate,'+',fundclass)"/>
    <xsl:key name="fundcollection-check" match="fundcollection" use="concat(../../partyfundcode,'+',../funddate,'+',../fundclass,'+',fundcode)"/>
    <xsl:key name="fundcollection-check1" match="fundcollection" use="concat(../../partyfundcode,'+',../funddate,'+',../fundclass,'+',fundcode,'+',.)"/>
    <xsl:key name="valuewithcode-check" match="valuewithcode" use="concat(../../../partyfundcode,'+',../../funddate,'+',../../fundclass,'+',../fundcode,'+',valuecodeA, '+', valuecodeB,'+',valuecodeC,'+',../../fundcollection)"/>


    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="party">

        <xsl:for-each select="partyfund[generate-id(.)=generate-id(key('partyfund-check',concat(partyfundcode,'+',fund))[1])]">
            <xsl:for-each select="key('partyfund-check',concat(partyfundcode,'+',fund))">
                <partyfund>
                    <xsl:copy-of select="partyfundcode"/>
                    <xsl:for-each select="fund[generate-id(.)=generate-id(key('fund-check',concat(../partyfundcode,'+',funddate,'+',fundclass))[1])]">
                        <xsl:for-each select="key('fund-check',concat(../partyfundcode,'+',funddate,'+',fundclass))">
                            <fund>
                                <xsl:copy-of select="fundclass"/>
                                <xsl:copy-of select="funddate"/>
                                <xsl:for-each select="fundcollection[generate-id(.)=generate-id(key('fundcollection-check',concat(../../partyfundcode,'+',../funddate,'+',../fundclass,'+',fundcode))[1])]">
                                    <xsl:for-each select="key('fundcollection-check',concat(../../partyfundcode,'+',../funddate,'+',../fundclass,'+',fundcode))[generate-id(.)=generate-id(key('fundcollection-check1',concat(../../partyfundcode,'+',../funddate,'+',../fundclass,'+',fundcode,'+',.))[1])]">
                                        <fundcollection>
                                            <xsl:copy-of select="fundcode"/>
                                            <xsl:for-each select="valuewithcode[generate-id(.)=generate-id(key('valuewithcode-check',concat(../../../partyfundcode,'+',../../funddate,'+',../../fundclass,'+',../fundcode,'+',valuecodeA, '+', valuecodeB,'+',valuecodeC,'+',../../fundcollection))[1])]">
                                                <xsl:for-each select="key('valuewithcode-check',concat(../../../partyfundcode,'+',../../funddate,'+',../../fundclass,'+',../fundcode,'+',valuecodeA, '+', valuecodeB,'+',valuecodeC,'+',../../fundcollection))[1]">
                                                    <valuewithcode>
                                                        <amount>
                                                            <xsl:value-of select="sum(key('valuewithcode-check',concat(../../../partyfundcode,'+',../../funddate,'+',../../fundclass,'+',../fundcode,'+',valuecodeA, '+', valuecodeB,'+',valuecodeC,'+',../../fundcollection))/amount)"/>
                                                        </amount>
                                                        <xsl:copy-of select="valuecodeA"/>
                                                        <xsl:copy-of select="valuecodeB"/>
                                                        <xsl:copy-of select="valuecodeC"/>
                                                    </valuewithcode>
                                                </xsl:for-each>
                                            </xsl:for-each>
                                        </fundcollection>
                                    </xsl:for-each>
                                </xsl:for-each>
                            </fund>
                        </xsl:for-each>
                    </xsl:for-each>
                </partyfund>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>


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

1 回答 1

1

考虑生成一个密钥并使用像这个例子这样的组

XSLT 中的分组

这将帮助您选择唯一值等。

于 2012-11-19T23:41:03.473 回答