View Single Post
  #5 (permalink)  
Old May 11th, 2009, 02:04 PM
Martin Honnen Martin Honnen is online now
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
Default

Here is an example, the stylesheet puts an XPath to the XML element into the input type="checkbox"'s name attribute, then the onclick handler simply updates the element as needed.
XML sample document:
Code:
<data>
  <item>
    <foo>true</foo>
  </item>
  <item>
    <foo>false</foo>
  </item>
  <item>
    <foo>true</foo>
  </item>
</data>
XSLT stylesheet:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output method="html" indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="data">
    <table border="1">
      <thead>
        <tr>
          <xsl:apply-templates select="item[1]/*" mode="th"/>
        </tr>
      </thead>
      <tbody>
        <xsl:apply-templates select="item"/>
      </tbody>
    </table>
  </xsl:template>
  
  <xsl:template match="item/*" mode="th">
    <th>
      <xsl:value-of select="name()"/>
    </th>
  </xsl:template>
  
  <xsl:template match="item">
    <tr>
      <xsl:apply-templates>
        <xsl:with-param name="p" select="position()"/>
      </xsl:apply-templates>
    </tr>
  </xsl:template>
  
  <xsl:template match="item/foo">
    <xsl:param name="p"/>
    <td>
      <input type="checkbox"
             name="{concat(name(..), '[', $p, ']/', name())}"
             value="{.}"
             onclick="updateCheckbox(this);">
        <xsl:if test=". = 'true'">
          <xsl:attribute name="checked">checked</xsl:attribute>
        </xsl:if>
      </input>
    </td>
  </xsl:template>

</xsl:stylesheet>
HTA code with javascript:
Code:
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HTA test</title>
<script type="text/javascript">
var msxmlVersion = '3.0';

var doc, sheet;

function updateCheckbox(cb)
{
  doc.documentElement.selectSingleNode(cb.name).text = cb.checked ? 'true' : 'false';
}

function load()
{
  doc = new ActiveXObject('Msxml2.DOMDocument.' + msxmlVersion);
  doc.async = false;
  doc.load('test2009051103.xml');
  doc.setProperty('SelectionLanguage', 'XPath');
  
  sheet = new ActiveXObject('Msxml2.DOMDocument.' + msxmlVersion);
  sheet.async = false;
  sheet.load('test2009051102Xsl.xml');
  
  document.getElementById('ph1').innerHTML = doc.transformNode(sheet);
}

window.onload = load;
</script>
</head>
<body>
<h1>Example</h1>

<div id="ph1"></div>

<div>
<input type="button" value="save" onclick="doc.save('test2009051103Output.xml')">
</div>
</body>
</html>
For testing I let the HTA save to a different file than the one the data is loaded from but you could of course change that once you have everything set up as needed.
__________________
Martin Honnen
Microsoft MVP - XML
My blog
Reply With Quote