|
Toggling between HTML and XML
The HTML Tidy procedure I spelled out here is proving awkward. If I screw something up that Tidy can't fix, I wind up checking the document in an XML parser. A convenient parser is the one in MSIE, and lately I've been using it for this purpose. This is awkward too, however. For a few days, I tried this procedure:
- Write the file, e.g. file.html
- Copy the file to, e.g., file.xml.
- Check file.xml in MSIE.
- Extract and post contents.
But the edit/copy/check cycle is ridiculous. Couldn't I toggle the same file between two modes? Here's one approach. By adding this to the file:
<head><meta http-equiv="content-type" content="text/xml"/></head>
I could view as XML and check well-formedness. Now toggling between HTML and XML modes was as simple as changing between "text/xml" and "text/html" and reloading. Here's are both views, for this in-progress document, in MSIE on Mac OS X:
Content-type: text/html
Content-type: text/xml
What I didn't discover until later is that, for reasons I'm sure someone will explain to me, this doesn't work in MSIE 6. Not knowing that, I next attempted what seemed like a brilliant hack. Why not make a couple of bookmarklets to poke the META HTTP-EQUIV tags into the DOM? Then it'd be just a one-click deal to toggle from HTML to XML and back.
Here's a snippet that (in theory) does the HTML-to-XML switcheroo:
<a href="javascript:void( function() {var element=document.createElement('meta'); element.setAttribute('http-equiv','content-type'); element.setAttribute('content', 'text/xml'); var oldhead = document.getElementsByTagName('head')[0]; var newhead=document.createElement('head'); newhead.appendChild(element); document.firstChild.replaceChild(newhead, oldhead);} () )">switchToXML</a>
Mozilla's amazingly cool DOM inspector enables us to verify that the expected change was made. Here's the document in its default text/html mode:
Content-type: text/html
And here's the document after clicking on that javascript: link to modify the META HTTP-EQUIV element:
Content-type: text/xml
It works! Except no, it doesn't. Although the DOM exactly matches the DOM obtained by reading in a file that originally contains an HTTP-EQUIV of text/xml, MSIE doesn't react to the dynamic change.
Sadly it's all kind of moot anyway. Safari won't parse XML, Mozilla parses without the XML viewer, but like MSIE 6 doesn't seem to want to let HTTP-EQUIV override the filename's extension. Ideas?
|