如何使用XSLT删除重复的节点?

时间:2009-06-20 23:18:28

标签: xml xslt

如果系统发现重复,它应该完全消除两个节点。 我有原始的xml:

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

,所需的输出应低于:

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

我们能写一个xsl来做这个吗?

感谢

2 个答案:

答案 0 :(得分:2)

试试这个:

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

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

这是假设当你说“重复”时你的意思是COUNTRY_ID具有相同的内容。我的XSLT似乎生成了您想要的输出。

答案 1 :(得分:1)

此选择将为您提供重复的COUNTRY_ID时的设置。 (&gt; 1)

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

使用该设置然后从您的选择中排除那些。

我没有进行测试,但是应该给你一套可以使用的测试。