我有一个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 标签中。
我想你想做这样的事情:
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>