Here is the DTD
<!ELEMENT Responses (Student*)>
<!ELEMENT Student (Response+)>
<!ATTLIST Student studentID ID #REQUIRED>
<!ELEMENT Response (List+)>
<!ELEMENT List (#PCDATA)>
<!ATTLIST List questionID IDREF #REQUIRED>
I want the college that most students are in. If there is a tie, report all colleges that tied.
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>)
I am getting error with count(), I just want to count with a condition.
2nd question how do I select the maximum ones
There is a typo in the last line of your code. You have an open curly brace just before count, but it is not closed before
</College>, and there is an extra closing parenthesis after</College>.To get the college that is associated with (attended by?) the most students, you need to order on the count, and take the first item that is returned that way. See the slightly altered code below.
Note: I had to guess about your data a bit, so I created something myself. It is such that there are actually three colleges with same max, but only one is returned. You may want to tweak a bit, by finding the max count itself first, and use that to locate all colleges with that count afterwards..