2013-11-04 84 views
0

計算平均我有一個這樣的XML文件:的Xquery的屬性

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<?xml-stylesheet href="class3.xsl" type="text/xsl" ?> 
<school> 
<student><name>Jack T</name> 
<course title="INF8430" note="78" /> 
<course title="INF1030" note="69" /> 
<course title="INF1230" note="85" /></student> 
<student><name>Marty L</name> 
<course title="INF8430" note="95" /> 
<course title="INF1030" note="82" /> 
<course title="INF1230" note="77" /></student> 
<student><name>Ben L</name> 
<course title="INF9430" note="59" /> 
<course title="INF1030" note="78" /> 
<course title="INF1230" note="79" /></student> 
</school> 

在我的XQuery我用這個:

{ 
for $s in distinct-values(doc("class3.xml")//course/@title) 
return 
<course title="{$s}"> 

</course> 

}

我試圖讓每個平均不同的標題。 我得到正確的標題,但我怎麼能算平均? 我嘗試了很多東西之間的課程標籤找到平均,有人可以幫助我 謝謝

回答

2

您return語句中加入這樣的:

avg(doc("class3.xml")//course[@title=$s]/@note) 
+0

太棒了,到底需要什麼。在我之前完成的所有測試中,我錯過了[@ title = s $]的部分,現在我明白了。謝謝馬修 – baronming

1

我敢肯定有更好的辦法...不知道這是你想要的,但它會幫助你到達那裏我希望:)

let $doc := 
<school> 
<student><name>Jack T</name> 
<course title="INF8430" note="78" /> 
<course title="INF1030" note="69" /> 
<course title="INF1230" note="85" /></student> 
<student><name>Marty L</name> 
<course title="INF8430" note="95" /> 
<course title="INF1030" note="82" /> 
<course title="INF1230" note="77" /></student> 
<student><name>Ben L</name> 
<course title="INF9430" note="59" /> 
<course title="INF1030" note="78" /> 
<course title="INF1230" note="79" /></student> 
</school> 

return 
    for $s in $doc//student 
    let $total := sum($s/course/@note) div count($s/course) 
    return <name>{$s/name}<avg>{$total}</avg></name> 
+0

感謝埃裏克,但它不正是我要找的。好的,你可以看到我的XML代碼以及我用於XQuery的代碼,我希望結果如下: average = 86.5(其中86.5來自78 + 95/2)。因此,我已經獲得了我的獨特課程:對於不同價值的$ s(doc(「class3.xml」)// course/@ title),但是我很難用$ s到達屬性註釋。也許存在兩種方法。謝謝 – baronming