PDA

View Full Version : XML and XSL problem... please help


Teng
05-18-2007, 03:55 PM
OK so i'm having problems with XML and XSL in passing data...

Heres my XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Students>
<Student id="2874377">
<StudentDetails>
<FirstName>Terry</FirstName>
<MiddleInitial>J</MiddleInitial>
<LastName>Adams</LastName>
<DateOfBirth>21/07/1988</DateOfBirth>
<Gender>M</Gender>
<Nationality>Australian</Nationality>
</StudentDetails>
</Student>
</Students>

Here's my HTML with javascript....

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Student Details</title>
</head>
<body>
<h1>Student Details</h1>
<script type="text/javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("Student.xml")

// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("Home.xslt")

// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>

And here's my XSL

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//Student[@id='2874377']">
<table>
<tr>
<td><xsl:value-of select="StudentDetails/LastName"/><xsl:value-of select="StudentDetails/FirstName"/><xsl:value-of select="Student@id"/></td>
</tr>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

However it wont pass the data to the html file... whats wrong with it? Any help would be appreciated. Thanks

Daniel Israel
05-18-2007, 07:07 PM
I hope you know this will only work in IE. You do? OK. Now, the transform isn't real robust. If it fails, it doesn't tell you anything. You should put a stylesheet declaration in your XML file and open it in the browser. That's a little better (at least you get some feedback).

Doing so, I found that (with your code), I had to change (in the XSLT file):


<xsl:value-of select="Student@id"/>


to


<xsl:value-of select="@id"/>


and it worked.

Teng
05-19-2007, 04:47 AM
How did it work? Are you opening the HTML file? and it is opening through that? because i changed my code to what you said and open the HTML file and it does not show any of the data from the xml?