我有一个包含这些数据的XML文件:
<?xml version="1.0" encoding="iso-8859-1" ?>
<university>
<lecture>
<class>English</class>
<hours>3</hours>
<pupils>30</pupils>
</lecture>
<lecture>
<class>Math</class>
<hours>4</hours>
<pupils>27</pupils
</lecture>
<lecture>
<class>Science</class>
<hours>2</hours>
<pupils>25</pupils>
</lecture>
</university>
是否有可能获得一个XQuery代码为我生成此输出(下面)?
<table>
<tr>
<td>English class runs for 3 with 30 pupils</td>
<td>Math class runs for 4 with 26 pupils</td>
<td>Science class runs for 2 with 25 pupils</td>
</tr>
</table>
编辑:以下是我的尝试:
let $classroom := doc("uni.xml")/university/lecture/class
let $hr := doc("uni.xml")/university/lecture/hours
let $pupl := doc("uni.xml")/university/lecture/pupils
<tr>
<table>
for $a in $classroom,
$b in $hr,
$c in $pupl
return <td>{$a} class runs for {$b} with {$c} pupils</td>
</tr>
</table>
我收到一条错误消息,说我需要返回一个已经执行过的值。如果我取出tr和table标签,它可以工作,但给我一个无限循环的结果。
答案 0 :(得分:1)
响应更新后的XML,以及使用变量的一些尝试过的XQuery样式,我建议这样:
<table>
<tr>
{
for $lecture in doc("uni.xml")/university/lecture
let $class := $lecture/class/string()
let $hours := $lecture/hours/string()
let $pupils := $lecture/pupils/string()
return
<td>{$class} class runs for {$hours} with {$pupils} pupils</td>
}
</tr>
</table>
演示:http://www.xpathtester.com/xquery/33e31b342098712e331285bda3010e0c
基本上,查询循环包含所有必需信息的公共父元素; lecture
个元素。然后使用简单的相对XPath / XQuery从每个lecture
中提取数据以生成所需的输出。