Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
Old March 13th, 2011, 04:39 PM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.