Need help in xml and xsl

webmasterbeta

New Member
Hello everyone i am new around here, please kindly help me as i am facing problem with xml and xsl, i am currently doing a assignment on XML and XSL, but i have very little knowledge in it, therefore hoping that the guru will be able to help me.

i need to load xml and xsl into the html webpage but the display just will not come out.

Html file.
<html>
<body text="#663333" link="#663333" vlink="#663333" alink="#663333">
<h2>
<script type="text/javascript">

function display() {
// The following functions load the catalog.xml and catalog.xsl files and do the XSLT transformation
// on the catalog.xml file using the stylesheet defined in catalog.xsl and the first function is
// completed for you.

// Load catalog.xml into a var called xmldata
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.load("catalog.xml");

// Load the catalog.xsl file that you have created into another variable called xslfile. The catalog.xsl file
// must select the number, name, desc and price tags from each product tag in catalog.xml and
// display the results in a table.

// add your code here, similar to the previous function ...

var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("display.xsl")

// Transform xmldata using xslfile and store in a var called xmlresult
// You will need to use the transformNode method ...

// add your code here ...

document.write(xml.transformNode(xsl))

// write the results to the screen
//document.write(xmlresult);
} // end function display()


// create new XML doc (load into memory only) to store selected items
var selectxml = new ActiveXObject("Microsoft.XMLDOM");
selectxml.async = false;
selectxml.loadXML("<?xml version=\"1.0\"?><selection type=\"simple\"></selection>");
if (selectxml.parseError.errorCode != 0) {
document.write("Error loading XML: " + selectxml.parseError.reason)
}


function add_item(name){

//add an item to the new XML doc in memory
root=selectxml.documentElement;
newNode=selectxml.createNode(1,"item","");
root.appendChild(newNode);
root.lastChild.text= name;

alert(selectxml.xml);

}

function prompt() {
//create a cookie and store the XML doc which is in memory
document.cookie = escape(selectxml.xml);

var cookie_stuff = getCookieVal();

alert(cookie_stuff);

}

function getCookieVal () {
// a function to return just the string part of the data in the cookie
var endstr = document.cookie.indexOf (";",0);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(0, endstr));

}



</script>
<SCRIPT>
// ----- Scripts to activate buttons ------

function over(item)
{
item.style.color = "black";
}

function out(item) {
item.style.color = "gray";
}

</SCRIPT>
<!-- Start of main HTML section -->
<font face="Verdana, Arial, Helvetica, sans-serif" color="#663300"> </font>
<font face="Verdana, Arial, Helvetica, sans-serif" color="#663300">Example Online
Store</font> </h2>
<hr>
<table border=0 cellpadding=0 cellspacing=0>
<TR>
<TD><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Step One</font></b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
Select items from table</font></TD>
</TR>
<TR><TD> </TD></TR>
</table>

<br>
<table width="401" BORDERCOLOR="burlywood" BGCOLOR=papayawhip BORDER="2" height="96" cellpadding="1">
<tr>
<td>
<div id="xslresult">
<!-- resulting HTML will be inserted here -->
<div align="center"></div>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div>
</div>
<div align="center">
<script type="text/javascript"> display(); </script>
<br>
</div>
</td>
</tr>
</table>
<br>
<DIV id="xslresult"></DIV>
<p>

<table border=0 cellpadding=5 cellspacing=0>
<TR>
<TD><A HREF=http://www.webdeveloper.com/forum/archive/index.php/"JavaScript:prompt()"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Step
Two</font></b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
Set Shopping basket cookie</font></A></TD>
</TR>
<TR><TD> </TD></TR>
<TR>
<TD><A HREF="basket.html"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Step
Three</font></b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
Display Shopping basket</font></A></TD>
</TR>
</table>

</body>
</html>

XML file
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
<catalog type="simple">
<product>
<number>109939</number>
<name>Desk</name>
<desc>office desk</desc>
<price currency="US">99.95</price>
</product>
<product>
<number>109940</number>
<name>Lamp</name>
<desc>office lamp</desc>
<price currency="US">10.95</price>
</product>
<product>
<number>109941</number>
<name>Chair</name>
<desc>office chair</desc>
<price currency="US">49.95</price>
</product>
<product>
<number>109942</number>
<name>Table</name>
<desc>office table</desc>
<price currency="US">120.95</price>
</product>
</catalog>

XSL file
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:variable name="iname" />

<xsl:template match="selection">
<catalog>
<xsl:for-each select=".">
<xsl:apply-templates/>
</xsl:for-each>
</catalog>
</xsl:template>

<xsl:template match="item">

<!--- NOTE - this variable is used to represent the product names you have selected to -->
<!--- replace the text Product type 1, Product type 2 etc in the Ass1.html file -->
<xsl:variable name="iname" select="."/>


<!--- You will need to change the following value-of statements to use your own catalog file -->
<!--- ie here it was called catalog.xml -->
<!--- and it was assumed the catalog xml structure had a root elemen called catalog with -->
<!--- one or more subelements called product and with a child element of product matching one of -->
<!--- the names chosen for the products in the select boxes from the Ass1.html selection boxes -->
<!--- It has also been assumed in what you see below that each product element has 3 subelements -->
<!--- called name, number and price -->

<product>
<name>
<xsl:value-of select="document('catalog.xml')/catalog/product[child::node()=string($iname)]/name"/>
</name>
<number>
<xsl:value-of select="document('catalog.xml')/catalog/product[child::node()=string($iname)]/number"/>
</number>
<price>
<xsl:value-of select="document('catalog.xml')/catalog/product[child::node()=string($iname)]/price"/>
</price>
</product>
</xsl:template>


</xsl:stylesheet>


PLease kindly help me.

Thanks in advanced.
Acidblue.
 
Back
Top