By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

ajax, xml and applying xslt

P: n/a
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.

Oct 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Stephen wrote:
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?
--
Rob
Oct 13 '05 #2

P: n/a
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?
You mean without using XMLHttpRequest? I did try to put the actual content of the xml in the div and got the same result. I think that the web form doesn't know what to do with it (the style sheet part of it) and it will have to be transformed while loading it. Is there method using XMLHttpRequest for applying transformation while loading?


Oct 13 '05 #3

P: n/a


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>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 13 '05 #4

P: n/a
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.

Oct 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.