Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 13th, 2011, 03:26 PM
Registered User
 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to HTML and back

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
 
Old March 13th, 2011, 04:39 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This looks to me like a case for XForms. Rather than transforming HTML back to XML, think in terms of the editing events on the HTML updating the XML instance - and that's essentially what XForms does.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 15th, 2011, 09:17 PM
Registered User
 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
think in terms of the editing events on the HTML updating the XML instance
Thanks - I get the picture.

Chris





Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting Textarea content back into HTML sdagger Classic ASP Basics 3 February 9th, 2010 07:25 PM
How to post a XML file to a remote server and get a XML response back surendran ASP.NET 3.5 Professionals 2 January 29th, 2010 03:39 AM
Regarding xml-html transformation of an xml string using xslt and javascript suprakash444 XSLT 1 January 12th, 2009 01:23 AM
using for each and going back in the xml tree smiter XSLT 2 January 10th, 2007 05:57 AM
how to post xml data to server and reading back js_pandey PHP How-To 0 March 29th, 2006 01:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.