views:

44

answers:

2

Hi

I am trying to do some simple pagination in XQuery. I would like my root element of the returned XML to have (as attributes) various properties about the pagination (current page etc).

However I can't seem to find a way to add these dynamic attributes to my root element. I've tried playing with the element name {expr} and attribute name {expr} functions, but can't seem to get them to work.

Any help appreciated.

Many thanks

<result>{

let $results :=
for $item in doc('mydoc')/root/item
return $item

let $requested-page-nbr := 2
let $items-per-page := 10
let $count := count($results)

let $last-page-nbr := fn:ceiling($count div $items-per-page)
let $actual-page-nbr := if ($requested-page-nbr gt $last-page-nbr) then $last-page-nbr else $requested-page-nbr
let $start-item := $items-per-page * $actual-page-nbr - ( $items-per-page - 1 )
let $natural-end-item := $actual-page-nbr * $items-per-page
let $actual-end-item := if ($count ge $natural-end-item) then $natural-end-item else $count

for $j in ($start-item to $actual-end-item )
let $current := item-at($results, $j)
return
 <document-summary
    requested-page-nbr="{$requested-page-nbr}"
    items-per-page="{$items-per-page}"
    count="{$count}"
    last-page-nbr="{$last-page-nbr}"
    actual-page-nbr="{$actual-page-nbr}"
    start-item="{$start-item}"
    natural-end-item="{$natural-end-item}"
    actual-end-item="{$actual-end-item}">
  {($current)}
 </document-summary>
}</result>
A: 

to add an attribute to the root:

<result>{attribute page {3}}</result>

in your case you probably want to do something like: (?)

...
return ( 
  attribute page {$actual-page-nbr},

  for $j in ($start-item to $actual-end-item )
  let $current := item-at($results, $j)
  return
    <document-summary
       requested-page-nbr="{$requested-page-nbr}"
       items-per-page="{$items-per-page}"
       count="{$count}"
       last-page-nbr="{$last-page-nbr}"
       actual-page-nbr="{$actual-page-nbr}"
       start-item="{$start-item}"
       natural-end-item="{$natural-end-item}"
       actual-end-item="{$actual-end-item}">
     {($current)}
    </document-summary>)
...

does that answer your question?

Dennis Knochenwefel
A: 

I don't think that is the proper XQuery way...

This XQuery:

declare variable $requested-page-nbr external;
declare variable $items-per-page external;
declare variable $items := /root/item;
declare variable $firsties := $items[position() mod $items-per-page = 1];
for $first in $firsties 
let $actual-page-nbr := index-of($firsties,$first)
let $group := $first|
              $first/following-sibling::item[position() < $items-per-page]
let $previous := ($actual-page-nbr - 1) * $items-per-page
where $actual-page-nbr = $requested-page-nbr
return
<result>
    <document-summary requested-page-nbr="{$requested-page-nbr}" 
                      items-per-page="{$items-per-page}" 
                      count="{count($items)}" 
                      last-page-nbr="{count($firsties)}" 
                      actual-page-nbr="{$actual-page-nbr}" 
                      start-item="{$previous + 1}" 
                      natural-end-item="{$previous + $items-per-page}" 
                      actual-end-item="{$previous + count($group)}">{ 
       $group 
   }</document-summary> 
</result> 

With this input:

<root>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
    <item>13</item>
</root>

With $requested-page-nbr set to 2 and $items-per-page set to 3, output:

<result>
    <document-summary requested-page-nbr="2"
                      items-per-page="3"
                      count="13"
                      last-page-nbr="5"
                      actual-page-nbr="2"
                      start-item="4"
                      natural-end-item="6"
                      actual-end-item="6">
        <item>4</item>
        <item>5</item>
        <item>6</item>
    </document-summary>
</result>

With $requested-page-nbr set to 4 and $items-per-page set to 4, output:

<result>
    <document-summary requested-page-nbr="4"
                      items-per-page="4"
                      count="13"
                      last-page-nbr="4"
                      actual-page-nbr="4"
                      start-item="13"
                      natural-end-item="16"
                      actual-end-item="13">
        <item>13</item>
    </document-summary>
</result>
Alejandro