Как удалить дубликаты узлов с помощью XSLT? - PullRequest
1 голос
/ 21 июня 2009

Если система обнаружит дубликат, она должна полностью исключить оба узла. У меня есть оригинальный 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 ]

2 голосов
/ 21 июня 2009

Попробуйте это:

<?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 голос
/ 21 июня 2009

Этот выбор даст вам набор, когда есть дубликат COUNTRY_ID. (> 1)

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

Используйте этот набор, чтобы исключить тех из вашего выбора.

Я не запускал тест, но он должен дать вам набор для работы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...