Search code examples
phpxmldomxpath

Fetching value of specific text node using DOMXPath


From the following structure:

image shows the div from where to get data

I'm trying to fetch the marked text with the following code:

$price_new='div/div[@class="cat_price"]/text()';

if ($price_new!=null && $node = $Website_Xpath->query ($price_new, $row )) {
                    $result [$value] ['Price'] = $node->item( 0 )->nodeValue;


                } else {
                    $result [$value] ['Price'] = "";
                }

but the node value is NULL. How do I fetch the number correctly?


Solution

  • You should provide the actual snippet, not just a screenshot of it. If I interpreted the screenshot correctly the snippet is something like:

    $xml = <<<'XML'
    <body>
      <div class="cat_price">
        <div class="was">67,000 - PKR</div>
        "
              64,9999"<span> - PKR</span>
      </div>
    </body>
    XML;
    

    The text node with the price is the following sibling of the div with the class was. So it is possible to fetch it using that axis:

    $document = new DOMDocument();
    $document->loadXml($xml);
    $xpath = new DOMXpath($document);
    
    $expression = 'string(//div[@class="cat_price"]
       /div[@class="was"]/following-sibling::text()[1])';
    
    var_dump($xpath->evaluate($expression));
    

    Unlike DOMXpath::query(), DOMXpath::evaluate() can return scalar values depending on the expression. A string cast or a string function will return a string.

    string(25) "
        "
              64,9999""
    

    However the result will not only contain the number but the quotes and some whitespaces. translate() and normalize-space() could be used to clean it up:

    $expression = 'normalize-space(
      translate(//div[@class="cat_price"]
        /div[@class="was"]/following-sibling::text()[1], \'"\', " ")
    )';
    
    var_dump($xpath->evaluate($expression));
    

    Output:

    string(7) "64,9999"