At 2009-10-09 07:14 -0400, Tony Mariella wrote:
If I have results that look like this:

<item>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 1</id>
       <address>2345</address>
    </if>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 2</id>
       <address>3456</address>
    </if>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 1</id>
       <address>2345</address>
    </if>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 2</id>
       <address>3456</address>
    </if>
</item>


And I want to de-dupe the results so that I get this:

<item>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 1</id>
       <address>2345</address>
    </if>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 2</id>
       <address>3456</address>
    </if>
</item>


How would I do it ?

I think the most elegant way would be not creating the duplicates in the first place.

But if that isn't possible, then a brute force method is below that would work. I would be a bit worried about the processing time taken to execute it though ... but the principle is really easy to see, even without comments.

I hope this helps.

. . . . . . . . Ken

T:\ftemp>type tony.xml
<item>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 1</id>
       <address>2345</address>
    </if>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 2</id>
       <address>3456</address>
    </if>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 1</id>
       <address>2345</address>
    </if>
    <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 2</id>
       <address>3456</address>
    </if>
</item>

T:\ftemp>xquery tony.xml tony.xq
<?xml version="1.0" encoding="UTF-8"?>
<item>
   <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 1</id>
       <address>2345</address>
    </if>
   <if>
       <name>Test</name>
       <date>01-Oct-2009</date>
       <id>ID 2</id>
       <address>3456</address>
    </if>
</item>
T:\ftemp>type tony.xq
<item>
  { for $each in /item/if return
        if ( not( $each/preceding-sibling::if[deep-equal(.,$each)] ) )
          then $each
          else ()
  }
</item>
T:\ftemp>


--
Upcoming: hands-on code list, UBL, XSLT, XQuery and XSL-FO classes
in Copenhagen Denmark and Washington DC USA, October/November 2009
Interested in other classes?  http://www.CraneSoftwrights.com/q/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/q/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:[email protected]
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to