2013-06-19 52 views
0

因爲我無法註冊然而,與XPath和命名空間的另一個問題

http://www.spotify.com/ns/music/1 

命名空間我用下面

/*[name()='tracks']/*[name()='track']/*[name()='name'] 

串檢索曲目名稱。問題是我只想要第一個軌道名稱,是否有任何解決方法只選擇第一個軌道塊?

<tracks xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1"> 
<opensearch:Query role="request" startPage="1" searchTerms="hypo disco"/> 
<opensearch:totalResults>2</opensearch:totalResults> 
<opensearch:startIndex>0</opensearch:startIndex> 
<opensearch:itemsPerPage>100</opensearch:itemsPerPage> 
<track href="spotify:track:5uYWzsjWBui67Au7AwfLMC"> 
<name>Killed Banano - Disco Version</name> 
<artist href="spotify:artist:2XtRg7z3aq8UsX1fstLJKM"> 
<name>Hypo</name> 
</artist> 
<id type="isrc">FR6V80402639</id> 
<album href="spotify:album:2r2lvkRFTGXDXfNxUslgv7"> 
<name>Random Veneziano</name> 
<released>2005</released> 
<availability> 
<territories> 
AD AE AF AG AI AL AM AN AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BM BN BO BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IN IO IQ IR IS IT JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR ST SV SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW ZZ 
</territories> 
</availability> 
</album> 
<track-number>19</track-number> 
<length>130.044000</length> 
<popularity>0.02090</popularity> 
</track> 
<track href="spotify:track:2w2d0WyMqLwMEGZRGMiBDS"> 
<name>New Disco</name> 
<artist href="spotify:artist:5ircLG6gSoS34Dh6AWNc6g"> 
<name>Estevon</name> 
</artist> 
<id type="isrc">ushm21309193</id> 
<album href="spotify:album:0AGlfBGzKfBLE9fHU6eOHc"> 
<name>Estevon Presents Hypo-Hybrid</name> 
<released>2013</released> 
<availability> 
<territories>worldwide</territories> 
</availability> 
</album> 
<track-number>13</track-number> 
<length>248.847000</length> 
<popularity>0.00000</popularity> 
</track> 
</tracks> 

回答

0

如果你只想要第一個曲目名稱,那麼這應該工作:

(/*[local-name()='tracks']/*[local-name()='track']/*[local-name()='name'])[1] 

此外,如果你想忽略的命名空間,我建議你使用local-name(),而不只是name()。對於你的情況,它的工作原理是一樣的,但如果有一個命名空間前綴,name()將返回它,而local-name()永遠不會。

+0

你也可以通配名字空間,而不是使用謂詞,我認爲這看起來很整潔:'/ *:tracks/*:track [1]/*:name' –