XForms/Read and write with get and put

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

Sometimes all you need to do is to put a nice user friendly form that edits a single static XML file. In this case a static file is any file where you know the exact pathname to the file when the form is created and you know that the file name will never change. This is the case when an application has a configuration file in a known location (either absolute or fixed relative to the XForm).

If this is a local file on your hard drive this is very easy to do just by using get and put operations and either absolute path names to the file or relative path names to the directory that the form is located.

XForms also allows a user to read data into a form using a HTTP get and to write data from a file using HTTP put operations using a remote web server. Just make sure you enable put operations on your web server and that the file you are putting is writeable This means you will need to do a chmod +w if it is a UNIX file.

Screen Image[edit | edit source]

Example of reading and writing a static file using XForms

Sample Program[edit | edit source]

<html
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xf="http://www.w3.org/2002/xforms"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <head>
      <title>Submission with get and put</title>
      <xf:model>
         <xf:instance id="data-instance" src="data.xml" xmlns="" />
         <xf:submission id="read-from-file" method="get"
             action="data.xml" replace="instance" instance="data-instance" />
         <xf:submission id="save-to-file" method="put"
             action="data.xml" replace="instance" instance="data-instance" />
      </xf:model>
   </head>
   <body>
      <p>Demonstration of using XForms to get and put data to local file using the submission element.</p>
      <xf:input ref="Element1">
         <xf:label>Elementu 1:</xf:label>
      </xf:input>
      <br />
      <xf:input ref="Element2">
         <xf:label>Elementul 2:</xf:label>
      </xf:input>
      <br />
      <xf:input ref="Element3">
         <xf:label>Elementuul 3:</xf:label>
         <br />
      </xf:input>
      <xf:submit submission="read-from-file">
         <xf:label>Reload</xf:label>
      </xf:submit>
      <xf:submit submission="save-to-file">
         <xf:label>Save</xf:label>
      </xf:submit>
   </body>
</html>

Sample data.xml Instance File[edit | edit source]

The following file is read into memory (from the same folder that the form was executed in) when the form is loaded into the browser. This file is written to the disk when the save button is pressed. The reload button will reload the file into the browser.

<?xml version="1.0" encoding="UTF-8"?>
<Data>
   <Element1>One</Element1>
   <Element2>Two</Element2>
   <Element3>Three</Element3>
</Data>

Discussion[edit | edit source]

Note that this form is not applicable when the form data is in a file name that is selected at run time. This is because the src attribute in instance is not dynamic. You can not use an XPath expression in the src attribute.

Here is an example of how files could be dynamically loaded in XForms 1.1:

<!-- sample code that could be used in XForms 1.1 -->
<xf:submission id="read-from-file" method="get" replace="instance" instance="data-instance">
   <xf:resource value="instance('my-instance')/FileNameSetByUpload"/>
</xf:submission>

Note that if this was the case, any form from a nefarious web site might be able to look for a known files or suspicious such as my-passwords.doc and upload them without your consent. This restriction may be eased if the "resource" attribute (part of the XForms 1.1 draft) gets implemented.

References[edit | edit source]

XForms 1.1 standard on submit resource - This allows the resource path to be dynamically created based on instance data.