How to remove duplicates when using xslt

我的未来我决定 提交于 2019-12-12 03:48:44

问题


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:


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.



来源:https://stackoverflow.com/questions/38362143/how-to-remove-duplicates-when-using-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!