xslt 使用 Muenchian 分组删除重复条目 [英] xslt to remove duplicates entries using Muenchian grouping

查看:39
本文介绍了xslt 使用 Muenchian 分组删除重复条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示 节点的所有 .但我不想显示来自同一 companydepartment 重复条目.

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>

从上面的XML中,我想消除IT部门的重复条目

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

示例:AOL 不止一次有IT 部门,但我只想显示IT 一次.

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

截至目前,我的 XSLT 如下:

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.

推荐答案

您需要第二级 Muenchian 分组 - 您已经有一个可以找到唯一公司名称的键,现在需要一个可以找到唯一公司部门的第二个键 :

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>

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

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

这篇关于xslt 使用 Muenchian 分组删除重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆