[xquery-talk] return only latest version of an item

G. Ken Holman gkholman at CraneSoftwrights.com
Thu May 14 17:39:21 PDT 2009


At 2009-05-14 16:06 -0400, A. Steven Anderson wrote:
>Unfortunately though, I tried your suggestion but only returned one 
>item which was had the greatest version of all items.

I thought Pavel's suggestion was just fine and, indeed, the result is 
more than one item (transcript below).

>As far as implementation, I'm querying eXist-db 1.2.5.
>
>Any other suggestions?

Find another processor?

BTW, your data set happens to have steadily increasing version values 
for each <id>.  If it is true that the last of each <id> has the 
highest version, this might execute even faster because it has no 
function call:

   /items/item[not(id=following-sibling::item[1]/id)]

It returns the same result as Pavel's.

I hope this helps.

. . . . . . . . . . Ken

t:\ftemp>type steven.xml
<items>
    <item>
       <id>1</id>
       <name>item # 1</name>
       <version>1</version>
    </item>
    <item>
       <id>1</id>
       <name>item # 1</name>
       <version>2</version>
    </item>
    <item>
       <id>1</id>
       <name>item # 1</name>
       <version>3</version>
    </item>
    <item>
       <id>2</id>
       <name>item # 2</name>
       <version>1</version>
    </item>
    <item>
       <id>2</id>
       <name>item # 2</name>
       <version>2</version>
    </item>
    <item>
       <id>3</id>
       <name>item # 3</name>
       <version>1</version>
    </item>
</items>

t:\ftemp>type steven.xq
for $item in /items/item
  where $item/version = max(/items/item[id = $item/id]/version)
  return $item


t:\ftemp>xquery steven.xml steven.xq
<?xml version="1.0" encoding="UTF-8"?>
<item>
       <id>1</id>
       <name>item # 1</name>
       <version>3</version>
    </item>
<item>
       <id>2</id>
       <name>item # 2</name>
       <version>2</version>
    </item>
<item>
       <id>3</id>
       <name>item # 3</name>
       <version>1</version>
    </item>
t:\ftemp>type steven2.xq
/items/item[not(id=following-sibling::item[1]/id)]
t:\ftemp>xquery steven.xml steven2.xq
<?xml version="1.0" encoding="UTF-8"?>
<item>
       <id>1</id>
       <name>item # 1</name>
       <version>3</version>
    </item>
<item>
       <id>2</id>
       <name>item # 2</name>
       <version>2</version>
    </item>
<item>
       <id>3</id>
       <name>item # 3</name>
       <version>1</version>
    </item>
t:\ftemp>

--
XQuery/XSLT/XSL-FO hands-on training - Los Angeles, USA 2009-06-08
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:gkholman at CraneSoftwrights.com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



More information about the talk mailing list