2010-11-27 23 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を使用すると、関心のある行全体を正しく一致させることができます。

私の質問はです:どのように私は(それ以下)には、次の2つの要素trと一緒にこのtrを選択することができます。

+1

@ gordon。コードサンプルが更新されました。 –

+0

※ご質問は一切ありません!また、「次の2つの行と一致させたい」という文もあります。意味がありません。どのような2つの行ですか?どの2つの行?あなたは遭遇した問題は何ですか?どうか明らかにしてください。 –

答えて

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要素の次の兄弟を選択する必要があります。

+1

が優れています。私は元のtrマッチを指定せずに逃げることができると思った。これは絶対に完璧であり、非常に高く評価されています。 –

+2

私は毎日何か新しいことを学びます。ありがとう:) –

+1

@esryl:それは、ユニオンを必要とせずにコンパクトな表現のほんの一例ですが、残念ながらXPathには 'following-sibling-or-self ::'軸がありません。 –

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

honnen、コードサンプルを更新しました。あなたのコードは、次の行の一致にはうまくいきますが、一致した行も返されません。 –

0

次に、Xpath軸の使用を検討する必要があります。 following-siblingprevious-sibling

あなたの表現そして、このようなものになります。

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

私はXPath軸を使って遊んでいますが、自分や兄弟を返すことはできません。 –

+1

@esryl:あなたが使用したコード例では、// table // trを参照しています。注意:あなたが基本的に言っているのは、どこでもintテーブルのどこかで発生しています。いい考えではない。 –

+0

右、軸のいい使い方。一致する行をループすると、次の兄弟を使用して元のノードに結び付けられた別のクエリが実行されます。トリックをやっている。あなたが実際に私に導いた方法は、私にはるかにコントロールを与えます。また、// –

1

楽しみのためだけに、2つの絶対式の労働組合なし:証拠として

//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> 
+0

@Alejandro:いい答え、+1。しかし、あなたはまだ組合があることに注意してください。また、 'preceding-sibling :: tr'ではなく' following-sibling :: tr'でなければなりません。 –

+0

@Dimitre:そうです。だから、私は "二つの絶対的な表現**の結合"と言ってきた。 'href'属性の' horse_home 'を保持している 'a'という子孫を持っています。 –

+0

@Alejandro:この表現は、少なくとも1つが述語を満たす3つの 'tr'兄弟を選択しています。これはOPが望むものではありません。彼は正確に最初の 'tr'が述語を満たし、次の2つの兄弟が述語を満たすかどうかは気にしないことを望んでいます。 –