Fixes for XML

The code would cause a yellow screen of death in Firefox if pages are served with <code>Content-Type: application/xml+xhtml</code>
these changes fix it to play nice with strict XML parsing.