这是DTD
<!ELEMENT Responses (Student*)>
<!ELEMENT Student (Response+)>
<!ATTLIST Student studentID ID #REQUIRED>
<!ELEMENT Response (List+)>
<!ELEMENT List (#PCDATA)>
<!ATTLIST List questionID IDREF #REQUIRED>
我想要大多数学生所在的大学。如果有领带,请报告所有并列的大学。
let $student := doc("responses.xml")/Responses/Student
for $dc in distinct-values
(for $college in $student/Response/List[@questionID = "college"]
return data($college))
return <College>{$dc} - number of students: {count($student[Response/List[@questionID = "college"] = data($dc)])</College>)
我在count()时遇到错误,我只想用条件计算。
第二个问题如何选择最大值
答案 0 :(得分:1)
代码的最后一行有一个拼写错误。在计数之前你有一个开放的花括号,但它在</College>
之前没有关闭,并且在</College>
之后还有一个额外的右括号。
要获得与大多数学生相关的大学(由?参加),您需要在计数上订购,并获取以该方式返回的第一个项目。请参阅下面稍作修改的代码。
注意:我不得不猜测一下你的数据,所以我自己创造了一些东西。事实上,实际上有三所大学具有相同的最大值,但只返回一个。你可能想稍微调整一下,首先找到最大数量本身,然后使用它来查找所有具有该数量的大学...
let $doc :=
document {
<Responses>
<Student studentID="s1">
<Response>
<List questionID="college">q1</List>
<List questionID="college">q2</List>
<List questionID="college">q3</List>
</Response>
<Response>
<List questionID="college">q4</List>
<List questionID="college">q5</List>
<List questionID="college">q6</List>
</Response>
</Student>
<Student studentID="s2">
<Response>
<List questionID="college">q4</List>
<List questionID="college">q5</List>
<List questionID="college">q6</List>
</Response>
<Response>
<List questionID="college">q7</List>
<List questionID="college">q8</List>
<List questionID="college">q9</List>
</Response>
</Student>
<Student studentID="s3">
<Response>
<List questionID="college">q1</List>
<List questionID="college">q2</List>
<List questionID="college">q3</List>
</Response>
<Response>
<List questionID="college">q4</List>
<List questionID="college">q5</List>
<List questionID="college">q6</List>
</Response>
</Student>
</Responses>
}
let $student := $doc/Responses/Student
return (
for $dc in
distinct-values(
for $college in $student/Response/List[@questionID = "college"]
return data($college)
)
let $count := count($student[Response/List[@questionID = "college"] = data($dc)])
order by $count descending, $dc ascending
return <College>{$dc} - number of students: {$count}</College>
)[1]