user2140852
user2140852

Reputation: 5

Use Xpath To Retrieve Elements

HTML Portion:

<div class="abc">
    <div style="text-align:left; itemscopr itemtype="xyz">
        <h1 itemtype="mno"> I want this text </h1>
    </div>
</div>

I am using

$text = $xpath->query('//div[class="abc"]/div/h1]

but I am getting no value. Please help me as I am new to it.

Upvotes: 0

Views: 119

Answers (1)

Petr Janeček
Petr Janeček

Reputation: 38444

You should try

//div[@class="abc"]/div/h1

The difference is in the @ sign before class, because the attribute axis is accessed this way. When you omit the @ sign, it looks for node names (tag names).

This returns you the whole h1 node (or, rather, a node-set containing all the matching h1 nodes).

If you only wanted the text from the element, try the evaluate function instead:

$text = $xpath->evaluate("//div[@class='abc']/div/h1/text()")

Upvotes: 1

Related Questions