我有一个大的数据表,大致如下。如下所述,这是一个完整的样本,而不是简化。
<table class="grid resultRaceGrid" id="mainGrid">
<tr>
<th width="10"> </th>
<th> </th>
<th> </th>
<th>HORSE/SP</th>
<th>AGE</th>
<th>WGT</th>
<th>TRAINER/JOCKEY</th>
<th>OR</th>
<th>TS</th>
<th>RPR</th>
<th width="10"> </th>
</tr>
<tr>
<td colspan="11" class="separator"> </td>
</tr>
<tr>
<td rowspan="2" class="last"><a href="/horses/result_home.sd?race_id=443557&r_date=2007-11-21&popup=yes" class="bull"><b>«</b></a></td>
<td rowspan="2" class="nowrap noPad"><h3>1 </h3></td>
<td rowspan="2" class="dstDesc"></td>
<td class="nowrap"><span class="black"><a href="#" id="noteIcon_673823" class="pencil" onclick="return false;"><!-- --></a><b><a href="/horses/horse_home.sd?horse_id=673823" onclick="return popup(this, {width:695, height:800})" title="Full details about this HORSE">Andytown</a></b> (IRE) 6/4F <img src="http://ui.racingpost.com/ico/tipping-success.gif" class="shilda" title="Tipped by Topspeed" alt="" /></span></td>
<td class="black">5</td>
<td class="nowrap black"><span>11-1 <span class="lightGray"></span></span></td>
<td class="nowrap black"><a href="/horses/trainer_home.sd?trainer_id=13176" onclick="return popup(this, {width:695, height:800})" title="Full details about this TRAINER">N G Richards</a></td>
<td rowspan="2" class="lightGray">—</td>
<td rowspan="2" class="lightGray"><span class="red bold">*</span></td>
<td rowspan="2" class="last"><span class="red bold">*</span></td>
<td rowspan="2" class="last"><a href="/horses/result_home.sd?race_id=450083&r_date=2008-03-08&popup=yes" class="bull"><b>»</b></a></td>
</tr>
<tr>
<td colspan="3"><span class="pedigrees">ch g <a href="/bloodstock/stallionbook/stallion_home.sd?horse_id=42337&popup=1" onclick="return popup(this, {width:734, height:800})" title="Full details about this STALLION">Old Vic</a> - <a href="/bloodstock/dam_home.sd?horse_id=519458" onclick="return popup(this, {width:695, height:800})" title="Full details about this DAM ">Pitfire (IRE)</a> (<a href="/bloodstock/stallionbook/stallion_home.sd?horse_id=303796&popup=1" onclick="return popup(this, {width:734, height:800})" title="Full details about this STALLION">Parliament</a>)</span></td>
<td class="lightGray"><a href="/horses/jockey_home.sd?jockey_id=82320" onclick="return popup(this, {width:695, height:800})" title="Full details about this JOCKEY">Fearghal Davis</a><sup>5</sup></td>
</tr>
<tr class="rowComment hideComment">
<td colspan="3"> </td>
<td colspan="7"><div class="commentText"> Went prominent 6th, challenged 2 out, soon ridden, narrow advantage last, forged clear towards finish (tchd 7-4)</div></td>
<td> </td>
</tr>
<tr>
<td colspan="11" class="separator"> </td>
</tr>
我有以下XPath,它允许我正确匹配整行感兴趣。
我的问题是:如何选择此tr
以及以下两个tr
元素(跟随它)。
$xpath->query("//table[@id='mainGrid']//tr[descendant::a[contains(@href,'horse_home')]]"
答案 0 :(得分:3)
使用强>:
//table[@id='mainGrid']
//tr[descendant::a
[contains(@href,'horse_home')]
]
|
//table[@id='mainGrid']
//tr[descendant::a
[contains(@href,'horse_home')]
]
/following-sibling::tr[not(position() > 2)]
此XPath表达式优化了您之前的选择,指定应该选择它与最多两个tr
兄弟姐妹(之前选择的tr
元素)之间的联合。
答案 1 :(得分:1)
只是为了好玩,没有两个绝对表达式的结合:
//table[@id='mainGrid']
//tr[(self::tr|preceding-sibling::tr[not(position() > 2)])
[descendant::a
[contains(@href,'horse_home')]
]
]
作为证明,假设这减少了输入样本:
<table id="mainGrid">
<tr>1</tr>
<tr>2</tr>
<tr>3</tr>
<tr>
<a href="/horses/horse_home.sd"/>
</tr>
<tr>5</tr>
<tr>6</tr>
<tr>7</tr>
<tr>
<a href="/horses/horse_home.sd"/>
</tr>
<tr>8</tr>
<tr>9</tr>
<tr>10</tr>
</table>
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="
//table[@id='mainGrid']
//tr[(self::tr|preceding-sibling::tr[not(position() > 2)])
[descendant::a
[contains(@href,'horse_home')]
]
]
"/>
</xsl:template>
</xsl:stylesheet>
输出:
<tr>
<a href="/horses/horse_home.sd" />
</tr>
<tr>5</tr>
<tr>6</tr>
<tr>
<a href="/horses/horse_home.sd" />
</tr>
<tr>8</tr>
<tr>9</tr>
答案 2 :(得分:0)
$xpath->query("//table[@id='mainGrid']//tr[descendant::a[contains(@href,'horse_home')] | //table[@id='mainGrid']//tr[descendant::a[contains(@href,'horse_home')]/following-sibling::tr[position() < 3]"
是一种方式(我认为效率不高)。
使用XPath 2.0(您的PHP API可能不支持),您可以使用$xpath->query("//table[@id='mainGrid']//tr[descendant::a[contains(@href,'horse_home')]/(., following-sibling::tr[position() < 3])
。
答案 3 :(得分:0)
那么你一定要考虑使用Xpath轴。
following-sibling
和previous-sibling
那么你的表达应该是这样的:
$xpath->query("//table[@id='mainGrid']/tr[descendant::a[contains(@href,'horse_home')]/following-sibling::tr[2]")