Search code examples
htmlxmlxqueryxmlspy

XQUERY for loop and html


I have the following XQUERY code in XMLSPY which runs fine.

xquery version "1.0";

<table border="1">
{
 for $re in distinct-values(reviews/review/reviewer)
 for $r in reviews/review
 where $re=$r/reviewer
 return  <tr> <td>{$r/movie_title}</td> </tr>
}
</table>

Now I want to modify it as follows :

xquery version "1.0";

<table border="1">
{
 <tr> <td>
 for $re in distinct-values(reviews/review/reviewer)
 for $r in reviews/review
 where $re=$r/reviewer
 return  {$r/movie_title}
 </td> </tr>
}
</table>

But it gives syntax error. What the second code should accomplish is that only outer for loop should create new row and new cell and inner loop should only add to that cell.


Solution

  • Try

    for $re in distinct-values(reviews/review/reviewer) return <tr>{
        for $r in reviews/review
            where $re=$r/reviewer
                return <td>{$r/movie_title}</td>
    }</tr>
    

    This XMLPLayground session might help you. It does something similar, i.e. iterative table output.