如何从xml文件中删除重复的元素? [英] How to remove duplicate elements from an xml file?

查看:79
本文介绍了如何从xml文件中删除重复的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的 XML 文件

<Employee FirstName="FirstName_0" LastName="LastName_1" dept="dept_2" empNumber="1"><日程安排><Schedule Date_join="2008-01-20" Date_end="2008-01-30"/></时间表></员工><Employee FirstName="FirstName_0" LastName="LastName_1" dept="dept_2" empNumber="2"><日程安排><Schedule Date_join="2008-01-20" Date_end="2008-01-30"/></时间表></员工><Employee FirstName="FirstName_2" LastName="LastName_1" dept="dept_2" empNumber="2"><日程安排><Schedule Date_join="2007-01-21" Date_end="2007-12-30"/></时间表></员工><Employee FirstName="FirstName_2" LastName="LastName_1" dept="dept_2" empNumber="2"><日程安排><Schedule Date_join="2007-01-21" Date_end="2007-12-30"/><Schedule Date_join="2008-06-20" Date_end="2008-01-30"/></时间表></员工></ns0:员工>

我想根据 fistname、last name 和 date_join 和 data_end 删除重复项.

请问,谁能解释一下如何使用 XSLT 实现这一点?

解决方案

以下是 如何根据元素名称和 id 字段删除重复项.将其扩展到任意字段应该不会太难.

<块引用>

问:扩展.我的 xml 的一部分看起来像这样:

 <位置><state>xxxx</state></位置><位置><state>yyyy</state></位置><位置><state>xxxx</state></位置>

<块引用>

所需的输出是:

xxxxyyyy

<块引用>

也就是说,不应打印 state 的重复值.这能做到吗?

 <xsl:for-each select="$unique-list"><xsl:value-of select="."/></xsl:for-each>

I have an XML file like

<ns0:Employees xmlns:ns0="http://TestIndexMap.Employees">
  <Employee FirstName="FirstName_0" LastName="LastName_1" dept="dept_2" empNumber="1">
    <Schedules>
      <Schedule Date_join="2008-01-20" Date_end="2008-01-30" />
    </Schedules>
  </Employee>
  <Employee FirstName="FirstName_0" LastName="LastName_1" dept="dept_2" empNumber="2">
    <Schedules>
      <Schedule Date_join="2008-01-20" Date_end="2008-01-30" />
    </Schedules>
  </Employee>
  <Employee FirstName="FirstName_2" LastName="LastName_1" dept="dept_2" empNumber="2">
    <Schedules>
      <Schedule Date_join="2007-01-21" Date_end="2007-12-30" />

    </Schedules>
  </Employee>
  <Employee FirstName="FirstName_2" LastName="LastName_1" dept="dept_2" empNumber="2">
    <Schedules>
      <Schedule Date_join="2007-01-21" Date_end="2007-12-30" />
      <Schedule Date_join="2008-06-20" Date_end="2008-01-30" />

    </Schedules>
  </Employee>
</ns0:Employees>

I would want to remove the duplicates based on the fistname, last name and date_join and data_end .

Please, can someone explain how to achive this with XSLT?

解决方案

Here are some samples of how to remove duplicates based on element name and id field. It should be not too hard to extend this to arbitrary fields.

Q: Expansion. A part of my xml looks like this:

 <location>
   <state>xxxx</state>
 </location>

 <location>
    <state>yyyy</state>
 </location>

  <location>
    <state>xxxx</state>
 </location>

The desired output is:

xxxx
yyyy

That is, duplicate values of state should not be printed. Can this be done?

   <xsl:variable name="unique-list"
     select="//state[not(.=following::state)]" />   

   <xsl:for-each select="$unique-list">
 <xsl:value-of select="." />
   </xsl:for-each>

这篇关于如何从xml文件中删除重复的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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