Cera
Cera

Reputation: 1909

Embedding an external XML file in HTML and using an XSL file to parse it?

I have an XML file ("exchange.xml") and an XSL file ("exchange.xsl") for parsing it. exchange.xml has the following line:

<?xml-stylesheet type="text/xsl" href="exchange.xsl"?>

Indicating that exchange.xsl should be used to parse the file.

It parses as expected if I load exchange.xml in a browser. However, I want to have exchange.xml embedded in an HTML file which otherwise does not have any XML in it. How do I do this? As far as I understand it, when you embed XML in HTML with the XML tag's SRC ID, you then have to parse it yourself for it to be displayed. I want it to automatically be displayed using the XSL file I've already created for it.

It has also come to my attention that some browsers (e.g. Android default browser) won't parse the XML with the XSL file, and require a server-side transform. Is it possible to embed my XML into an HTML file but use a server-side transform?

Upvotes: 1

Views: 2995

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167696

As far as I am aware HTML 4 as specified by the W3C does not provide any way to embed XML markup in an HTML 4 document. And of the browsers I know only IE has an extension to HTML 4 that allows that, the so called XML data islands, http://msdn.microsoft.com/en-us/library/ms766512%28v=VS.85%29.aspx, where IE's HTML parser recognizes an xml element that can contain XML markup as its content or link to it with the src attribute. So unless you want to use something IE specific like an XML data islands the cross-browser way to load XML as data inside of an HTML document is to use XMLHttpRequest with Javascript.

Upvotes: 2

Valerij
Valerij

Reputation: 27758

You can use PHP: XSL class for transforming XML and include it via PHP in your page.

eg: showxml.php

<?php
function processXML($file,$styles){
...  //example here
}
<html>
...
<div id="xml>
<?php echo processXML('foo.xml','bar.xsl'); ?>
</div>
...
</html>

Upvotes: 0

Related Questions