At 2010-02-17 11:49 -0500, Tony Mariella wrote:
The Tall Rd items are different than the Short Rd items.
I'm trying to de-dupe the items with the caveat that if the testVal tag is set,
then collapse all identical items containing a blank testVal tag.

But you didn't say that the first time.  :{)}

<results>
 <item>
    <addr>24 Short Rd</addr>
    <city>Baltimore</city>
    <state>MD</state>
    <testVal>TEST1</testVal>
 </item>
 <item>
    <addr>55 Tall Rd</addr>
    <city>Orlando</city>
    <state>FL</state>
    <testVal/>
 </item>
</results>

Spelling out your requirement in words, backing it up with the example really helps.

I hope the answer below helps.

. . . . . . . . . . . Ken


T:\ftemp>call xquery tony2.xq
<?xml version="1.0" encoding="UTF-8"?>
<results>
   <item>
      <addr>24 Short Rd</addr>
      <city>Baltimore</city>
      <state>MD</state>
      <testVal>TEST1</testVal>
   </item>
   <item>
      <addr>55 Tall Rd</addr>
      <city>Orlando</city>
      <state>FL</state>
      <testVal/>
   </item>
</results>
T:\ftemp>type tony2.xml
<results>
   <item>
      <addr>24 Short Rd</addr>
      <city>Baltimore</city>
      <state>MD</state>
      <testVal/>
   </item>
   <item>
      <addr>24 Short Rd</addr>
      <city>Baltimore</city>
      <state>MD</state>
      <testVal/>
   </item>
   <item>
      <addr>24 Short Rd</addr>
      <city>Baltimore</city>
      <state>MD</state>
      <testVal>TEST1</testVal>
   </item>
   <item>
      <addr>55 Tall Rd</addr>
      <city>Orlando</city>
      <state>FL</state>
      <testVal/>
   </item>
   <item>
      <addr>55 Tall Rd</addr>
      <city>Orlando</city>
      <state>FL</state>
      <testVal/>
   </item>
   <item>
      <addr>55 Tall Rd</addr>
      <city>Orlando</city>
      <state>FL</state>
      <testVal/>
   </item>
</results>

T:\ftemp>type tony2.xq
declare function local:distinct-items ($items as node()*) as node()*
{
  (:walk through the information finding unique members:)
  let $unique := for $i at $ipos in $items
                 let $before_i := subsequence( $items, 1, $ipos - 1 )
                 where every $bi in $before_i
                       satisfies not( deep-equal($bi, $i) )
                 return $i
   (:rearrange the information to isolate the non-testVal info:)
   let $interim := for $u in $unique
                   return <interim>
                            <compare>{$u/node() except $u/testVal}</compare>
                            {$u}
                          </interim>
   (:walk through the rearranged information de-duping those without a
     value for testVal and for those with testVal removing all the same
     without it:)
   for $each in $interim return
       if ( string( $each/item/testVal ) )
         then $each/item (:because this has testVal:)
         else if ( some $i in $interim except $each
                   satisfies deep-equal( $i/compare, $each/compare ) )
                 then () (:because the other must have testVal:)
                 else $each/item (:because none have testVal:)
};

<results>
  { local:distinct-items( doc('tony2.xml')/results/item ) }
</results>
T:\ftemp>rem Done!




--
XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19
XSLT/XQuery/XPath training:   San Carlos, California 2010-04-26/30
Vote for your XML training:   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