Hi,

Here's one way, using a map:

Algorithm:

1/ Order item by @id, then by @create-date-time descending, forming a new 
sequence of item.
2/ Insert item into a map keyed by @id; because you sorted on @create-date-time 
descending, more recent values will be overwritten by older ones, leaving just 
the older ones.
3/ Extract the values from the map to re-form your data structure.

XQuery:


let $items := <items>
  <item id="201100649049" create-date-time="2013-08-27T15:43:33"/>
  <item id="201100649049" create-date-time="2013-08-28T07:35:40"/>
  <item id="201100649040" create-date-time="2013-08-26T07:35:40"/>
</items>

let $ordered-item :=
  for $item in $items/item
  order by xs:integer($item/@id), xs:dateTime($item/@create-date-time) 
descending
  return $item

let $map := map:map()
let $_ := $ordered-item/map:put($map, ./@id, .)

return element items { 
        for $key in map:keys($map)
        return map:get($map, $key) 
}



Notes:

i. You can't do the map:put() in the ordering FLWOR expression, as you might 
first think, since / implies document-order, which is the order of the input 
expression, rather that of the ordered output expression, so you'd end up 
inserting into the map in document order, not the desired sorted order.
ii. I'm not ordering the final result sequence; this technique doesn't retain 
the input node order, though you could append a position attribute onto the 
intermediate item node, and re-order on that, if you really need to.
iii. Casting may be necessary/unnecessary depending on whether you are using a 
schema.

Ellis.

On 17 Sep 2013, at 22:03, Abhishek53 S <[email protected]> wrote:

> Hi All,
> 
> Need some tricks on XPATH evaluation.
> 
> I need distinct id with minimum create-data-time value.
> 
> Input
> 
> <items>
>   <item id="201100649049" create-date-time="2013-08-27T15:43:33"/>
>   <item id="201100649049" create-date-time="2013-08-28T07:35:40"/>
>   <item id="201100649040" create-date-time="2013-08-26T07:35:40"/>
> </items>
> 
> Output (Removed row 2)
> 
> <items>
>   <item id="201100649049" create-date-time="2013-08-27T15:43:33"/>
>   <item id="201100649040" create-date-time="2013-08-26T07:35:40"/>
> </items>
> 
> Please let me know some XPATH expression to evaluate this
> 
> Something like below working for distinct id but not working for minimum 
> create-date-time:(
> 
> $items/item[not(@id = preceding-sibling::item/@id) ]
> 
> 
> Thanks
> Abhishek
> =====-----=====-----=====
> Notice: The information contained in this e-mail
> message and/or attachments to it may contain 
> confidential or privileged information. If you are 
> not the intended recipient, any dissemination, use, 
> review, distribution, printing or copying of the 
> information contained in this e-mail message 
> and/or attachments to it are strictly prohibited. If 
> you have received this communication in error, 
> please notify us by reply e-mail or telephone and 
> immediately and permanently delete the message 
> and any attachments. Thank you
> 
> 
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general

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

Reply via email to