Reputation: 730
h3
tag containing the date of the Stoke City vs. Crystal palace match? (e.g. Saturday 4 October)HTML snippet listing 4 (out of the remaining 321) English Premier League Football fixtures for the 2014/15 season
<div class="fixtures">
<h3>Monday 29 September</h3>
<dl class="matches">
<dt class="match">
<span class="match-time">20:00</span>
<span class="home-side">
<span>
<img src="http://omo.akamai.opta.net/image.php?&sport=football&entity=team&description=badges&dimensions=20&id=110" alt="Stoke City">
</span>
<a href="http://www.dailymail.co.uk/sport/teampages/stoke-city.html">Stoke City</a>
</span>
<span>
<span> </span>
<span>vs</span>
<span> </span>
</span>
<span class="away-side">
<span>
<img src="http://omo.akamai.opta.net/image.php?&sport=football&entity=team&description=badges&dimensions=20&id=4" alt="Newcastle United">
</span>
<a href="http://www.dailymail.co.uk/sport/teampages/newcastle-united.html">Newcastle United</a>
</span>
</dt>
</dl>
<dl class="matches">
<dt class="match">
<span class="match-time">15:00</span>
<span class="home-side">
<span>
<img src="http://omo.akamai.opta.net/image.php?&sport=football&entity=team&description=badges&dimensions=20&id=13" alt="Leicester City">
</span>
<a href="http://www.dailymail.co.uk/sport/teampages/leicester.html">Leicester City</a>
</span>
<span>
<span> </span>
<span>vs</span>
<span> </span>
</span>
<span class="away-side">
<span>
<img src="http://omo.akamai.opta.net/image.php?&sport=football&entity=team&description=badges&dimensions=20&id=90" alt="Burnley">
</span>
<a href="http://www.dailymail.co.uk/sport/teampages/burnley.html">Burnley</a>
</span>
</dt>
</dl>
<h3>Saturday 4 October</h3>
<dl class="matches">
<dt class="match">
<span class="match-time">15:00</span>
<span class="home-side">
<span>
<img src="http://omo.akamai.opta.net/image.php?&sport=football&entity=team&description=badges&dimensions=20&id=14" alt="Liverpool">
</span>
<a href="http://www.dailymail.co.uk/sport/teampages/liverpool.html">Liverpool</a>
</span>
<span>
<span> </span>
<span>vs</span>
<span> </span>
</span>
<span class="away-side">
<span>
<img src="http://omo.akamai.opta.net/image.php?&sport=football&entity=team&description=badges&dimensions=20&id=35" alt="West Bromwich Albion">
</span>
<a href="http://www.dailymail.co.uk/sport/teampages/west-bromwich-albion.html">West Bromwich Albion</a>
</span>
</dt>
</dl>
<dl class="matches">
<dt class="match">
<span class="match-time">15:00</span>
<span class="home-side">
<span>
<img src="http://omo.akamai.opta.net/image.php?&sport=football&entity=team&description=badges&dimensions=20&id=110" alt="Stoke City">
</span>
<a href="http://www.dailymail.co.uk/sport/teampages/stoke-city.html">Stoke City</a>
</span>
<span>
<span> </span>
<span>vs</span>
<span> </span>
</span>
<span class="away-side">
<span>
<img src="http://omo.akamai.opta.net/image.php?&sport=football&entity=team&description=badges&dimensions=20&id=31" alt="Crystal Palace">
</span>
<a href="http://www.dailymail.co.uk/sport/teampages/crystal-palace.html">Crystal Palace</a>
</span>
</dt>
</dl>
</div>
Upvotes: 2
Views: 655
Reputation: 89295
You can try this XPath :
//h3[following-sibling::dl[1][.//span[contains(concat(' ', normalize-space(@class), ' '), ' home-side ') and span/img[@alt='Hull City']]]]
Basically, above XPath select <h3>
element having next sibling <dl>
element containing a <span class="home-side">
and another <span>
with <img alt="Hull City">
(formatted version) :
//h3[
following-sibling::dl[1][
.//span[
contains(concat(' ', normalize-space(@class), ' '), ' home-side ')
and
span/img[@alt='Hull City']
]
]
]
UPDATE :
Following is an XPath example that checks for both home team and away team :
//h3[
following-sibling::dl[1][
.//span[
contains(concat(' ', normalize-space(@class), ' '), ' home-side ')
and
span/img[@alt='Hull City']
]
and
.//span[
contains(concat(' ', normalize-space(@class), ' '), ' away-side ')
and
span/img[@alt='Crystal Palace']
]
]
]
UPDATE 2 :
To be able to account multiple <dl>
s, I think it will be easier to find <dl>
that satisfies home and away team criteria first, then move backward to find closest <h3>
element from such <dl>
:
//dl[
.//span[
contains(concat(' ', normalize-space(@class), ' '), ' home-side ')
and
span/img[@alt='Stoke City']
]
and
.//span[
contains(concat(' ', normalize-space(@class), ' '), ' away-side ')
and
span/img[@alt='Crystal Palace']
]
]/preceding-sibling::h3[1]
Upvotes: 1
Reputation: 41875
If the structure is always going to be the same, you could point it first to that img tag with that alt value, then traverse it backwards.
Example:
$dom = new DOMDocument();
$dom->loadHTML($markup);
$xpath = new DOMXpath($dom);
$needle = 'Hull City';
$element = $xpath->query("//span/img[contains(@alt, '$needle')]");
if($element->length > 0) {
$img = $element->item(0);
$header = $xpath->query('ancestor::node()/preceding-sibling::h3[1]', $img);
if($header->length > 0) {
echo $header->item(0)->nodeValue; // Saturday 4 October
}
}
Upvotes: 1