2012-08-03 31 views
3

我有這個表的源代碼HERE獲取從表的具體數據使用XPath

enter image description here

我想所有的行,我至極可以用做:

enter image description here

使用string-join($doc//*[@id='salaries']/tbody/tr/normalize-space(.), '
')的預期最終輸出是:

1985-86 Los Angeles Lakers NBA $2,030,000 
1987-88 Los Angeles Lakers NBA $2,000,000 
1988-89 Los Angeles Lakers NBA $3,000,000 

我的問題是,如何從最終輸出中刪除第三列(在這個例子中入選NBA)得到這個:

1985-86 Los Angeles Lakers $2,030,000 
1987-88 Los Angeles Lakers $2,000,000 
1988-89 Los Angeles Lakers $3,000,000 

PS:我不知道該列始終在那個地方,但固定包含在它的聯賽a[contains(@href, 'league')]

+2

而非圖片,你能顯示源? – choroba 2012-08-03 09:56:31

+0

@choroba,是的,我忘了它xD ...發佈更新:) – Enissay 2012-08-03 10:08:57

回答

2

此XPath 2.0表達式

for $i in 1 to count(/tbody/tr), 
     $r in /tbody/tr[$i], 
     $s in string-join($r/td[not(position() eq 3)]/normalize-space(.), ' ') 
    return 
    concat($s, '
') 

所提供的XML文檔評價時:

<tbody> 
<tr class="" data-row="0"> 
    <td align="left">1985-86</td> 
    <td align="left"><a href="/teams/LAL/1986.html">Los Angeles Lakers</a></td> 
    <td align="left"><a href="/leagues/NBA_1986.html">NBA</a></td> 
    <td align="right" csk="2030000">$2,030,000</td> 
</tr> 
<tr class="" data-row="1"> 
    <td align="left">1987-88</td> 
    <td align="left"><a href="/teams/LAL/1988.html">Los Angeles Lakers</a></td> 
    <td align="left"><a href="/leagues/NBA_1988.html">NBA</a></td> 
    <td align="right" csk="2000000">$2,000,000</td> 
</tr> 
<tr class="" data-row="2"> 
    <td align="left">1988-89</td> 
    <td align="left"><a href="/teams/LAL/1989.html">Los Angeles Lakers</a></td> 
    <td align="left"><a href="/leagues/NBA_1989.html">NBA</a></td> 
    <td align="right" csk="3000000">$3,000,000</td> 
</tr> 
</tbody> 

產生想要的,正確的結果:

1985-86 Los Angeles Lakers $2,030,000 
1987-88 Los Angeles Lakers $2,000,000 
1988-89 Los Angeles Lakers $3,000,000 

如果要排除的列的位置是不能保證是固定的,使用

for $i in 1 to count(/tbody/tr), 
     $r in /tbody/tr[$i], 
     $s in string-join($r/td[not(starts-with(a/@href,'/leagues'))] 
           /normalize-space(.), ' ') 
    return 
    concat($s, '&#xA;') 
+0

Awsome,我從來沒有見過一個像這:o ...像平常一樣完美:) – Enissay 2012-08-03 13:18:51

+0

@ Enissay:不客氣。我很好奇 - 你的平臺是什麼?您使用哪種特定的XPath 2.0處理器以及使用哪種主機語言? – 2012-08-03 13:20:11

+0

那麼,正如我在我以前的問題中所說的,我在Web-Harvest的腳本中使用了這個...以下是一些示例:http://web-harvest.sourceforge.net/samples.php ...我希望這個答案您的問題:) – Enissay 2012-08-03 13:51:18

2

要exlude第三列,使用

tbody/tr/td[position()!=3] 

要exlude含有的鏈接,你可以使用

tbody/tr/td[not(contains(a/@href,'league'))] 
+0

嗯,我已經嘗試過,但我無法設法得到上面的最終輸出:/ – Enissay 2012-08-03 10:36:38