使用 xslt 时如何删除重复项

How to remove duplicates when using xslt

我能够使用关键的 Muenchian 分组从 city1 或 city2 或 city 3 中删除重复项并生成 id,如下所示。但无法通过循环进入所有 city1、city2 和 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 中删除重复项的代码

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

这假定您处于 test 根元素的上下文中。