CSnerd
CSnerd

Reputation: 2229

Xquery syntax error

<authors> {
for $a in fn:distinct-values(doc("data.xml")/books/book/author/text())
 return  <name> {$a} </name> 
 {
    for $b in doc("data.xml")/books/book[author/text() = $a]
    return <book> {$b/title/text()}</book>
 }
}
</authors>

This is my code, when I ran this code, there is syntax error:

4,1: static error [err:XPST0003]: invalid expression: syntax error, unexpected expression (missing comma "," between expressions?)

What is wrong with this code?

Upvotes: 0

Views: 827

Answers (1)

Ewout Graswinckel
Ewout Graswinckel

Reputation: 1273

From your query it's not exactly clear in what format you want the output to be returned, but the closest I can come up with which would be syntactically correct is this (note that the curly brackets are only used for outputting stuff within xml syntax):

<authors> {
for $a in fn:distinct-values(doc("data.xml")/books/book/author/text())
 return  (<name> {$a} </name>,
    for $b in doc("data.xml")/books/book[author/text() = $a]
    return <book> {$b/title/text()}</book>)
}
</authors>

a more 'logical' structure to return would be this though:

<authors> {
for $a in fn:distinct-values(doc("data.xml")/books/book/author/text())
 return  <author>
           <name> {$a} </name>
           { for $b in doc("data.xml")/books/book[author/text() = $a]
               return <book> {$b/title/text()}</book>
           }
         </author>
}
</authors>

Upvotes: 4

Related Questions