The easiest way to parse XML in XHTML for applying CSS

I am wondering how I can parse the XML documents returned by the search so that I can style them with CSS on the website. I assume that I need to turn the XML into xhtml, as first, and then change the elements accordingly. I need them to display xhtml.

Does anyone know the easiest way to do this? I know that simple XML style with CSS is not good practice, so I think there should be a solution for javascript.

Any insight would be great, thanks!

D

+4
source share
1 answer

If you import XML through AJAX, you only need a few key things ...

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

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

2.) Use standard W3C methods (appendChild, importNode, responseXML) and NOT patented Microsoft methods (innerHTML, responseText), or your application will be considered as notepad text instead of a real application. Here's 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); } 

Using the correct code, you will not have problems importing XHTML. For a live demonstration, visit my site in my profile, and then click on the site settings in the upper right corner.

+1
source

Source: https://habr.com/ru/post/1394538/


All Articles