Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>DOMDocument-&gt;saveHTML()</code> takes your XML DOM infoset and writes it out as old-school HTML, not XML. You should not use <code>saveHTML()</code> together with an XHTML doctype, as its output won't be well-formed XML.</p> <p>If you use <code>saveXML()</code> instead, you'll get proper XHTML. It's fine to serve this XML output to standards-compliant browsers if you give it a <code>Content-Type: application/xhtml+xml</code> header. But unfortunately IE6-8 won't be able to read that, as they can still only handle old-school HTML, under the <code>text/html</code> media type.</p> <p>The usual compromise solution is to serve <code>text/html</code> and use ‘HTML-compatible XHTML’ as outlined in Appendix C of the XHTML 1.0 spec. But sadly there is no PHP <code>DOMDocument-&gt;saveXHTML()</code> method to generate the correct output for this.</p> <p>There are some things you can do to persuade <code>saveXML()</code> to produce HTML-compatible output for some common cases. The main one is that you have to ensure that only elements defined by HTML4 as having an <code>EMPTY</code> content model (<code>&lt;img&gt;</code>, <code>&lt;br&gt;</code> etc) actually do have empty content, causing the self-closing syntax (<code>&lt;img/&gt;</code>) to be used. Other elements must not use the self-closing syntax, so if they're empty you should put a space in their text content to stop them being so:</p> <pre><code>&lt;script src="x.js"/&gt; &lt;-- no good, confuses HTML parser and breaks page &lt;script src="x.js"&gt; &lt;/script&gt; &lt;-- fine </code></pre> <p>The other one to look out for is handling of the inline <code>&lt;script&gt;</code> and <code>&lt;style&gt;</code> elements, which are normal elements in XHTML but special <code>CDATA</code>-content elements in HTML. Some <code>/*&lt;![CDATA[*/.../*]]&gt;*/</code> wrapping is required to make any <code>&lt;</code> or <code>&amp;</code> characters inside them behave mostly-consistently, though note you still have to avoid the <code>]]&gt;</code> and <code>&lt;/</code> sequences.</p> <p>If you want to really do it properly you would have to write your own HTML-compatible-XHTML serialiser. Long-term that would probably be a better option. But for small simple cases, hacking your input so that it doesn't contain anything that would come out the other end of an XML serialiser as incompatible with HTML is probably the quick solution.</p> <p>That or just suck it up and live with old-school non-XML HTML, obviously.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload