Thursday, June 23, 2011

Xquery, the need to cast your variables

Today I have wasted 1 hour on this:

the inputs are:



27
Al Quran



1
La Tregua





Mohamed



Levi




my Xquery does:

THIS is WRONG:

let $myAuthorIndex := data($books/book[name="Mohamed"]/authorIndex)
let $myAuthors := $authors/author[$myAuthorIndex]


well, this will not work.... you will get ALL AUTHORS.
The trick is to CAST $myAuthorIndex to an integer, otherwise it will be considered a string and ignored


THIS is correct:

let $myAuthorIndex := xs:int(data($books/book[name="Mohamed"]/authorIndex))
let $myAuthors := $authors/author[$myAuthorIndex]

No comments: