PDA

View Full Version : Binding the checkboxes in the table with HTML in XML


Tozilla
10-10-2004, 07:04 AM
i created a XML file that have values for checkboxes. And I created a HTML file with checkboxes. I'm just wondering how I can bind the data to HTML, so the user can automatically see the checkboxes "checked" when the HTML file is opened.

The data in the XML file "data.xml

<Car>false</Car>
<Boat>false</Boat>
<Motorcycle>true</Motorcycle>



<div datasrc="#data" id="rightbox">
<h2 id="subtitle">Transportation</h2>

<table width="400" align="center">
<tr>
<td width="100">Car</td>
<td><input type="checkbox"></td>
<td width="100">Boat</td>
<td><input type="checkbox"></td>
<td width="100">Motorcycle</td>
<td><input type="checkbox"></td>
</tr>
</table>
</div>

mpjbrennan
10-10-2004, 10:48 AM
You can do this with XSLT - example below. It's a bit messy but I was in a rush - I'm sure it could be tidied up.

patrick

XML file
------------
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test_xsl.xml"?>
<main>
<Car>false</Car>
<Boat>false</Boat>
<Motorcycle>true</Motorcycle>
</main>

XSLT file
------------
<?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:apply-templates select="*"/>

</body>

</html>

</xsl:template>



<xsl:template match="main">
<table width="400" align="center">
<xsl:for-each select="Car">
<xsl:variable name="test" select="."/>
<tr>
<td width="100">Car</td>
<td><input type="checkbox">
<xsl:if test="$test='true'">
<xsl:attribute name="checked"/>
</xsl:if>
</input>
</td>

</tr>

</xsl:for-each>

<xsl:for-each select="Boat">
<xsl:variable name="test" select="."/>
<tr>
<td width="100">Boat</td>
<td><input type="checkbox">
<xsl:if test="$test='true'">
<xsl:attribute name="checked">
</xsl:attribute>
</xsl:if>
</input>
</td>

</tr>

</xsl:for-each>

<xsl:for-each select="Motorcycle">
<xsl:variable name="test" select="."/>
<tr>
<td width="100">Motorcycle</td>
<td><input type="checkbox">
<xsl:if test="$test='true'">
<xsl:attribute name="checked">
</xsl:attribute>
</xsl:if>
</input>
</td>

</tr>

</xsl:for-each>
</table>
</xsl:template>



</xsl:stylesheet>

Tozilla
10-10-2004, 11:06 AM
You can do this with XSLT - example below. It's a bit messy but I was in a rush - I'm sure it could be tidied up.

patrick



thank you for helping me. I know what to do now

Thank you :)

mpjbrennan
10-10-2004, 02:31 PM
Glad you found a solution - I was going to offer this as an alternative (only for IE):

HTML file
---------
<html>
<head>
<script type="text/javascript">
var xmldoc = new ActiveXObject("Microsoft.XMLDOM")
xmldoc.async = false
xmldoc.load("data.xml")
var root = xmldoc.documentElement.childNodes
document.write("<div id='rightbox'><h2 id='subtitle'>Transportation</h2><table width='400' border='border' align='center'><tr>")
//document.write("<td width='100'>hello</td>")
for (i=0; i <= root.length-1; i++){
document.write("<td width='100'>" + root[i].tagName + "</span>")
root[i].childNodes[0].nodeValue == 'false' ? document.write("<td><input type='checkbox'></td>") : document.write("<td><input type='checkbox' checked='checked'></td>")
}
document.write("</tr></table></div>")
</script>
</head>
</html>


Patrick