本文介绍了使用xslt时如何清除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用Muenchian分组从city1或city2或city3中删除重复项,这是key和generate id,如下所示。但是无法通过循环进入所有city1,city2和city3来删除重复项。

I am able to remove duplicates from either city1 or city2 or city 3 using Muenchian grouping which is key and generate id as shown below. but am not able to remove duplicates by looping into all city1, city2 and city3

以下是xml

<test>
<records>
<city1>Sweden</city1>
<country1>value1<country1>
<town1>value2<town1>
<city2>Paris</city2>
<country2>value1<country2>
<town2>value2<town2>
<city3>London</city3>
<country3>value1<country3>
<town3>value2<town3>
</records>
<records>
<city1>Sweden</city1>
<country1>value1<country1>
<town1>value2<town1>
<city2>Frankfut</city2>
<country2>value1<country2>
<town2>value2<town2>
<city3>NEwYork</city3>
<country3>value1<country3>
<town3>value2<town3>
</records>
<records>
<city1>SFO</city1>
<country1>value1<country1>
<town1>value2<town1>
<city2>London</city2>
<city2>Frankfut</city2>
<country2>value1<country2>
<city3>Frankfut</city3>
<country3>value1<country3>
<town3>value2<town3>
</records>
</test>

输出应为

Row|Add|Sweden|value1|value2
Row|Add|London|value1|value2
Row|Add|NewYork|value1|value2
Row|Add|SFO|value1|value2

用于从city1删除重复项的代码

Code used for removing duplicates from city1

  <xsl:key name="Keycity"  match="//test/records" use="city1" />
<xsl:for-each select="//records[generate-id(.) = generate-id(key('Keycity', city1))]">
      <xsl:sort select="."/>
      <xsl:variable name="city1" select="."/>

        <Row Action="ADD">
          <xsl:value-of select="city1" />
        </Row>
      </xsl:if>
    </xsl:for-each>


推荐答案

将您的密钥定义为:

<xsl:key name="Keycity" match="city1 | city2 | city3" use="." />

然后执行:

<xsl:for-each select="(records/city1 | records/city2 | records/city3)[generate-id(.) = generate-id(key('Keycity', .))]">
    <xsl:sort select="."/>
    <Row Action="ADD">
        <xsl:value-of select="." />
    </Row>
</xsl:for-each>

这假设你在测试的上下文中根元素。

这篇关于使用xslt时如何清除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 09:32