Re: data export for subsequent transformation to csv

  I hope this message reaches you as your email address doesn't seem 
accurate, but perhaps you'll see it on the list.

There are a couple of issues with your form:

1. The instance should start out with elements for all the elements you 
want in the result.  In general, XForms form controls only change the 
text values of elements and attributes, and don't add them.  (There are 
a few cases where they do get added by controls and actions, but in the 
case of your form, they don't.)

2. The use of namespaces in the instance isn't quite right.   You might 
want to read up on an XML book on the topic, or just take a look at what 
I've done and generalize.

3. Each form control needs a "ref" attribute with the path to the 
element in the instance where you want the data placed.   I've put some 
in that work, but you can of course change the instance data around.  If 
you need to move the elements down a level inside other elements, you'll 
need to use the XPath syntax "a/b/c" to get to element "c" inside "b" 
inside "a".  In XForms, you get the toplevel element name for free.

That ought to be make it work.  Here's the output I got once I made 
those changes:

|<generalinformation><purpose>Building</purpose><weatherLlocation>London</weatherLocation></generalinformation>|

Here's the updated form, and below that are some enhancements you might 
consider:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:xf="http://www.w3.org/2002/xforms">
<head>
<style type="text/css">
       code {text-color:black; background:white; font-family:verdana, 
sans-serif}
</style>
<xf:model>
<xf:instance>
<generalinformation xmlns="">
<purpose />
<weatherLocation />
</generalinformation>
</xf:instance>
<xf:submission resource='file:testgeneral.xml' method="put" id="gi" />
</xf:model>
<title>
       test xml to csv for postgresql
</title>
</head>
<body>
<p>
<xf:select1 ref="purpose" appearance='minimal'>
<xf:label>purpose</xf:label>
<xf:item>
<xf:label>England</xf:label>
<xf:value>Building</xf:value>
</xf:item>
<xf:item>
<xf:label>Scotland</xf:label>
<xf:value>Building</xf:value>
</xf:item>
<xf:item>
<xf:label>Northern Ireland</xf:label>
<xf:value>Building</xf:value>
</xf:item>
<xf:item>
<xf:label>Energy England</xf:label>
<xf:value>joule</xf:value>
</xf:item>
<xf:item>
<xf:label>Energy Wales</xf:label>
<xf:value>joule</xf:value>
</xf:item>
<xf:item>
<xf:label>Energy Scotland</xf:label>
<xf:value>joule</xf:value>
</xf:item>
<xf:item>
<xf:label>Energy Northern Ireland</xf:label>
<xf:value>joule</xf:value>
</xf:item>
</xf:select1>
</p>
<p>
<xf:select1 ref="weatherLocation" appearance='minimal'>
<xf:label>Weather location</xf:label>
<xf:item>
<xf:label>London</xf:label>
<xf:value>London</xf:value>
</xf:item>
</xf:select1>
</p>
<xf:submit submission='gi'>
<xf:label>Send</xf:label>
</xf:submit>
</body>
</html>

4. Not all XForms processors support method="put" to file: URIs, but 
yours apparently does.  I suspect this is just a test though and you'll 
be using put or post to an http: URI eventually.
|
|
5. You might also want to keep the form from being submitted until the 
user chooses some data.

You can do this in two ways:

6a. You can start off with initial data:

<xf:instance>
<generalinformation xmlns="">
<purpose>Building</purpose>
<weatherLocation>London</weatherLocation>
</generalinformation>
</xf:instance>

6b. Or you can use a "bind" element to say that the values must be 
non-empty (required):

<xf:model>
<xf:instance>
<generalinformation xmlns="">
<purpose />
<weatherLocation />
</generalinformation>
</xf:instance>
<xf:bind nodeset="purpose" required="true()" />
<xf:bind nodeset="weatherLocation" required="true()" />
<xf:submission resource='file:testgeneral.xml' method="put" id="gi" />
</xf:model>

6c. You can do both!

7.  There's another slight issue you might encounter if you decide to 
use this same style of form to edit existing data.  Note that 
information gets lost in the "purpose" selection.  A user might pick 
"Scotland", but the data that's produced is indistinguishable from 
England or Northern Ireland, so if you were to load the form again and 
provide the instance data with those values, the form would show England 
(since it's the first match to the value "Building"). There are ways to 
do this mid-tier logic inside XForms, but you might consider putting 
this decision logic into your server side, and letting the form just 
collect the data (England, Scotland, Energy Wales, etc.)

Please write back if you have questions.

Leigh.
||
On 08/12/2010 03:28 AM, e-letter wrote:
> Readers,
>
> I am trying to create a form to save data, for eventual import to a
> postgresql database. The following file was created:
>
> <...
>
> If I selected 'England' and 'London' in the form, I was expecting an
> xml file containing these values and I expected to need to create an
> xslt file for a csv file to be produced. What is my mistake to create
> the xml file?
>
> yours,
>
> xforms@conference.jabber.org
>

Received on Thursday, 12 August 2010 18:32:00 UTC