grouping repeating xml data with xsl



I have an XML file that I need to transform with XSL to group tags with the same attribute-value name(@name). In the sample input below, the only repeating value is "objectClass". I can't seem to find any other posts that solve the problem that I'm having. Below is my input, code, current output and desired output. XSL 2.0 is not an option for me.


Input:



<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>


Current XSL:



<xsl:stylesheet xmlns:xsl="http://ift.tt/tCZ8VR" 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>


Current Output:



<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>


Desired Output:



<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>


If I only have one MPTPartyUserInqMultiLDAPData grouping, the tags are grouped correctly but when I have multiple iterations, which I will, it groups them together as shown above which I don't want. I understand why it's doing it but can't figure out how to keep the grouping within the MPTPartyUserInqMultiLDAPData tag.


No comments:

Post a Comment