使用xsl对重复的xml数据进行分组

时间:2015-01-21 17:15:59

标签: xml xslt-1.0 xslt-grouping

我有一个XML文件,我需要使用XSL转换为具有相同属性值名称(@name)的组标记。在下面的示例输入中,唯一的重复值是" objectClass"。我似乎无法找到任何其他帖子来解决我遇到的问题。下面是我的输入,代码,电流输出和所需输出。 XSL 2.0对我来说不是一个选择。

输入:

<top>
<MPTPartyUserInqMultiLDAPData>
    <DN>cn=BRK001,ou=USERS,ou=INTERNAL,o=UMB</DN>
    <attribute-value name="passwordUniqueRequired">TRUE</attribute-value>
    <attribute-value name="passwordRequired">TRUE</attribute-value>
    <attribute-value name="uid">BRK001</attribute-value>
    <attribute-value name="initials">R</attribute-value>
    <attribute-value name="cn">BRK001</attribute-value>
    <attribute-value name="umbGUID">I00000000001498</attribute-value>
    <attribute-value name="objectClass">inetOrgPerson</attribute-value>
    <attribute-value name="objectClass">umbPerson</attribute-value>
    <attribute-value name="objectClass">organizationalPerson</attribute-value>
    <attribute-value name="objectClass">Person</attribute-value>
    <attribute-value name="objectClass">ndsLoginProperties</attribute-value>
    <attribute-value name="objectClass">Top</attribute-value>
    <attribute-value name="umbCustomer">22586</attribute-value>
</MPTPartyUserInqMultiLDAPData>
<MPTPartyUserInqMultiLDAPData>
    <DN>cn=BRK001,ou=USERS,ou=INTERNAL,o=UMB</DN>
    <attribute-value name="passwordUniqueRequired">TRUE</attribute-value>
    <attribute-value name="passwordRequired">TRUE</attribute-value>
    <attribute-value name="uid">BRK001</attribute-value>
    <attribute-value name="initials">R</attribute-value>
    <attribute-value name="cn">BRK001</attribute-value>
    <attribute-value name="umbGUID">I00000000001498</attribute-value>
    <attribute-value name="objectClass">inetOrgPerson</attribute-value>
    <attribute-value name="objectClass">umbPerson</attribute-value>
    <attribute-value name="objectClass">organizationalPerson</attribute-value>
    <attribute-value name="objectClass">Person</attribute-value>
    <attribute-value name="objectClass">ndsLoginProperties</attribute-value>
    <attribute-value name="objectClass">Top</attribute-value>
    <attribute-value name="umbCustomer">22586</attribute-value>
</MPTPartyUserInqMultiLDAPData>
</top>

当前XSL:

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

<xsl:key name="test" match="attribute-value" use="@name"/>

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

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

<xsl:template match="//*[local-name()='MPTPartyUserInqMultiLDAPData']">
    <xsl:variable name="ldapFormatted">
    <MPTPartyUserInqMultiLDAPData>
            <entries>
                <xsl:for-each select="attribute-value[count(. | key('test', @name)[1]) = 1]">
                    <attributes>
                        <name>
                            <xsl:value-of select="@name"/>
                        </name>
                        <xsl:for-each select="key('test', @name)">
                            <value>
                                <xsl:value-of select="."/>
                            </value>
                        </xsl:for-each>
                    </attributes>
                </xsl:for-each>
            </entries>
        </MPTPartyUserInqMultiLDAPData>
    </xsl:variable>

    <xsl:copy-of select="$ldapFormatted"/>

</xsl:template>

当前输出:

<top>
<MPTPartyUserInqMultiLDAPData>
    <entries>
        <attributes>
            <name>passwordUniqueRequired</name>
            <value>TRUE</value>
            <value>TRUE</value>
        </attributes>
        <attributes>
            <name>passwordRequired</name>
            <value>TRUE</value>
            <value>TRUE</value>
        </attributes>
        <attributes>
            <name>uid</name>
            <value>BRK001</value>
            <value>BRK001</value>
        </attributes>
        <attributes>
            <name>initials</name>
            <value>R</value>
            <value>R</value>
        </attributes>
        <attributes>
            <name>cn</name>
            <value>BRK001</value>
            <value>BRK001</value>
        </attributes>
        <attributes>
            <name>umbGUID</name>
            <value>I00000000001498</value>
            <value>I00000000001498</value>
        </attributes>
        <attributes>
            <name>objectClass</name>
            <value>inetOrgPerson</value>
            <value>umbPerson</value>
            <value>organizationalPerson</value>
            <value>Person</value>
            <value>ndsLoginProperties</value>
            <value>Top</value>
            <value>inetOrgPerson</value>
            <value>umbPerson</value>
            <value>organizationalPerson</value>
            <value>Person</value>
            <value>ndsLoginProperties</value>
            <value>Top</value>
        </attributes>
        <attributes>
            <name>umbCustomer</name>
            <value>22586</value>
            <value>22586</value>
        </attributes>
    </entries>
</MPTPartyUserInqMultiLDAPData>
<MPTPartyUserInqMultiLDAPData>
    <entries/>
</MPTPartyUserInqMultiLDAPData>
</top>

期望的输出:

<top>
<MPTPartyUserInqMultiLDAPData>
    <entries>
        <attributes>
            <name>passwordUniqueRequired</name>
            <value>TRUE</value>
        </attributes>
        <attributes>
            <name>passwordRequired</name>
            <value>TRUE</value>
        </attributes>
        <attributes>
            <name>uid</name>
            <value>BRK001</value>
        </attributes>
        <attributes>
            <name>initials</name>
            <value>R</value>
        </attributes>
        <attributes>
            <name>cn</name>
            <value>BRK001</value>
        </attributes>
        <attributes>
            <name>umbGUID</name>
            <value>I00000000001498</value>
        </attributes>
        <attributes>
            <name>objectClass</name>
            <value>inetOrgPerson</value>
            <value>umbPerson</value>
            <value>organizationalPerson</value>
            <value>Person</value>
            <value>ndsLoginProperties</value>
            <value>Top</value>
        </attributes>
        <attributes>
            <name>umbCustomer</name>
            <value>22586</value>
        </attributes>
    </entries>
</MPTPartyUserInqMultiLDAPData>
<MPTPartyUserInqMultiLDAPData>
    <entries>
        <attributes>
            <name>passwordUniqueRequired</name>
            <value>TRUE</value>
        </attributes>
        <attributes>
            <name>passwordRequired</name>
            <value>TRUE</value>
        </attributes>
        <attributes>
            <name>uid</name>
            <value>BRK001</value>
        </attributes>
        <attributes>
            <name>initials</name>
            <value>R</value>
        </attributes>
        <attributes>
            <name>cn</name>
            <value>BRK001</value>
        </attributes>
        <attributes>
            <name>umbGUID</name>
            <value>I00000000001498</value>
        </attributes>
        <attributes>
            <name>objectClass</name>
            <value>inetOrgPerson</value>
            <value>umbPerson</value>
            <value>organizationalPerson</value>
            <value>Person</value>
            <value>ndsLoginProperties</value>
            <value>Top</value>
        </attributes>
        <attributes>
            <name>umbCustomer</name>
            <value>22586</value>
        </attributes>
    </entries>
</MPTPartyUserInqMultiLDAPData>
</top>

如果我只有一个MPTPartyUserInqMultiLDAPData分组,那么标签会被正确分组,但是当我有多次迭代时,我会将它们组合在一起,如上所示我不想要的。我理解为什么要这样做,但无法弄清楚如何将分组保留在MPTPartyUserInqMultiLDAPData标记内。

1 个答案:

答案 0 :(得分:0)

你想要做的事情如下:

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="attr" match="attribute-value" use="concat(@name, '|', generate-id(..))"/>

<xsl:template match="/top">
    <xsl:copy>
        <xsl:apply-templates select="MPTPartyUserInqMultiLDAPData"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="MPTPartyUserInqMultiLDAPData">
    <xsl:copy>
        <entries>
            <xsl:for-each select="attribute-value[count(. | key('attr', concat(@name, '|', generate-id(..)))[1])=1]">
                <attributes>
                    <name>
                        <xsl:value-of select="@name"/>
                    </name>
                    <xsl:for-each select="key('attr', concat(@name, '|', generate-id(..)))">
                        <value>
                            <xsl:value-of select="."/>
                        </value>
                    </xsl:for-each>
                </attributes>
            </xsl:for-each>
        </entries>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>