473,378 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

ajax, xml and applying xslt

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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Sommer | last post by:
Hi, I think the following is not possible but maybe (hopefully) I am wrong: I have: <xsl:param name="test"> <tag1>asdfasdf</tag1> <tag2>asdfasdf</tag2> </xsl:param>
0
by: melledge | last post by:
Ajax Developers' Day added to XTech 2006 agenda XTech 2006 - 17-19 May - Hotel Grand Krasnopolsky - Amsterdam, The Netherlands
3
by: Simon Brooke | last post by:
I've been doing XSL transforms, converting XML to HTML, server side since 2000. In those days, clients which could do the transformation client side didn't exist, so whether to transform...
0
by: melledge | last post by:
Ajax Developers' Day to Kick Off XTech 2006 Conference Industry experts offer insight into next generation of the Web ALEXANDRIA, VIRGINIA, USA - April 25, 2006 - In response to the rapidly...
31
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked...
3
by: RC | last post by:
Let's say: if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { // Now I got an XML object here var xmlDocument = XMLHttpRequestObject.responseXML; // next I have...
2
by: milecimm | last post by:
Hello, I need some help to solve the following problem (if it is possible, that's it): I'm using a xpath expression to programatically get data from my xml file. I want to transform ONLY the...
2
by: soni2926 | last post by:
hi, does anyone know of any good books on ajax and asp.net, one that teaches ajax itself before jumping in atlas? I wanted to get an understanding of ajax and how to use it, most books i've seen...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.