DougP
DougP

Reputation: 159

Easiest way to parse XML into XHTML to apply CSS

I'm wondering how I can parse XML documents returned by a search so that I can style them with CSS on a website. I'm assuming I need to turn the XML into xhtml some how first, and then style the elements accordingly. I need them to display xhtml.

Does anyone know the easiest way to go about this? I know styling straight XML with CSS is not good practice, which is why I'm thinking there must be a javascript solution.

Any insight would be great, thanks!

D

Upvotes: 4

Views: 394

Answers (1)

John
John

Reputation: 13729

If you're importing XML via AJAX then there are only a few key things you need to do...

1.) The parent-most element needs an XML namespace...

<div id="ajax_search_result1" xmlns="http://www.w3.org/1999/xhtml">

2.) Use W3C standard methods (appendChild, importNode, responseXML) and NOT proprietary Microsoft methods (innerHTML, responseText) or your application will be treated like notepad text instead of an actual application. Here is roughly what the code looks like...

if (window.XMLHttpRequest) {var xmlhttp = new XMLHttpRequest();}
else if (window.ActiveXObject) {try {xmlhttp = new ActiveXObject('Msxml2.XMLHTTP')} catch (e) {try{xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')} catch (e){}}}

xmlhttp.open('GET',url,true);
xmlhttp.send(null);

xmlhttp.onreadystatechange=function()
{
 if (xmlhttp.readyState=='4')
 {
  var xmlDoc=xmlhttp.responseXML;
  document.importNode(xmlDoc.getElementsByTagName('div')[0],true),id_container_obj);
 }

By using the correct code you'll have no problem styling imported XHTML. For a live demo visit my site in my profile and then at the top right click on site options.

Upvotes: 1

Related Questions