开发者

Remove Duplicate Entry from XML using Xslt

<LISTINGS>
<LISTING LISTING_ID="123456789">
    <NAME1>1</NAME1>
    <NAME1>1</NAME1>
    <NAME1>13</NAME1>
    <NAME1>13</NAME1>
    <NAME1>12</NAME1>
    <NAME1>100</NAME1>
    <NAME1>sumit is testing</NAME1>
    <NAME1>TEST IT</NAME1>
</LISTING>

<LISTING LISTING_ID="987654321">
    <NAME1>3</NAME1>
    <NAME1>3</NAME1>
    <NAME1>4</NAME1>
</LISTING>

<LISTING LISTING_ID="5656566565">
    <NAME1>3</NAME1>
    <NAME1>4</NAME1>
&l开发者_运维知识库t;/LISTING>
</LISTINGS>

Output should be

<LISTINGS>
<LISTING LISTING_ID="123456789">
    <NAME1>1</NAME1>
    <NAME1>13</NAME1>
    <NAME1>12</NAME1>
    <NAME1>100</NAME1>
    <NAME1>sumit is testing</NAME1>
    <NAME1>TEST IT</NAME1>
</LISTING>

<LISTING LISTING_ID="987654321">
    <NAME1>3</NAME1>
    <NAME1>4</NAME1>
</LISTING>

<LISTING LISTING_ID="5656566565">
    <NAME1>3</NAME1>
    <NAME1>4</NAME1>
</LISTING>
</LISTINGS>


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kElementByListingIdAndValue"
             match="LISTING/*"
             use="concat(../@LISTING_ID,'+',.)"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="LISTING/*[count(.|key('kElementByListingIdAndValue',
                                               concat(../@LISTING_ID,
                                                      '+',
                                                      .)
                                              )[1]) != 1]"/>
</xsl:stylesheet>

Output:

<LISTINGS>
    <LISTING LISTING_ID="123456789">
        <NAME1>1</NAME1>
        <NAME1>13</NAME1>
        <NAME1>12</NAME1>
        <NAME1>100</NAME1>
        <NAME1>sumit is testing</NAME1>
        <NAME1>TEST IT</NAME1>
    </LISTING>
    <LISTING LISTING_ID="987654321">
        <NAME1>3</NAME1>
        <NAME1>4</NAME1>
    </LISTING>
    <LISTING LISTING_ID="5656566565">
        <NAME1>3</NAME1>
        <NAME1>4</NAME1>
    </LISTING>
</LISTINGS>


Using the Muenchian method for grouping:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kListNameByVal" match="LISTING/NAME1"
  use="concat(generate-id(..),'+',.)"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match=
 "NAME1
   [not(generate-id()
       =
        generate-id(key('kListNameByVal',
                        concat(generate-id(..),'+',.)
                        )
                        [1]
                    )
        )
   ]
 "/>
</xsl:stylesheet>

when applied on the provided XML document:

<LISTINGS>
<LISTING LISTING_ID="123456789">
    <NAME1>1</NAME1>
    <NAME1>1</NAME1>
    <NAME1>13</NAME1>
    <NAME1>13</NAME1>
    <NAME1>12</NAME1>
    <NAME1>100</NAME1>
    <NAME1>sumit is testing</NAME1>
    <NAME1>TEST IT</NAME1>
</LISTING>

<LISTING LISTING_ID="987654321">
    <NAME1>3</NAME1>
    <NAME1>3</NAME1>
    <NAME1>4</NAME1>
</LISTING>

<LISTING LISTING_ID="5656566565">
    <NAME1>3</NAME1>
    <NAME1>4</NAME1>
</LISTING>
</LISTINGS>

the wanted, correct result is produced:

<LISTINGS>
   <LISTING LISTING_ID="123456789">
      <NAME1>1</NAME1>
      <NAME1>13</NAME1>
      <NAME1>12</NAME1>
      <NAME1>100</NAME1>
      <NAME1>sumit is testing</NAME1>
      <NAME1>TEST IT</NAME1>
   </LISTING>
   <LISTING LISTING_ID="987654321">
      <NAME1>3</NAME1>
      <NAME1>4</NAME1>
   </LISTING>
   <LISTING LISTING_ID="5656566565">
      <NAME1>3</NAME1>
      <NAME1>4</NAME1>
   </LISTING>
</LISTINGS>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜