I'm trying to figure out how to transform XML to HTML and then back.
Here's my XML sample:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="f1.xsl"?>
<settings>
<txt_setting id="s1" desc="Max message length" cont_type="int"></txt_setting>
<chk_setting id="s2" desc="Log warnings"></chk_setting>
<opt_setting id="s3" desc="Log to" cont_type="opt_1"></opt_setting>
<txt_setting id="s4" desc="Log since" cont_type="dt"></txt_setting>
<txt_setting id="s5" desc="Error percentage" cont_type="dbl"></txt_setting>
</settings>
I'm transforming it to HTML via:
Code:
<?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><head></head><body>
<xsl:apply-templates/></body></html>
</xsl:template>
<xsl:template match="settings">
<table>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="txt_setting">
<xsl:variable name="id">
<xsl:value-of select="@id"/>
</xsl:variable>
<xsl:variable name="cont_type">
<xsl:value-of select="@cont_type"/>
</xsl:variable>
<tr><td><xsl:value-of select="@desc"/></td>
<td><input name="{$id}" value="" type="text" cont_type="{@cont_type}" /></td></tr>
</xsl:template>
<xsl:template match="chk_setting">
<xsl:variable name="id">
<xsl:value-of select="@id"/>
</xsl:variable>
<tr><td><xsl:value-of select="@desc"/></td>
<xsl:if test="@value='Y'">
<td><input name="{@id}" type="checkbox" checked="Y" /></td>
</xsl:if>
<xsl:if test="not(@value='Y')">
<td><input name="{@id}" type="checkbox" /></td>
</xsl:if>
</tr>
</xsl:template>
<xsl:template match="opt_setting[@cont_type='opt_1']">
<xsl:variable name="id">
<xsl:value-of select="@id"/>
</xsl:variable>
<xsl:variable name="val">
<xsl:value-of select="@value"/>
</xsl:variable>
<tr>
<td><xsl:value-of select="@desc"/></td>
<xsl:choose>
<xsl:when test="@value='F'">
<td><input name="{@id}" type="radio" value="F" checked="Y" /> File</td>
<td><input name="{@id}" type="radio" value="D" /> Database</td>
</xsl:when>
<xsl:when test="@value='D'">
<td><input name="{@id}" type="radio" value="F" /> File</td>
<td><input name="{@id}" type="radio" value="D" checked="Y" /> Database</td>
</xsl:when>
<xsl:otherwise>
<td><input name="{@id}" type="radio" value="F" /> File</td>
<td><input name="{@id}" type="radio" value="D" /> Database</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
As you can see, the idea is that the HTML version would allow the user to change values, at which point I'd like to transform it back to the same XML I started from. Only saving user input, of course.
If this proved practical, I could embed browser control in my executable and allow users to edit configuration files (which are stored as XML) via this double transformation. If format of a file changed, I'd alter the XSL files, but wouldn't have to redo the UI and recompile the executable.
Thoughts and ideas appreciated. Thanks!
Chris