这个 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, '
')