[xquery-talk] Optimal expression for gathering related elements

David Lee dlee at calldei.com
Tue Mar 1 10:33:21 PST 2011

I find I use this pattern frequently when I need to group multiple elements
associated with some shared identifier (say @id)
Suppose I have something like

  <drug @id="1"><text>texta</text></drug>
  <drug @id="2"> <text>textb</text></drug>
  <drug @id="3"> <text>textc</text></drug>
  <drug @id="1"> <text>textd</text></drug>
  <drug @id="2"> <text>texte</text></drug>

And I want to create a set of combined entries
   <unique_drug @id="1">
 	  <drug ><text>texta</text></drug>
         <drug > <text>textd</text></drug>
   <unique_drug @id="2">
 	  <drug ><text>textb</text></drug>
         <drug > <text>texte</text></drug>


What I do is something like this :
( typed into email, not tested ...)

for $id in  distinct-values(/all/drug/@id)
     <unique_drug id="{$id}">
          		for $drug in /all/drug[@id eq $id] 

What I was offhand wondering, is if there isnt something more direct (in
XQuery 1).
It seems both verbose and inefficient, although of course efficiency is a
processor issue. (maybe it makes indexes ...)
But still ... it seems it has to scan all elements to get the unique id's
then re-scan them N times to get the matching elements, 
when I can imagine a syntax which would do both at once in linear time as
opposed to (presumably) exponential time.
It seems like something a declarative expression should be able to state

Any suggestions ? Or am I just fantasizing 



David A. Lee
dlee at calldei.com

More information about the talk mailing list