# Generating HTML

### Create a 10x10 table and color the cells

    xquery version "1.0";
    (: $Id: table.xq 6434 2007-08-28 18:59:23Z ellefj $ :)
    (: An example found in Saxon: creates a table with 10x10 cells :)

    declare namespace f="http://my-namespaces.org";

    declare function f:background-color($x as xs:double, $y as xs:integer)
    as xs:string {
        if($x mod 2 + $y mod 2 <= 0) then "lightgreen"
    	else if($y mod 2 <= 0) then "yellow"
    	else if($x mod 2 <= 0) then "lightblue"
    	else "white"
    };

    <body>
    	<table>{
    	for $y in 1 to 10 return
    		<tr>
    		{
    			for $x in 1 to 10 return
    				let $bg := f:background-color($x, $y),
    					$prod := $x * $y
    				return
    					<td bgcolor="{$bg}">
    						{if ($y > 1 and $x > 1) then $prod else <b>{$prod}</b>}
    					</td>
    		}
    		</tr>
    	}</table>
    </body>

### Create a multiplication table

    xquery version "1.0";
    declare option exist:serialize "method=html5 media-type=text/html";
    declare variable $max := 20;

    <table border="1" width="100%">
        <th>1 x 1</th>
        {
            for $i in (1 to $max)
            return
                <th>{ $i}</th>
        }

        {
            for $a in (1 to $max)
            return
                <tr>
                    <th>{ $a,"*"}</th>

                    {
                        for $b in (1 to $max)
                        return
                            if ($a = $b) then
                                <td bgcolor="#F46978">{$a * $b}</td>
                            else if ($a mod 2) then
                                <td bgcolor="#A46978">{$a * $b}</td>
                            else
                                <td bgcolor="#A09224">{$a * $b}</td>
                    }
                </tr>

        }
    </table>

### Generate a list of the acts and scenes in Hamlet, with the roles appearing in each scene

    declare option exist:serialize "method=html5 media-type=text/html";

    <html>
        <body>{
            for $act in doc("/db/apps/demo/data/hamlet.xml")/PLAY/ACT
            return
                <ul>
                    <li>
                        <h2>{$act/TITLE/text()}</h2>
                        <ul>
                        {
                            for $scene in $act/SCENE return
                                <li>
                                    <h3>{$scene/TITLE/text()}</h3>
                                    <ul>
                                    {
                                        for $speaker in distinct-values($scene//SPEAKER)
                                        order by $speaker return
                                            <li>{$speaker}</li>
                                    }
                                    </ul>
                                </li>
                        }
                        </ul>
                    </li>
                </ul>
        }</body>
    </html>