2014-04-04 35 views
0

如何使用xquery返回某個索引處的值?例如,如果這是我的xml文件:使用xquery獲取索引值?

<game> 
    <team> 
     <player>id="1"</player> 
     <player>id="2"</player> 
     </team> 
    <team> 
     <player>id="3"</player> 
     <player>id="4"</player> 
     </team> 
    <team> 
     <player>id="5"</player> 
     <player>id="6"</player> 
     </team> 
    </game> 

說我要回各隊的第一個玩家ID,這樣我就得到1,3,5,如何將我寫這篇文章的查詢?我試過這樣的東西,但它不起作用:

for $team in doc("game.xml")//team 
return data($team/player/@id[1]) 

任何幫助,將不勝感激。謝謝。

+0

嘗試應'id's會有玩家標籤作爲屬性的一部分? –

回答

0

試試這個

//team/player[1]/text() 

Id是節點的文本,而不是一個屬性。

你可以在這裏 http://www.unit-testing.net/Xpath

+0

以下是xquery版本:doc(「http://dl.dropboxusercontent.com/u/1487285/game.xml」)// team/player [1]/text() – wcandillon

+2

您可以放心地刪除'/ text( )'從上面。最好使用string()來獲取元素的字符串值而不是text(),因爲如果有評論,您的代碼不會中斷。 –

+0

@MichaelKay Intersting。我不認爲我曾經使用過string()函數。語法會是什麼樣子? – TGH