0

I've this HTML code ....

.....
.....
.....
<div class="bloccoOspedale">
 <div id="nomeOspedale">
  <div class="datiOspedale">Pazienti in corso di visita: 2 di cui in O.B.I.: 0</div>
  <div class="datiOspedaleCodici">
   <div class="codBianco">Codice bianco:0</div>
   <div class="codVerde">Codice verde:1</div>
   <div class="codGiallo">Codice giallo:1</div>
   <div class="codRosso">Codice rosso:0</div>
  </div>
  <div class="datiOspedale">Pazienti in attesa di visita: 0</div>
 </div>
 <div class="bloccoOspedale">
  <div id="nomeOspedale">
   <div class="datiOspedale">Pazienti in corso di visita: 23 di cui in O.B.I.: 0</div>
  <div class="datiOspedaleCodici">
   <div class="codBianco">Codice bianco:1</div>
   <div class="codVerde">Codice verde:9</div>
   <div class="codGiallo">Codice giallo:11</div>
   <div class="codRosso">Codice rosso:2</div>
  </div>
  <div class="datiOspedale">Pazienti in attesa di visita: 10</div>
 </div>
 <div class="bloccoOspedale">
  <div id="nomeOspedale">
   <div class="datiOspedale">Pazienti in corso di visita: 16 di cui in O.B.I.: 2</div>
   <div class="datiOspedaleCodici">
    <div class="codBianco">Codice bianco:0</div>
    <div class="codVerde">Codice verde:9</div>
    <div class="codGiallo">Codice giallo:7</div>
    <div class="codRosso">Codice rosso:0</div>
   </div>
   <div class="datiOspedale">Pazienti in attesa di visita: 6</div>
  </div>    
.....
.....
.....

and I've to extract the string "Codice bianco:0" using XPath in PHP ... I've tried

//div[@class="datiOspedaleCodici"]/div[1]/text()

or

//div[@class="datiOspedaleCodici"]/div[@class="codBianco"]/text()

but nothing happens ....

What is the right XPath?

Thank you in advance ...

UPDATE

I've tried to use this PHP code sample ...

<?php
    ini_set('display_errors', 1);

    $url = 'http://www.asl1.liguria.it/templateProntoSoccorso.asp';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_PROXY, '');
    $data = curl_exec($ch);
    curl_close($ch);

    $dom = new DOMDocument();
    @$dom->loadHTML($data);

    $xpath = new DOMXPath($dom);

    $Number = $xpath->query('//div[@class="datiOspedaleCodici"]/div[@class="codBianco"]/text()');

    foreach( $Number as $node )
    {
      echo "Number: " .$node->nodeValue;
      echo '<br>';
      echo '<br>';
    }    
?>

... to make clearer what I need ..... the code runs with no errors but nothing is printed ...

2
  • 1
    open the page in chrome. open developer tools. find the element you need in source and click copy xpath. done Commented Aug 15, 2017 at 21:47
  • Exactly what I've done (using Firefox .....), but it doesn't work .... Commented Aug 16, 2017 at 5:17

1 Answer 1

1

Using the fragment of your XML, the following code...

$doc = new SimpleXMLElement ( $xml );
$text = $doc->xpath('//div[@class="datiOspedaleCodici"]/div[@class="codBianco"]/text()');
foreach ( $text as $t ) {
    echo $t.PHP_EOL;
}

Does in fact echo...

Codice bianco:0
Codice bianco:1
Codice bianco:0
Sign up to request clarification or add additional context in comments.

5 Comments

#Nigel I've updated my original question with a little code sample that I use to test ..... It seems very similar to yours (you use SimpleXMLElement, I use DOMXPath and it works for other case in the sampe "business case in my code, so I'd like not to change...), but unfortunately my code print nothing. Suggestions?
I've just cut and paste your code and it gives me Number: Codice bianco:0<br><br>Number: Codice bianco:1<br><br>Number: Codice bianco:0<br><br>. Can you put echo "Loaded:".$dom->saveXML(); after your loadHTML line and check that you get some output.
That is exacty what I'd like to obtain!!! .... quite strange that in my case it does not work .... anyway now I know that my code is right .... Thanks!
Nigel ... if you can ... now my code works (I simply restart my Ubuntu VM .....), BUT ..... if I use in my code complete xpath like /html/body/table/tbody/tr/td[2]/table[2]/tbody/tr/td[3]/table/tbody/tr[2]/td[1]/table/tbody/tr/td/div[1]/div[3]/div[1]/text() to extract a single text instead of //div[@class="datiOspedaleCodici"]/div[@class="codBianco"]/text() to extract all the occurences of "codBianco" class again nothing is printed ... thanks in advance
I've solved .... this is the right xpath to use (//div[@class="datiOspedaleCodici"]/div[1]/text())[1] to get the text of the first instance of the class "codBianco". NOTE the [1] at the end!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.