tags:

views:

572

answers:

3

If system finds a duplicate , it should eliminate both nodes altogether. I have original xml as:

<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
  <RECORD>
    <COUNTRY_ID>2111</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2121</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2211</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2221</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3111</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3121</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3211</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3221</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2111</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2121</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2211</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2221</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2311</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>2321</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3111</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3121</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3211</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3221</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3311</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3321</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4111</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4121</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4211</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4221</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4311</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4321</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
</RESPONSE>

and the desired output should be below:

<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
<RECORD>
    <COUNTRY_ID>2311</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
<RECORD>
    <COUNTRY_ID>2321</COUNTRY_ID>
    <CITY id="2:">0</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
<RECORD>
    <COUNTRY_ID>3311</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>3321</COUNTRY_ID>
    <CITY id="3:">30</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4111</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4121</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="1:14404388">LA11CFTXX</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4211</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4221</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="2:14048042">LAZ1129YY</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4311</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="1:">1</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
  <RECORD>
    <COUNTRY_ID>4321</COUNTRY_ID>
    <CITY id="4:">100</CITY>
    <LOCATION id="3:14048629">LAZ11B91AA</LOCATION>
    <REPID id="2:">2</REPID>
    <FLIGHTID id="1:1091513">ALK</FLIGHTID>
  </RECORD>
</RESPONSE>

can we able to write an xsl to do this?

thanks

+1  A: 

Try this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  <xsl:template match="/">
    <xsl:apply-templates select="RESPONSE"/>
  </xsl:template>

  <xsl:template match="RESPONSE">
    <RESPONSE>
      <xsl:apply-templates select="RECORD"/>
    </RESPONSE>
  </xsl:template>

  <xsl:template match="RECORD">
    <xsl:variable name="this" select="."/>
    <xsl:variable name="country" select="COUNTRY_ID/text()"/>
    <xsl:if test="not(../RECORD[COUNTRY_ID/text() = $country and generate-id($this) != generate-id(.)])">
      <xsl:copy-of select="."/>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

This is all assuming that when you say "duplicate" you mean that COUNTRY_ID has the same content. My XSLT appears to generate your desired output.

Jacob
thank you thats exactly what i wanted.
+1  A: 

This select will give you the set of when there is a duplicate COUNTRY_ID. (> 1)

select="/RESPONSE/RECORD/COUNTRY_ID/*[count(.|key('x',name())[1])>1]">

Use that set to then exclude those from your select.

I did not run a test, but it should give you the set to work with.

Mark Schultheiss
A: 

take a look here

http://www.exslt.org/set/functions/distinct/index.html

Josh

Josh
thanks. but thats xslt2.0
http://www.exslt.org/set/functions/distinct/set.distinct.template.xsl is a xslt 1.0 template implementation
Josh