Here are the painstaking details:
- In XML, collapsing is allowed on all empty elements.
- In HTML4, collapsing is not really allowed.
- In XHTML1, collapsing is supposed to follow the XML rules, but (as you noticed) actually doesn't.
- In HTML5, collapsing is allowed on only certain elements.
In theory, htmlqp() should work, but the libxml HTML parser is so buggy that this isn't always the case. (QueryPath's parser is actually the libxml one that comes with PHP.)
Today I checked in the branch `feature/html5`, which will allow you to choose a full HTML5 parser that should obey the collapsing rules. It probably won't be stable for a little while, though. The new parser is this one:
https://github.com/Masterminds/html5-php
The workarounds I have seen are basically like yours (sometimes using <!-- comments --> instead of a space).
So... I think this will get fixed for QueryPath 3.1. But right now it falls into the category of "known and *really* annoying bug".