2014-04-04 36 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