| Feedback | Creating XML from Excel |

Javascript in a web page to display an XML file

Javascript can be linked to buttons on an HTML web page. The buttons can re-sort or select a particular set of records from an XML file.

authsig_java2.htm (the HTML code for the page containing the data and 3 buttons)

<HTML>
<head>
<style type='text/css'>
body {font-family: Arial, Helvetica; font-size: 10pt; margin-left: 10; margin-right: 10}
td {padding: 1pt 3pt 2pt 3pt; border-style: solid; border-width: 1; border-color: #0F5BB9}
table {border-collapse: collapse; border-width: 1 ; border-style: solid; border-color: #0F5BB9 }
</style>

</head>

<BODY>
<XML id=xmldso></XML>
<XML id=sortname src="sortname.xsl"></XML>
<XML id=sortcode src="sortcode.xsl"></XML>
<XML id=getfin src="getfin.xsl"></XML>

<SCRIPT Language="JavaScript">
xmldso.async = false;
xmldso.load("xl_xml_data.xml");
var xmldoc = xmldso.cloneNode(true);

function ApplyStyleSheet(xsldoc){
xmldoc.documentElement.transformNodeToObject(xsldoc.documentElement,xmldso.XMLDocument);
}

</SCRIPT>
<h1>Using Java to display alternative views of XML data</h1>
<p>A single unformatted XML data file (xl_xml_data.xml) is opened in a table. It can be rearranged by selecting 3 alternative XSL files (sortcode.xsl, sortcode.xsl and getfin.xsl).</p>
<p>The table derives its appearance from the CSS Style command and not from any XSL instructions.</p>
<br>
<TABLE DATASRC='#xmldso'>
<tr><THEAD><TH>Care Gp</TH><TH>Auth Code</TH> <TH>Name</TH> <TH>Value</TH><th>Joined</th></THEAD></tr>
<TR>
 <TD><SPAN class="none" DATAFLD="directorate"></SPAN><Font></TD> 
 <TD><SPAN class="none" DATAFLD="acode"></SPAN></TD>
 <TD><SPAN class="none" DATAFLD="name"></SPAN></TD>
 <TD align='right'><SPAN class="none" DATAFLD="value"></SPAN></TD>
 <TD><SPAN class="none" DATAFLD="start_date"></SPAN></TD>
</TR>
</TABLE>

<p>
<INPUT TYPE=BUTTON VALUE="Sort By Name" onclick="ApplyStyleSheet(sortname.XMLDocument);">
<INPUT TYPE=BUTTON VALUE="Sort By Code" onclick="ApplyStyleSheet(sortcode.XMLDocument);">
<INPUT TYPE=BUTTON VALUE="Get Finance" onclick="ApplyStyleSheet(getfin.XMLDocument);">
</p>
</BODY>
</HTML> 

sortname.xsl (linked to the Sort by Name button)

<meadinkent>
<xsl:for-each order-by="+name" select="record" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<record>
 <directorate><xsl:value-of select="directorate"/></directorate>
 <acode><xsl:value-of select="acode"/></acode>
 <name><xsl:value-of select="name"/></name>
 <value><xsl:value-of select="value"/></value>
 <start_date><xsl:value-of select="start_date"/></start_date>
</record>
</xsl:for-each>
</meadinkent>

getfin.xsl (linked to the Select Finance Directorate button)

<meadinkent>
<xsl:for-each order-by="+acode" select="record[directorate$eq$'Finance']" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<record>
 <directorate><xsl:value-of select="directorate"/></directorate>
 <acode><xsl:value-of select="acode"/></acode>
 <name><xsl:value-of select="name"/></name>
 <value><xsl:value-of select="value"/></value>
 <start_date><xsl:value-of select="start_date"/></start_date>
</record>
</xsl:for-each>
</meadinkent>



file: xsl_java_coding.htm © MeadInKent.co.uk 2011 Page last updated Nov07 View a selection of recommended books on Excel