ajax, xml and applying xslt

S

Stephen

I have the following that outputs an xml file to a div using ajax:

<script type="text/javascript">

function ajaxXML(url,control_id){
if (document.getElementById) {
var x = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}
if (x) {
x.onreadystatechange = function()
{
if (x.readyState == 4 && x.status == 200)
{
el = document.getElementById(control_id);
el.innerHTML = x.responseText;
}
}
x.open("GET", url, true);
x.send(null);
}
}

</script>

<body onload="ajaxXML('XMLSample.xml','xmlOutPut')">
<div id="xmlOutPut">
</div>
</body>

My question is the xml file has a stylesheet reference in it:
<?xml-stylesheet type="text/xsl" href="XMLSample.xslt"?>

when I open the xml file in a browser, it applies the styesheet, but
when I process it using ajax in the above example, the stylesheet/xslt
is ignored. I am new to ajax and I am probably missing something
simple. Any help is appreciated.
 
R

RobG

Stephen said:
I have the following that outputs an xml file to a div using ajax:

<script type="text/javascript">

function ajaxXML(url,control_id){
if (document.getElementById) {
var x = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}
if (x) {
x.onreadystatechange = function()
{
if (x.readyState == 4 && x.status == 200)
{
el = document.getElementById(control_id);
el.innerHTML = x.responseText;
}
}
x.open("GET", url, true);
x.send(null);
}
}

</script>

<body onload="ajaxXML('XMLSample.xml','xmlOutPut')">
<div id="xmlOutPut">
</div>
</body>

My question is the xml file has a stylesheet reference in it:
<?xml-stylesheet type="text/xsl" href="XMLSample.xslt"?>

when I open the xml file in a browser, it applies the styesheet, but
when I process it using ajax in the above example, the stylesheet/xslt
is ignored. I am new to ajax and I am probably missing something
simple. Any help is appreciated.

What is the doctype of the document you are stuffing the XML into?

For your chosen browser(s) and doctype, how many different doctypes
can a single page have?

You are using XMLHttpRequest to get your file and innerHTML to write
it into your document. What happens if you simply place the XML
content inside the div in the page source and load the page?
 
S

Stephen

What is the doctype of the document you are stuffing the XML into?
.net web form (aspx)

For your chosen browser(s) and doctype, how many different doctypes
can a single page have?
I have to admit ignorance here. I am not sure what you mean.

You are using XMLHttpRequest to get your file and innerHTML to write
it into your document. What happens if you simply place the XML
content inside the div in the page source and load the page?
 
M

Martin Honnen

Stephen wrote:

el.innerHTML = x.responseText;
when I open the xml file in a browser, it applies the styesheet, but
when I process it using ajax in the above example, the stylesheet/xslt
is ignored.

Well Ajax is no magic to get stuff working dynamically that does not
even work statically.
If you would load a static "HTML document" with e.g.
<div>
<?xml-stylesheet type="text/xsl" href="XMLSample.xslt"?>
<root>
<contents />
</root>
</div>
then you would not get any meaningful results either, the contents of a
HTML <div> element should be some other HTML elements the browser
understands and not some XML elements with some XML processing instruction.

If you want to apply XSLT to an XML input and have the browser render
the result of the XSLT transformation then you would better do the XSLT
transformation on the server.
Client side XSLT is only supported by a few browsers (IE 6, IE 5 only if
someone installs MSXML 3 on the client too, Mozilla, Safari (since
1.3?)) and scripting it only by IE and Mozilla I think.
The only meaningful construct in HTML to have client-side XSLT applied
to some XML document with a xml-stylesheet processing instruction is e.g.
<iframe src="XMLSample.xml"></iframe>
No XMLHttpRequest needed or helpful for that.

If you wanted to use XMLHttpRequest and client-side XSLT then you need
to load both the XML and XSL document and script the transformation and
then use DOM scripting in HTML to insert the result of the
transformation in the document. Mozilla would allow you to use
transformToFragment to directly create HTML DOM nodes, with MSXML in IE
you end with a string you can then insert with setting innerHTML or
using insertAdjacentHTML.

The documentation to script XSLT transformations in Mozilla is here:
<http://www.mozilla.org/projects/xslt/js-interface.html>
 
S

Stephen

Thanks for the reply. The iframe one would work because it is loading
the xml in a frame, which would be like loading it directly in the
browser right?
I was having a brain fart, because even in .net you need code to
transform xml. I don't know what made me think a .net web form could
transform xml on it's own. I want to look at the loading both docs as
in your last suggestion, avoiding postbacks as much as possible. This
is for an intranet and we are a IE only shop, but there is always a
"but.." Who knows if a user wouldn't download and install mozilla
firefox. Anyway, i will hit your link you provided and possible
rethink what I am doing. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top