xslt使用Muenchian分组删除重复条目

时间:2015-03-24 13:32:11

标签: xml xslt xslt-1.0

我正在尝试显示<department>个节点的所有<company>个。但我不想在同一department显示company的重复条目。

XML:

<employee_data>
    <employeedetails id="1">
        <company id="1">
            <companyname>AOL</companyname>
            <department>IT</department>
        </company>
        <employeename>Patrick</employeename>
        <employeedesg>Software Engineer</employeedesg>
        <employeesalary>18000</employeesalary>
        <employeedoj>10/03/2015</employeedoj>
    </employeedetails>

    ..... similar sets......
     ..... similar sets......

    <employeedetails id="10">
        <company id="1">
            <companyname>AOL</companyname>
            <department>IT</department>
        </company>
        <employeename>Patricia</employeename>
        <employeedesg>HR Assistant</employeedesg>
        <employeesalary>18000</employeesalary>
        <employeedoj>10/03/2015</employeedoj>
    </employeedetails>
</employee_data>

从上面的XML中,我想删除IT部门

的重复条目

示例:AOL多次有IT部门,但我只想显示IT一次。

截至目前,我的 XSLT 如下:

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

    <xsl:key name="companyname" match="company" use="companyname"/>

    <xsl:template match="/">
        <xsl:for-each select="/employee_data/employeedetails/company[generate-id() = generate-id(key('companyname', companyname)[1])]">
            <tr>
                <td>
                    <xsl:value-of select="@id"/>
                </td>
                <td>
                    <xsl:apply-templates select="key('companyname', companyname)" />
                </td>
            </tr>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="company">
        <xsl:value-of select="department" />
        <br />
    </xsl:template>
</xsl:stylesheet>

screenshot中突出显示的部门不应再显示两次。

1 个答案:

答案 0 :(得分:1)

您需要第二级别的Muenchian分组 - 您已经拥有一个找到唯一公司名称的密钥,您现在需要第二个密钥才能找到唯一的公司部门

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

    <xsl:key name="companyname" match="company" use="companyname"/>
    <xsl:key name="companyDepartment" match="company"
             use="concat(companyname, '|', department)" />

    <xsl:template match="/">
        <xsl:for-each select="/employee_data/employeedetails/company[generate-id() = generate-id(key('companyname', companyname)[1])]">
            <tr>
                <td>
                    <xsl:value-of select="@id"/>
                </td>
                <td>
                    <xsl:apply-templates select="key('companyname', companyname)
                          [generate-id() = generate-id(key('companyDepartment',
                              concat(companyname, '|', department))[1])]" />
                </td>
            </tr>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="company">
        <xsl:value-of select="department" />
        <br />
    </xsl:template>
</xsl:stylesheet>

这会过滤与当前company匹配的所有companyname元素的列表,因此您只需首先提及每个department