2011-12-07 86 views
0

這裏的Xquery計數是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>) 

我得到錯誤與計數(),我只想計算一個條件。

第二個問題我該如何選擇最高者

+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]