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

Pavel Minaev int19h at gmail.com
Thu May 14 12:41:17 PDT 2009


On Thu, May 14, 2009 at 11:23 AM, G. Ken Holman
<gkholman at cranesoftwrights.com> wrote:
> At 2009-05-14 14:18 -0400, A. Steven Anderson wrote:
>>
>> I've got a pretty complex xquery that returns only the latest version of
>> all items in a collection, but I know there has got to be a more efficient
>> way to do it.
>> ...
>> What would be the most efficient way to do this?
>
> By which criteria are you trying to measure efficiency?
>
> Below is an example using max()
>
> 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
> /items/item[version=max(/items/item/version)]

That doesn't work as intended - it gets all items with version number
equal to the highest version number for all items. What is needed (if
I understood the original request correctly) is to only compare
versions within group of items with the same id. Something like this:

 for $item in /items/item
 where $item/version = max(/items/item[id = $item/id]/version)
 return $item

It's hard to tell the performance impact of this, since it's heavily
implementation-dependant. Though I'd expect grouping operations (like
xsl:for-each-group) to be easier to optimize, if they were there.



More information about the talk mailing list