Jump to content

Html and xml on same page


chknabeel

Recommended Posts

No, but even if you could... what's the big idea anyway?The proper way to embed XML data in (X)HTML is to have some JavaScript that would load the code, extract the certain parts of the data you want, and add it to the places where you want that data.W3Schools have their own loadXMLDoc() method that does the first part for you, and the second and third parts are described in the rest of the DOM tutorial. There are other (in certain cases better) ways to load an XML document of course, but that's one easy way.

Link to comment
Share on other sites

There are other possibilities. One is to use XSLT to transform your XML into HTML. Not an easy skillset to learn, but if you think you'll use it alot . . .Use PHP (or something similar) to parse an XML DOMDocument, extract data, and embed it in appropriate HTML structures. This is a pre-processor way of doing what boen suggested doing with AJAX and HTML DOM methods.A more dodgy strategy would be to embed your XML into your HTML document (ie, before the closing HTML tag) just as it is. Use CSS to style the XML elements by their XML names. I believe all modern browsers can now support this, and it is often recommended for developers who want to use HTML5 elements not currently recognized by browsers. They don't inherit any properties, so their default styles are like divs or spans until you give them CSS, and the recommendation is usually applied only to the new semantic elements of HTML5. But for the functionality to work at all, the newest browsers pretty much have to honor any custom tag, so the trick works. I told you it was dodgy.IE is a little behind on this (it will not honor custom tags at all) but there is a workaround (call it a kludge if you want). JavaScript can be written so that IE will recognize a custom element. Say you have XML tags with names like customer, product, cost. Add JavaScript that looks like this:document.createElement('customer');document.createElement('product');document.createElement('cost');You don't have to do anything with those elements you've just created. It's just a trick that allows IE to recognize those elements as if they were genuine HTML. I'm not sure, but I think those statements have to execute before the browser reads the tags themselves, so that part of your script will have to execute at the top of your document.Of course, your XML document may not be structured in a way that makes this at all useful. Maybe if you wanted it to look something like a table.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...