Jump to content

XMLHttpRequest to load XML document in XSL


H-D

Recommended Posts

Hi, I am new to XSLT and I am trying to load a XML document in XSL so I can manipulate and format the output based on the XML parameter. I am using the example provided by W3schools to load XML but it does not seem to work for. XSL document I am using is attached below I do not see any outputs after the call to “loadXMLDoc()”. If I comment out this line I will see all the outputs.What am I missing? Results with the loadXMLDoc() call:Loading xml file.. Body of html Result with the call commented out:Loading xml file..xml file loaded.. Body of html Thank you for your help in advance. Regards,HD XSL code:===================================================<?xml version="1.0"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <script> function loadXMLDoc(dname) { var xhttp; if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",dname,false); xhttp.send(); return xhttp.responseXML; } var xmlDoc; document.write("Loading xml file..<br/>"); xmlDoc=loadXMLDoc("cd_catalog.xml"); document.write("xml file loaded..<br/>"); </script> <br/> <td>Body of html</td> </body> </html></xsl:template></xsl:stylesheet>================================================================= XML:===================================================<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="../macros/test.xsl"?><CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> <CD> <TITLE>Greatest Hits</TITLE> <ARTIST>Dolly Parton</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>RCA</COMPANY> <PRICE>9.90</PRICE> <YEAR>1982</YEAR> </CD></CATALOG>===============================

Link to comment
Share on other sites

It is not clear why you mix XSLT and HTML and Javascript. If you want to load an additional XML document in your XSLT code then use the "document" function with e.g.

<xsl:variable name="doc2" select="document('cd_catalog.xml')"/>

If on the other hand you simply want to use Javascript in HTML, why do you also need XSLT?As for the Javascript not working, tell us exactly which browsers you have tried and which error messages you see in the error or script console of the browser. So far the only problem I see is that Mozilla browsers do not support the use of document.write in script in the result of an XSLT transformation. But that way you shouldn't get any output.

Link to comment
Share on other sites

I don’t have to use XSLT. I have also tried to just load the XML in HTML using Javascript and experience the same problem. I have tried IE9 and Google Chrome. I do not get an error. I just don’t see the outputs that I expect. Thanks,hd

Link to comment
Share on other sites

Hi, I see the error code now going to script console. When I try to load XML using Javascript in HTML I get the following errors: In IE9:test2.html, line 15 character 13SCRIPT5: Access is denied. Where line 15 is: xhttp.open('GET',dname,false); ----In Chrome I get the following error:

Thanks,hd

Link to comment
Share on other sites

So the problem is with different origins of files and security restrictions in browsers. Inside the browser you will find that script in HTML documents loaded from the file system might be denied access to other origins where depending on the browser and depending on security settings a different origin could be a HTTP server or a different directory on the file system. Or script in a document loaded from on HTTP domain can't access the file system or can't access a different HTTP domain (although the latest versions of browsers usually allow that if the server cooperates, see http://en.wikipedia.org/wiki/Cross-origin_resource_sharing). You might want to ask in a forum dedicated to the browser you target or at least post in http://w3schools.invisionzone.com/index.php?showforum=26, explaining in detail the locations of files used when you get those errors, I don't think this is XSLT related.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...