0

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>
user3067170
  • 193
  • 4
  • 14

1 Answers1

0

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.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • This works perfectly.But i need to add more values for each row(I have updated my output in the question). For this i need to know what is "." in for example if it is city1 or city2. – user3067170 Jul 14 '16 at 14:58
  • I am afraid I have no idea what you mean. Where are these values coming from? An why are they attached only to "Sweden"? – michael.hor257k Jul 14 '16 at 15:10
  • am so sorry to being so vauge. i have now updated my xml and output – user3067170 Jul 14 '16 at 15:24
  • Sorry, I still don't follow. The example is ambiguous (value1 and value 2 appear in many places, and it's not clear which one came from where). Please explain the **rules** that the transformation needs to follow first, then give example/s. – michael.hor257k Jul 14 '16 at 15:28
  • I am afraid it makes no sense to me. I suggest you close this question and post a new one - hopefully with a better explanation – michael.hor257k Jul 14 '16 at 15:59
  • am so sorry for confusing you. The actual scenario is i have records as flights with 3 destinations each. I have to store those destinations without duplicating the records – user3067170 Jul 14 '16 at 15:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117345/discussion-between-user3067170-and-michael-hor257k). – user3067170 Jul 14 '16 at 15:59
  • No, let us end this here. – michael.hor257k Jul 14 '16 at 16:01
  • i have added a new question at http://stackoverflow.com/q/38379280/3067170 Please take a look – user3067170 Jul 14 '16 at 16:17