带有条件的Xquery计数

带有条件的Xquery计数,xquery,Xquery,这是DTD <!ELEMENT Responses (Student*)> <!ELEMENT Student (Response+)> <!ATTLIST Student studentID ID #REQUIRED> <!ELEMENT Response (List+)> <!ELEMENT List (#PCDATA)> <!ATTLIST List questionID IDREF #RE

这是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>)
let$student:=doc(“responses.xml”)/responses/student
以不同的值计算$dc
(对于$student/Response/List中的$college[@questionID=“college”]
返回数据($college))
return{$dc}-学生人数:{count($student[Response/List[@questionID=“college”]=data($dc)])
我在count()中遇到错误,我只想使用一个条件进行计数


第二个问题如何选择最大值?

您的代码最后一行有一个输入错误。您在计数之前有一个开放的大括号,但在
之前它没有闭合,在
之后还有一个额外的闭合括号

要获得与大多数学生相关联的学院(入学人数?),您需要按数量订购,并获取第一个以这种方式返回的项目。请参阅下面稍加修改的代码

注意:我不得不猜测一下你的数据,所以我自己创建了一些东西。事实上,有三个学院的最大值相同,但只返回一个。你可以先找到最大值本身,然后用它来定位所有具有该值的学院

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$doc:=
文件{
q1
问题2
第三季度
第四季度
问题5
问题6
第四季度
问题5
问题6
问题7
问题8
问题9
q1
问题2
第三季度
第四季度
问题5
问题6
}

let$student:=$doc/Responses/student
返回(
港币$dc
不同的价值观(
对于$student/Response/List中的$college[@questionID=“college”]
返回数据($college)
)
让$count:=count($student[应答/列表[@questionID=“college”]=data($dc)])
按$count降序,$dc升序排序
return{$dc}-学生人数:{$count}
)[1]

代码的最后一行有一个输入错误。在计数之前有一个开放的大括号,但在
之前它没有闭合,在
之后还有一个额外的闭合括号

要获得与大多数学生相关联的学院(入学人数?),您需要按数量订购,并获取第一个以这种方式返回的项目。请参阅下面稍加修改的代码

注意:我不得不猜测一下你的数据,所以我自己创建了一些东西。事实上,有三个学院的最大值相同,但只返回一个。你可以先找到最大值本身,然后用它来定位所有具有该值的学院

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$doc:=
文件{
q1
问题2
第三季度
第四季度
问题5
问题6
第四季度
问题5
问题6
问题7
问题8
问题9
q1
问题2
第三季度
第四季度
问题5
问题6
}

let$student:=$doc/Responses/student
返回(
港币$dc
不同的价值观(
对于$student/Response/List中的$college[@questionID=“college”]
返回数据($college)
)
让$count:=count($student[应答/列表[@questionID=“college”]=data($dc)])
按$count降序,$dc升序排序
return{$dc}-学生人数:{$count}
)[1]

您收到了什么错误消息?(我确实看到您缺少一个右大括号,并且在末尾有一个任性的右括号。)您应该提供一些示例数据,因为您的元素名称不直观。例如,学院是如何表示的?您收到了什么错误消息?(我确实看到您缺少一个右大括号,并且在末尾有一个任性的右括号。)您应该提供一些示例数据,因为您的元素名称不直观。例如,学院是如何表示的?