I'm stuck with a seemingly simple XQuery exercise. Below a piece of the XML file.
<Ships>
<Class name = "Kongo" type = "bc" country = "Japan" numGuns = "8"
bore = "14" displacement = "32000">
<Ship name = "Kongo" launched = "1913" />
<Ship name = "Hiei" launched = "1914" />
<Ship name = "Kirishima" launched = "1915">
<Battle outcome = "sunk">Guadalcanal</Battle>
</Ship>
<Ship name = "Haruna" launched = "1915" />
</Class>
</Ships>
I'm trying to transform the XML into an XHTML with the Class name attributes (Kongo) as header and an enumerated list of the child ship names and launch-years:
<h1>Kongo</h1>
<table>
<tr><th>Name</th><th>Launched</th><tr>
<tr><td>Kongo</td><td>1913</td></tr>
<tr><td>Hiei</td><td>1914</td></tr>
<tr><td>Kirishima</td><td>1915</td></tr>
<tr><td>Haruna</td><td>1915</td></tr>
<h1>Next Class name</h1>
....
I'm stuck with the following nested return-FLOWR XQuery:
declare option output "method=xml";
declare option output "doctype-public=-//W3C//DTD XHTML 1.0 Strict//EN";
declare option output "doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
declare option output "omit-xml-declaration=no";
declare option output "indent=yes";
<html xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XQ-7</title>
</head>
<body>
{for $class in doc("xml/battleships.xml")/Ships/Class
let $cname := data($class/@name)
let $sname := data($class/Ship/@name)
let $slaunched := data($class/Ship/@launched)
return
<h1>{$cname}</h1>
<table>
{for $ship in doc("xmlbattleships.xml")/Ships/Class/Ship
where data($ship/../@name) eq $cname
let $sname := data($ship/@name)
let $slaunched := data($ship/@launched)
return
<tr><td>{$sname}</td><td>{$slaunched}</td></tr>
}
</table>
}
</body>
</html>