XQuery to HTML table: generating columns

306 Views Asked by At

I'm working with a collection of 52 XML documents. I'm trying to write an XQuery to generate an HTML table from them. Each document would get one column with about 50 rows. The XQuery I have so far looks like this:

    declare variable $bills := collection('BillsXML/?select=*.xml');
    declare variable $week :=$bills/bill/data(@week);
    <html>
    <head></head>
    <body>
    <table>
    <tr><th>Cause</th><th>Number</th></tr>
    {
    for $b in $bills
        let $causes := $b//item/data(@cause)
        for $c in $causes
            let $deaths-per-cause := $b//item[data(@cause)=$c]/data(@number)
    
    return <tr><td>{$c}</td><td>{$deaths-per-cause}</td></tr>
    }
    </table>
    </body>
    </html>

This works fine when run over one $b (document): I get a two-column table, with the fifty causes of death making fifty rows using the $c for-loop. But what I want to do is also to automatically generate the fifty-two columns, one per $b (document). The first column should still list the causes of death, and each column after should have the document's $week number in the and the $deaths-per-cause from that document arrayed down the cells of the column. Is this possible? For example, do I write two FLWOR statements, one to generate the columns and the other to generate the rows to populate them? And if so, how do they interlock?

1

There are 1 best solutions below

2
Michael Kay On

You haven't shown your XML source so this is a rough guess, but you want something like

for $c in $causes return 
<tr>{
  <td>{$c}</td>,
  for $b in $bills return
     <td>{$b//item[@cause=$c]/data(@number)}</td>
}</tr>