我想使用给定的输出生成xquery。 我的XML如下:
<?xml version="1.0" encoding="UTF-8"?>
<S_db>
<student id="94811">
<name>Rusty Nail</name>
<quizzes>
<quiz>16</quiz>
<quiz>12</quiz>
</quizzes>
<projects>
<project>44</project>
<project>52</project>
</projects>
<exams>
<exam>77</exam>
<exam>68</exam>
<exam>49</exam>
</exams>
</student>
<student id="2562">
<name>Guy Wire</name>
<quizzes>
<quiz>15</quiz>
<quiz>23</quiz>
</quizzes>
<projects>
<project>33</project>
<project>47</project>
</projects>
<exams>
<exam>78</exam>
<exam>86</exam>
<exam>88</exam>
</exams>
</student>
<student id="137745">
<name>Barb Wire</name>
<quizzes>
<quiz>20</quiz>
<quiz>25</quiz>
</quizzes>
<projects>
<project>48</project>
<project>60</project>
</projects>
<exams>
<exam>38</exam>
<exam>48</exam>
<exam>66</exam>
</exams>
</student>
</S_db>
要生成的输出如下:
<?xml version="1.0" encoding="UTF‑8"?>
<projects>
<student>
<name>Barb Wire</name>
<total>108</total>
</student>
<student>
<name>Guy Wire</name>
<total>80</total>
</student>
<student>
<name>Rusty Nail</name>
<total>96</total>
</student>
我尝试按如下方式生成Xquery:
<student>
{for $i in (1 to 3) return
<total> { for $a in /S_db
let $s := sum($a/student[$i]/projects/project)
let $name := $a/student[$i]/name
return ($s)}
</total>
}
</student>
我当前的输出:
<?xml version="1.0" encoding="UTF-8"?>
<student>
<total>96</total>
<total>80</total>
<total>108</total>
</student>
我不知道如何使用相应的总标记获取该名称标记。 我已将特定学生的name元素存储在名称变量
中答案 0 :(得分:1)
这是一种可能的方式:
<projects>
{
for $s in /S_db/student
return
<student>
{
$s/name,
<total>{sum($s/projects/project)}</total>
}
</student>
}
</projects>
<强> xpathtester.com demo
强>
输出
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<student>
<name>Rusty Nail</name>
<total>96</total>
</student>
<student>
<name>Guy Wire</name>
<total>80</total>
</student>
<student>
<name>Barb Wire</name>
<total>108</total>
</student>
</projects>