Skip to content

Instantly share code, notes, and snippets.

@lighth7015
Last active October 23, 2022 03:01
Show Gist options
  • Save lighth7015/89504b6db719231882390b9a922503ad to your computer and use it in GitHub Desktop.
Save lighth7015/89504b6db719231882390b9a922503ad to your computer and use it in GitHub Desktop.
Issue querying for namespaced child fragments.
<?php
$template =<<<EOF
<!DOCTYPE html>
<html xmlns:x="http://schema.hpprx.com/schemas">
<head></head>
<body>
<div x:for-each="(foo, i) of bar">
a
</div>
</body>
</html>
EOF;
class Template {
private DOMDocument $doc;
public function __construct(string $buffer) {
$doc = $this->doc = new DOMDocument;
$doc->loadHTML($buffer);
}
private static function xpath(string $attribute): string {
$path = sprintf('//*[@x:%s]', $attribute);
var_dump($path);
return $path;
}
public function queryElements(string $attribute) {
$xpath = new DOMXPath($this->doc);
$xpath->registerNamespace('x', 'http://schema.hpprx.com');
// var_dump($this->doc->saveHTML(null));
return $xpath->query(static::xpath($attribute));
}
}
$loader = new Template($template);
var_dump($loader->queryElements('for-each'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment