0

I've been working on some practice application involving xPaths and Retrieving elements from other website.

I used DomXpath for it but it is not returning a result or nodelist.

Here's the code:

$DOM = new DOMDocument();
@$DOM->loadHTML($html);
$xpath = new DOMXPath($DOM);
$nodes = $xpath->query("//span[contains(@style,'font-size:25px;')]");
foreach ($nodes as $node) {
    echo $node->nodeValue;
}

The page source of the example:

    <div class="front-view-content full-post">
    <p>
      <span style="font-size:25px; color:#98293D;">RED</span><br>
      <span style="font-size:25px; color:#98293D;">BLUE</span><br>
      <span style="font-size:25px; color:#98293D;">WHITE</span></p>
    </div>

it doesn't return anything just a plain blank.

Gerard Santos
  • 316
  • 1
  • 5
  • 12

2 Answers2

0

There is no semicolon ; in the source, so xpath doesn't match.

$nodes = $finder->query('//span[@style="font-size:25px"]');

Should work

Alex Blex
  • 34,704
  • 7
  • 48
  • 75
0

Trying to match attributes that contain a certain value is a little more complicated than just doing [@style="your search string"], as this will only match a style attribute that exactly matches your search string.

To my knowledge there's no shorthand selectors in xpath, similar to the ones in CSS for instance, that allows you to do [@style*="your search string"] or [@style~="your search string"], etc.

To test if a string contains another string, you use the contains() function. You're example xpath query would then have to be transformed to:

//span[contains(@style,"font-size:25px;")]

Be aware though that matching isolated strings, at the word boundary if you will, (such as matching the class main in class="nav main", but not in class="nav maintenance", for instance), gets a little more complicated, still. I'll refer you to this answer for such an example.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
  • It's not working either. I get a blank page result. I've used the xpath query that you've suggested – Gerard Santos May 29 '17 at 14:51
  • @GerardSantos Then you should remove the `@` in front of `$DOM->loadHTML($html);` and let PHP show you the actual errors, in stead of suppressing them, because, if the HTML you've shown is just a snippet of a larger page that is valid HTML, it should work just fine, because I've just tested it. If you're unsure how to successfully load possibly invalid HTML with `DOMDocument` I suggest you ask a new question or search StackOverflow; perhaps such questions are already answered. – Decent Dabbler May 29 '17 at 20:00