Question

I am using XQuery in exist-db and I want to use a counter. For example I have this code :

xquery version "3.0";
for $P in 1 to 351
  let $S := data(doc(concat('/db/INDEX/' , $P , '.html'))//h1)
    for $result in $S
     return <a class="libraryIndexlink" href="http://library.net/newindex.aspx?pid=102834&amp;BookID=106201&amp;PageIndex={$P}&amp;Language=1#p0">{$result}</a>

I need that after Language=1#p the counter variable follows. For example:

Language=1#p0">

Language=1#p1">

Language=1#p2">

I write this codes:

declare function local:prod2ndDigit() {
    for $x in (0 to 10)
        return <counter>{$x}</counter>
};
local:prod2ndDigit()

This code works, but has a logical error. My other code is:

declare function local:prod2ndDigit() {
  declare variable $C := 0
  return $C := $C + 1
};
local:prod2ndDigit()

This code does not work.

The final code is:

declare function local:prod2ndDigit() {
    for $x in (0 to 10)
        return <counter>{$x}</counter>
};

for $P in 1 to 351
  let $S := data(doc(concat('/db/INDEX/' , $P , '.html'))//h1)
    for $result in $S
     return <a class="libraryIndexlink" href="http://library.net/newindex.aspx?pid=102834&amp;BookID=106201&amp;PageIndex={$P}&amp;Language=1#plocal:prod2ndDigit()">{$result}</a>

But this code seems to have a logical error. I write this code but it returns false instead of an integer:

let $C := 0
    return
        $C = $C + 1
Was it helpful?

Solution

The logic you are using does not apply to a functional programming language like XQuery. I guess you want to iterate over each h1 element and want to include an increasing counter. This is actually even simpler than you probably imagined:

for $result at $key in data(doc(concat('/db/INDEX/' , $P , '.html'))//h1)
return <a class="libraryIndexlink" href="http://library.net/newindex.aspx?pid=102834&amp;BookID=106201&amp;PageIndex={$P}&amp;Language=1#p{$key - 1}">{$result}</a>

The at $key statement gives you the position, i.e. the counter you want. As it seems you want to start with 0, but in XQuery sequences always start at position 1, hence we use the - 1

There are a couple of other things wrong in your code. You will have to put XQuery function calls or variables into curly braces, what you did do for $P, but not for local:prod2ndDigit().

That the code let $C := 0 return $C = $C + 1 returns false is correct. You are using the = operator, which does mean equal in XQuery. You probably intended to write an assignment, which the operator for is :=. Also, it seems like you want to increase the counter. Keep in mind that XQuery is a functional language, as I said before, so you can never manipulate a variable You will always create a new $counter variable and shadow the old value, but you can not change the variable as you do in other languages like C, Java, Python and so on and so forth.

I don't have any idea what you want to achieve with your function local:prod2ndDigit(). It returns a sequence of items, but I do not see the use case here.

OTHER TIPS

Thanks dirkk . I manipulate your answer and get the correct answer: Very very thanks....

xquery version "3.0";
for $P in 1 to 351
for $result at $key in data(doc(concat('/db/INDEX/' , $P , '.html'))//h1)
return <a class="libraryIndexlink" href="http://library.net/newindex.aspx?pid=102834&amp;BookID=106201&amp;PageIndex={$P}&amp;Language=1#p{$key - 2+$P}">{$result}</a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top