Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Flag to consider document fragments as part of the DOM #17110

Description

@daniesy

Description

XPath queries don't work with <template> tags.

The following code:

<?php

use Dom\HTMLDocument; 
use Dom\XPath;

$html = '<html><body><template><p>Hello World</p></template></body></html>'; 
$dom = HTMLDocument::createFromString($html, LIBXML_NOERROR); 
$xpath = new XPath($dom);
$xpath->registerNamespace("html", "http://www.w3.org/1999/xhtml");
$query = "//html: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

I understand that the new HTMLDocument implementation adheres more closely to HTML5 standards, meaning that the content of <template> tags is considered a document fragment and not part of the main DOM. However, the current workaround is cumbersome:

  1. Query all <template> tags directly.
  2. Create separate HTMLDocument instances for their content.
  3. Query the content of each HTMLDocument individually.

This process becomes increasingly complex with nested templates. I believe this could be simplified with a flag in either HTMLDocument or XPath.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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