# Working with XML, XSL and Javascript with Firefox



## brent.charlebois (May 8, 2007)

The following code doesn't work...Why not?

var	xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("clients_xml.xml");
var	xslDoc=document.implementation.createDocument("","",null);
xslDoc.load("clients_xsl.xsl");

Thanks for your help.


----------



## stirling (Jan 10, 2007)

Hi again Brentc

not a lot to go on here, it's nigh impossible to debug fragments, however I'll take a guess...

I'll assume you've included the standard Firefox test for document.implementation and document.implementation.createDocument and that it passed.

If so all I can guess is that there are issues with wondoze ME. I'd suggest for the time being you go back to IE (re: your other post) and then check you have at least MSXML V3 installed (do a search for msxml3.dll in {rootdrive}:\winnt\system32 - if not download it from MS.


----------



## brent.charlebois (May 8, 2007)

Yes, I did a conditional for Firefox, etc. and even took the conditional out to no avail..And yes I have MSXML3.dll but then maybe its, as you mentioned, the ME...I'll try it with another browser if i can get hold of one...ie. Netscape...

Brentc


----------



## stirling (Jan 10, 2007)

so what's failing exactly? can you give any more info of what's happening?


----------



## brent.charlebois (May 8, 2007)

It's doing the same thing with IE now. The following code no longer works:

// code for IE
// Load XML 
if (window.ActiveXObject) {
document.write("We are in XML load")
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("new_clients_xml.xml")

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("new_clients_xsl.xsl")
}
// Transform
document.write(xml.transformNode(xsl))

I changed the file names in case...What I did do though is drag and drop the files to my mp3 player and back again?...when I click on the xml file I get an error: 'can't load XML file.' I checked the XML and here is some of it:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="new_clients_xsl.xsl"?>


<clientstatus>
<client>
<name>George</name>
<team>A</team>
<lunch>Friday</lunch>
<dinner>Sunday</dinner>
<smoking>NA</smoking>
<bathroom>Week 2</bathroom>
<board>VP</board>
<otherchores>Yard Work</otherchores>
</client>

<clientstatus>

Can you see anything wrong with it?


----------



## stirling (Jan 10, 2007)

well the most obvious thing is the line <?xml-stylesheet type="text/xsl" href="new_clients_xsl.xsl"?> in the xml.

remember my earlier posts, you can *either* explicitly code a routine to do the transform (like you're attempting at the moment) *OR* you can get the browser to do it for you.

The line above tells the browser to apply a transform, so when you explicitly attempt a transform in your code you're trying to transform something that's already been transformed if you get my drift...

So when doing the transform explicitly - take out the stylesheet ref in your xml.


----------



## brent.charlebois (May 8, 2007)

I tried as you said...with and without explicit transformation but still nothing...if I click on the xml file containing the style-sheet I get this error: 'Cannot display XML file with XSL file'. If I take the style-sheet out of the xml file and click on the html file with the script to load the two files, the html before the script works fine but when it gets to the script to load the xml and xsl it bombs...The thing is that all this was working before, as I told you. The only thing that I did that I can think of is I put the files on my mp3 player and back again...I got some warnings about the file names(I forget exactly what the warning was). I kind of think I has to do with the file names...but I saved the code under different file names and, well that didn't work either..???


----------



## stirling (Jan 10, 2007)

well i suppose the mp3 player could have changed the encoding to something other than ISO-8859-1 but...

I notice you havn't tried loading the xml file without the xsl link into IE - what happens then?

I would suggest you go back and start again cos like sometimes happens things get in such a state that you don't know where you are.

I'm guessing you're working with the example "crossbrowser" from w3schools. I'd start with their example code and make sure that runs ok and then simply change the xml and xsl file for yours.


----------



## brent.charlebois (May 8, 2007)

The html file which loads the other files works again. I've narrowed the problem down to the transformation...IT doesn't work...either way. If I click on the xml file it finds errors in the xsl file...I've gone over and over it to no avail...again, thank you for your assistance.


----------



## brent.charlebois (May 8, 2007)

ok, here's my xsl file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="clientstatus">

<html>
<body bgcolor="880000">
<center>
<h3>We are in the XSL file</h3> this line doesn't display
<table border="1" bgcolor="#DEB887">
<tr bgcolor="#FFE4B5">
<th align="center">Name</th>
<th align="center">Team</th>
</tr>
<xsl:for-each select="client">

<tr>
<td align="center"><xsl:value-of select="name"/></td>
<td align="center"><xsl:value-of select="team"/></td>
</tr>
</xsl:for-each>
</table>
<br><br>

</center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


Can you see anything wrong with it?


----------



## stirling (Jan 10, 2007)

ok here's what I did... I copied your xsl to notepad and saved it as test.xsl.
I then double clicked on it to open it in IE.

here's the errors IE gave me...

*End tag 'center' does not match the start tag 'br'. Error processing resource ... Line 25, Position 3 


</center>
--^*

(It's actually that you havn't closed the <br> tags i.e. <br/>. All browsers have been letting us write crappy syntactically incorrect html for years but they have to enforce correct syntax for xml - as I said previously - it's the law:smile


*End tag 'xsl:stylesheet' does not match the start tag 'xsl:transform'. Error processing resource ... Line 29, Position 3 


</xsl:stylesheet>
--^*

i.e. if you open with transform then close with transform </xsl:transform>


----------



## brent.charlebois (May 8, 2007)

Thank you so much stirling! My IE did not give me those error messages you got...it just got the arrow pointing to the </center> tag. So now that's working!

Now here I go again...why doesn't the following XQuery work (I got the structure straight from M3Schools but put my own data in 

<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>

I get the code printed out as is and {data($x)} as a list item....? I'll keep working on it though.


----------



## stirling (Jan 10, 2007)

Hi again brentc

well it works fine for me...

Depends on what you're doing... I get confused as to whether you're externally linking to your xsl file or applying the transform via jscript. You seem to chop and change. Anyway if you're linking you need to have this as the second line in your books.xml file.

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

and then have your query in a well-formed books.xsl file. Then load books.xml into IE.

OR if via jscript

Don't have the above link in your xml file and do your document.write(xml.transformNode(xsl))


----------



## brent.charlebois (May 8, 2007)

I did what you said-both ways-explicitly in the XML file and with JScript in the HTML file...neither way works!

Anyway, here's another one for you:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>


<head>
<script type="text/javascript">
var xmlDoc;
var xslDoc;
var i;

function loadXML()
{
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("new-clients-xml.xml");
getClientinfo(); 

}
}


function getClientinfo()
{	
var n=xmlDoc.getElementsByTagName('name');
var t=xmlDoc.getElementsByTagName('team');

writeClientinfo(n,t);
}


function writeClientinfo(n,t)
{
for(i=0;i<n.length;i++) {

document.write("Name: ");
document.write(n_.childNodes[0].nodeValue);
document.write("<br />");

document.write("Team: ");
document.write(t.childNodes[0].nodeValue);
document.write("<br />");
document.write("<br />");

}
}

</script>

</head>

<body bgcolor="#0000FF" onload="loadXML()" >

WE are in the BODY
<input type="button" value="Next Client" onClick="getClientinfo()">
After the BUTTON

</body>

</html>


I get the client listing but not the blue background or button...ie. nothing in the <body> works...can you see what's wrong with this code?_


----------

