Skip to content

XPath not working as expected #17103

@daniesy

Description

@daniesy

Description

The following code:

<?php

use Dom\HTMLDocument; 
use Dom\XPath;

$html = '<html><body><p>Hello World</p></body></html>'; 
$dom = HTMLDocument::createFromString($html, LIBXML_NOERROR); 
$xpath = new XPath($dom); 
$query = '//body//text()'; 

foreach ($xpath->query($query) as $text) { echo $text->nodeValue . "\n"; }

Resulted in this output:

Nothing

But I expected this output instead:

Hello World

This worked perfectly with DOMXPath.

PHP Version

PHP 8.4.1

Operating System

MacOS 15.1.1 (24B91) / AlmaLinux 9.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions