Jimmi mile
Jimmi mile

Reputation: 23

Xpath get text content from multiple tags

I have this HTML template:

<ul>
<li>
<div>
<span class="field_full"><strong>Title 1</strong></span> :
<span itemprop="alternativeHeadline">
<span itemprop="alternativeHeadline">
DESC 1
</span>
</span></div>
</li>
<li>
<div>
<span class="field_full"><strong>Title 2</strong></span> :
<span itemscope="" itemtype="http://schema.org/type2" itemprop="type2">
<a href="/"><span itemprop="name">DESC 2</span></a>
</span>
</div>
</li>
<li>
<div>
<span class="field_full"><strong> Title 3</strong></span>:
<span itemprop="type3" itemscope="" itemtype="http://schema.org/type3">
<a href="/"><span itemprop="name">DESC 3-1</span></a>, <a href="/"><span itemprop="name">DESC 3-2</span></a>, <a href="/"><span itemprop="name">DESC 3-3</span></a>
</span>
</div>
</li>
<li>
<span class="field_full"><strong>Title 4</strong></span>:
<span> <a href="/">DESC 4</a></span>
</li>
<li>
<span class="field_full"><strong>Title 5</strong></span>:
<span itemprop="type">
<a href="/">DESC 5-1</a>, <a href="/">DESC 5-2</a>
</span>
</li>
<li>
<span class="field_full"><strong>Title 6</strong></span>:
<span itemprop="type">
DESC 6
</span>
</li>
<li>
<span class="field_full"><strong>Title 7</strong></span>:
<span itemprop="type">
DESC 7
</span>
</li>
<li>
<span class="field_full"><strong>Title 8</strong></span>:
<span itemprop="type">
<a href="/">DESC 8</a>
</span>
</li>
</ul>

I want to use xpath to get this expected result:

TITLE 1 = DESC 1
TITLE 2 = DESC 2
TITLE 3 = DESC 3-1, DESC 3-2, DESC 3-3
TITLE 4 = DESC 4
TITLE 5 = DESC 5-1, DESC 5-2
TITLE 6 = DESC 6
TITLE 7 = DESC 7
TITLE 8 = DESC 8

What i have tried ?

$dom = new DOMDocument();
$dom->loadHTML($html_string);
$xpath = new DOMXpath($dom);

$elements = $xpath->query("//span[@class='field_full']");
foreach($elements as $e) {
    echo $e->nodeValue . '<br/>';
}

But unfortunatelly this return only TITLE 1, TITLE 2, TITLE 3 etc.

I want get their respective values (In this case DESC 1, DESC 2 etc ...).

What is the approch i can take to acheive this goal ?

Thank's

Upvotes: 2

Views: 406

Answers (2)

Nick
Nick

Reputation: 147146

To get the exact result you want, you can use a relative XPath query, using the original <span> node as the root:

$elements = $xpath->query("//span[@class='field_full']");
foreach($elements as $e) {
    echo trim($e->nodeValue) . ' = ';
    $spans = $xpath->query("following-sibling::span", $e);
    foreach ($spans as $span) echo " " . trim($span->nodeValue);
    echo "<br/>";
}

Output:

Title 1 =  DESC 1<br/>
Title 2 =  DESC 2<br/>
Title 3 =  DESC 3-1, DESC 3-2, DESC 3-3<br/>
Title 4 =  DESC 4<br/>
Title 5 =  DESC 5-1, DESC 5-2<br/>
Title 6 =  DESC 6<br/>
Title 7 =  DESC 7<br/>
Title 8 =  DESC 8<br/>

Demo on 3v4l.org

Upvotes: 1

Jeto
Jeto

Reputation: 14927

The following expression should do it:

//span[@class="field_full"]/following-sibling::span

Demo: https://3v4l.org/rTmq9

Upvotes: 0

Related Questions