Skip to content

Can't use xpath with comments in SimpleXML #12170

Closed
@nielsdos

Description

@nielsdos

Description

The following code:

<?php
$sxe = simplexml_load_string(<<<XML
<?xml version="1.0"?>
<container>
    <!-- comment -->
</container>
XML);

var_dump($sxe->xpath("//comment()"));

Resulted in this output:

array(0) {
}

But I expected this output instead:

array(1) {
[0] => the comment node
}

However, you can see the comment node with var_dump($sxe):

object(SimpleXMLElement)#1 (1) {
  ["comment"]=>
  object(SimpleXMLElement)#2 (0) {
  }
}

So SimpleXML can handle it, kinda.

PHP Version

PHP 8.1+

Operating System

Linux

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