[xquery-talk] reversing element ordering, with a caveat

Brian Maso brian at blumenfeld-maso.com
Sun Mar 15 19:23:36 PST 2009


In XQuery, I think its just a matter of running over the distinct values of
//book/@pubYear in descending order. Something like:

(* Maybe there's a more elegant way of getting the sequence of years in
descending order? *)
let $descending-distinct-years := distinct-values(for $v in
//book/@pubYear/value() order by $v descending return $v)
for $year in $descending-distinct-years
return //book[@pubYear = $year]

This will retain original document ordering for elements with the same
@pubYear value, but years will be descending from highest value to lowest.

Brian Maso

On Sun, Mar 15, 2009 at 6:04 PM, Howard Katz <howardk at fatdog.com> wrote:

> I have a series of books ordered chronologically by publication date, 1942
> to 2009. I want to generate a file in which the books have the reverse
> chronological ordering, 2009 to 1942, but with the caveat that books
> *within* a particular year need to maintain the original document order
> relative to that year. In the following, the catid attributes within a
> particular year remain in ascending order after the transformation.  (The
> catid's are used here only for demonstration purposes; they're not present
> in the actual document.)
>
> I'd be happy to see a solution either in XQuery or in XSLT. Here's some
> sample input and the output.
>
> Input:
>
> <library>
>        <book pubYear='1942' catid='1' >
>                <otherStuff ... /></book>
>        <book pubYear='1942' catid='2' >
>                <otherStuff ... /></book>
>        <book pubYear='1942' catid=' 3 ' >
>                <otherStuff ... /></book>
>        ...
>
>        <book pubYear='2009' catid=' 2011' >
>                <otherStuff ... /></book>
>        <book pubYear='2009' catid=' 2012 ' >
>                <otherStuff ... /></book>
>        <book pubYear='2009' catid=' 2013 ' >
>                <otherStuff ... /></book>
> </library>
>
> Output:
>
> <library>
>        <book pubYear='2009' catid=' 2011' >
>                <otherStuff ... /></book>
>        <book pubYear='2009' catid=' 2012 ' >
>                <otherStuff ... /></book>
>        <book pubYear='2009' catid=' 2013 ' >
>                <otherStuff ... /></book>
>        ...
>
>        <book pubYear='1942' catid=' 1 ' >
>                <otherStuff ... /></book>
>        <book pubYear='1942' catid=' 2 ' >
>                <otherStuff ... /></book>
>        <book pubYear='1942' catid=' 3 ' >
>                <otherStuff ... /></book>
> </library>
>
> TIA,
> Howard
>
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://x-query.com/pipermail/talk/attachments/20090315/0d9a4701/attachment.htm


More information about the talk mailing list