2010-11-27 2 views
3

У меня есть большая таблица данных, грубо изложенная следующим образом. В соответствии с просьбой ниже приведена полная выборка, а не упрощение.Элемент выбора XPath и следующий элемент

<table class="grid resultRaceGrid" id="mainGrid"> 
<tr> 
    <th width="10">&nbsp;</th> 
    <th>&nbsp;</th> 
    <th>&nbsp;</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">&nbsp;</th> 
</tr> 
<tr> 
    <td colspan="11" class="separator">&nbsp;</td> 
</tr> 
<tr> 
    <td rowspan="2" class="last"><a href="/horses/result_home.sd?race_id=443557&amp;r_date=2007-11-21&amp;popup=yes" class="bull"><b>&laquo;</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&nbsp;<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">&mdash;</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&amp;r_date=2008-03-08&amp;popup=yes" class="bull"><b>&raquo;</b></a></td> 
</tr> 
<tr> 
    <td colspan="3"><span class="pedigrees">ch g <a href="/bloodstock/stallionbook/stallion_home.sd?horse_id=42337&amp;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&amp;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">&nbsp;</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>&nbsp;</td> 
</tr> 
<tr> 
    <td colspan="11" class="separator">&nbsp;</td> 
</tr> 

У меня есть следующий XPath, который позволяет мне правильно соответствовать целой строке интереса.

Мой вопрос: как я могу выбрать этот tr вместе со следующими двумя tr элементами (после него).

$xpath->query("//table[@id='mainGrid']//tr[descendant::a[contains(@href,'horse_home')]]" 
+1

@gordon. обновлен образец кода. – 2010-11-27 18:57:53

+0

есть * вопрос * совсем нет! Кроме того, предложение «Я также хочу сопоставить следующие две строки». не имеет никакого смысла. Какие два ряда? Какие две строки? В чем проблема, с которой вы столкнулись? Просьба уточнить. – 2010-11-27 19:46:21

ответ

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).

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]).

0

Тогда вы должны обязательно рассмотреть использование осей Xpath. following-sibling и previous-sibling

Ваше выражение тогда, должно выглядеть примерно так:

$xpath->query("//table[@id='mainGrid']/tr[descendant::a[contains(@href,'horse_home')]/following-sibling::tr[2]") 
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> 
Смежные вопросы