我被一个看似简单的 XQuery 练习困住了。下面是一段 XML 文件。
<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>
我正在尝试将 XML 转换为带有类名称属性 (Kongo) 作为标题和子船名称和发射年份的枚举列表的 XHTML:
<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>
....
我坚持使用以下嵌套的 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>