xslt to remove duplicates entries using Muenchian

2019-03-02 14:04发布

问题:

I am trying to display all <department>s of <company> node. But I don't want to display duplicate entries of department from the same 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>

From the above XML, I want to eliminate duplicate entries of IT department

Example: AOL has IT Department more than once, but I want to display IT only once.

As of now, my XSLT is as below:

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

Highlighted department in the screenshot should not be displayed twice.

回答1:

You need a second level of Muenchian grouping - you already have a key that finds unique company names, you now need a second key that finds unique company-department pairs:

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

This filters the list of all company elements matching the current companyname so you just get the first mention of each department.