I'm trying to build something to parse custom HTML similar to what you can do in Vue.js or React for example. There you can use empty attributes but phps \DOMDocument is giving me an error for markup like this:
<div><div foo></div></div>
PHP Warning: DOMDocument::loadXML(): Specification mandates value for attribute foo in Entity, line: 11
Just do this to reproduce the problem:
$document = new \DOMDocument();
$document->loadXML($html);
I've already read https://www.php.net/manual/en/domdocument.loadxml.php and https://www.php.net/manual/en/libxml.constants.php and tried LIBXML_NOWARNING but the warning still appeared for whatever reason. Then I've tried LIBXML_NOERROR but this lead to no output at all.
I'm not using $document->loadHTML($html); intentionally because if used with unknown tags you'll end up with this warning:
PHP Warning: DOMDocument::loadHTML(): Tag mytag invalid in Entity.
I know that I can suppress this warning but I would prefer to not suppress warnings at all. There might be other warnings and I don't consider it good coding style to suppress warnings, they shouldn't happen, because there can be side effects. If don't mind switching to loadHTML() if there is another way to prevent this warning.
So is there any way I can deal with empty value attributes that don't have a value defined at all in the markup using \DOMHtml?
$document->loadHTML($html).php example.php.