How to remove duplicates when using xslt

2019-09-09 17:05发布

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

Below is the 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>

Output should be

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

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>

1条回答
smile是对你的礼貌
2楼-- · 2019-09-09 17:38

Define your key as:

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

Then do:

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

This assumes you are in the context of the test root element.

查看更多
登录 后发表回答