Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Dynamic XForms submissions

Using JavaScript for DOM programming to decouple forms from submission targets

Kevin Kelly (kevin.kelly@us.ibm.com), Senior Technical Staff Member, IBM, Software Group
Kevin E. Kelly is an IBM senior technical staff member who works on emerging software standards for the Web and the healthcare industry. Mr. Kelly is chairman of the Worldwide Web Consortium (W3C) Compound Document Formats (CDF) Working Group, member of the W3C Hypertext Coordination Group, and has been a member of the W3C XForms Working Group. He is also a member of the Health Level 7 (HL7) organization and works on Service Oriented Architecture (SOA) initiatives in HL7 and the Object Management Group (OMG). His focus is on developing open standards-based technologies for faster, more efficient standards adoption through XML-based and model-driven technologies.
Jan J. Kratky, Advisory Software Engineer, IBM, Software Group
Jan Joseph Kratky, the development lead for the XML Forms Generator and Visual XForms Designer, is a member of the W3C XForms Working Group. A Sun Certified Java Programmer and Sun Certified Web Component Developer, Mr. Kratky has worked with Java technologies since 1997 and with Eclipse technologies since 2001. He is currently a software engineer with IBM Emerging Software Standards in Research Triangle Park, N.C.
Steve K Speicher (sspeiche@us.ibm.com), Senior Software Engineer, IBM, Software Group
Steve Speicher is an IBM Senior Software Engineer working on emerging standards, both infrastructure based and industry verticals (healthcare). He is a member of the W3C Compound Document Formats Working Group, he led the development of the Compound XML Document Toolkit, and he uses Model-Driven Development (MDD) to improve the development of standards. He has previously worked on change and release management tools in the Rational division and in IBM internal tools.

Summary:  XForms is an ideal open standards technology for collecting and submitting data from a wide variety of Web-capable platforms. Creating a single input form for collecting data from many sources is common. However, each location filling out the form may have its own site unique submission requirements such as submitting to multiple targets that are not known to the form author. Mutliple submission targets include local save locations, or submitting to a write-only "vault" for auditing or logging, or any other site-specific submission targets. Using JavaScript™ to edit the DOM with XForms allows a single form to accommodate multiple, site-unique submission requirements.

Date:  07 Nov 2006
Level:  Advanced
Also available in:   Chinese

Activity:  8234 views
Comments:  

Background

Often a single form can be developed to collect standard sets of data from many different sources. A single one-size-fits-all form for a specific data collection purpose is ideal because it can constrain data input values for data integrity and allow for easy correlation and summarization of data across a wide variety of sources. XForms is a good choice for this kind of data-driven form because it is an open standard that can run on a variety of Web-enabled platforms.

XForms

XForms is a W3C standard for data model driven forms for the Web.

The XForms document in Figure 1 is rendered in Mozilla Firefox 1.5 with the Firefox XForms extension being developed by Mozilla for Firefox.

See the Resources section for XForms links.

In order for the data to be collected, the XForms document must have a submission target, or someplace for the data to be sent or stored when the input is complete and the user clicks a submit button. However, each form filling location may have unique submission requirements, such as saving a local copy of the form submitted, or submitting a form to a write-only location or "vault" for recording each submission for auditing and logging purposes. These unique submission requirements can erode the value of a single form since a single form cannot encapsulate each form filling location's unique submission requirements. The ability to decouple the submission requirements from the standard form so that each form filling site can have as many specific unique submission targets as needed can be achieved by using JavaScript for Document Object Model (DOM) programming and joining the submission targets with the XForms document at runtime in the browser.

See the Downloads section to download the complete XForms, schemas, XML data instances, and stylesheet used in the article.

Sample XForms document

Consider a scenario like a long-term medical study, where study participants are interviewed periodically and asked about a specific set of symptoms that they may be experiencing. A simple form for recording a single subject instance response to a periodic check-up interview might look like the XForms document in Figure 1.


Figure 1. Simple incident report in XForms
Simple Incident Report Form

In this simple example form, only three input fields are being collected: a string for Subject ID, a date using xs:date type with an XForms rendered date picker to constrain the input, and an enumerated set of symptoms (none, fever, muscle pain, joint stiffness, and nausea). The schema definition for instances submitted by this XForms document is shown in Listing 1.


Listing 1. Incident reporting schema
      
...            
<xs:simpleType name="incidentType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="none" />	  
    <xs:enumeration value="fever" />
    <xs:enumeration value="muscle pain" />
    <xs:enumeration value="joint stiffness" />
    <xs:enumeration value="nausea" />	    	    
  </xs:restriction>
</xs:simpleType>

<xs:complexType name="incidentReportType">
	<xs:sequence>
		<xs:element name="theSubjectID" type="xs:string" maxOccurs="1" />
		<xs:element name="theDate" type="xs:date" maxOccurs="1" />
    	<xs:element name="theSymptom" type="incidentType" maxOccurs="1" /> 
	</xs:sequence>
</xs:complexType>

<xs:element name="theIncident" type="incidentReportType"/>
...
      

Since each input field is backed by a schema definition that explicitly defines its type, XForms can prohibit submission of the input data until all data is valid against the schema that enforces homogeneity of the data across all the submission sites. In this schema, theDate is constrained to the type xs:date and theSymptom is constrained to the type incidentType. Additionally, the maxOccurs attribute further constrains the data so that each input field will occur only once in the submitted data instance. A sample submitted instance for this form instance looks like what is shown in Listing 2.


Listing 2. Incident XML instance
       
...      
<theIncident xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="theIncident.xsd">
     	<theSubjectID>7D</theSubjectID>
     	<theDate>2006-10-13</theDate>
     	<theSymptom>none</theSymptom>
</theIncident>  
...    
      


Static XForms submit

The XForms document shown in Figure 1 has a submit button presented to the user of the form to be able to submit the data when input is complete. This submit button activation can be associated with XForms data model-specific submission actions. In this case the data model is an instance like Listing 2 based on the schema in Listing 1. In the XForms document the submission action or actions are defined with the model and instance that the actions apply to. For example, in Listing 3, two submissions are defined: a remoteSave submission that will submit the XML instance theIncidentInstance to an xformstest.org echo service, and a localSave submission that will save a copy of the theIncidentInstance on the local disk.


Listing 3. XForms model definition with submission definitions
       
...
<xforms:model id="theIncidentModel" schema="theIncident.xsd">
  <xforms:instance id="theIncidentInstance" src="theIncident.xml"/>
  <xforms:submission id="remoteSave" 
  action="http://xformstest.org/cgi-bin/showinstance.sh"
      method="post"/>
  <xforms:submission id="localSave" action="./local_save.xml"
      method="put"/>                              
</xforms:model>
...
      

A simple XForms trigger is used to create the submit button that upon activation will invoke the two (or more if defined and invoked) submission actions, as shown Listing 4.


Listing 4. Trigger for the submit button
      
...        
<xforms:trigger>
 <xforms:label>Submit</xforms:label>    
	<xforms:action ev:event="DOMActivate">
		<xforms:send submission="remoteSave"/>
		<xforms:send submission="localSave"/>
	</xforms:action>
</xforms:trigger> 
...        
        

This is a valid and useful form, but it requires the form author to know about the local submission requirements (like the local disk save) of the form filler site, which is not a scalable option for a form being used by many sites. What is needed is a way to allow local form filler sites to define local submission requirements separate from the form and the submitted instance definition.


Dynamic XForms submit

The XForms data model mechanism and JavaScript programming against the Document Object Model (DOM) presents a scalable solution that allows the form to remain unchanged, but provides each site with the flexibility to define as many unique submission requirements as needed. The first piece of the solution is a separate data model for defining and holding the submission actions, which XForms supports easily. A simple schema can be defined to hold submit action information such as the target of the submission, an identifier to identify the submission action uniquely, and an invocation method (such as put or post), as shown in Listing 5.


Listing 5. Submission actions schema
       
...             
<xs:simpleType name="methodType">
  <xs:restriction base="xs:string">
      <xs:enumeration value="put" />
          <xs:enumeration value="post" />
            </xs:restriction>
</xs:simpleType>

 <xs:complexType name="actionType">
  <xs:simpleContent>
     	<xs:extension base="xs:string">
  <xs:attribute name="theMethod" type="methodType" />
  <xs:attribute name="theId" type="xs:string" />		   		    	    	
   		</xs:extension>
   	</xs:simpleContent>
   </xs:complexType>
           
<xs:complexType name="submitListType" >
   <xs:sequenceminOccurs="0"maxOccurs="unbounded">
   <xs:element name="theAction" type="actionType" />
  </xs:sequence>
</xs:complexType> 
...	                        
       

This schema will allow instances to be created with multiple submissions populating a separate data model. A primer data model with default submission settings could be sent with the form and then edited at each site. An instance example with the two submissions options from the previous "hardcoded" XForms document submissions from Listing 3 are shown in a separate XML instance that can be used as a XForms data model in Listing 6.


Listing 6. Submission actions instance
       
...      
<theSubmitList>
	<theAction theMethod="post" theId="remoteSave">
	http://xformstest.org/cgi-bin/showinstance.s</theAction>
	<theAction theMethod="put" theId="localSave">./local_save.xml</theAction>
</theSubmitList>      
...
      

Once a separate data model for the submissions is available, then JavaScript can be used to write into the DOM at runtime and associate the instance model to be submitted with the local submission targets. The JavaScript and the two models along with an "empty" trigger are shown in Listing 7. Note that the theIncidentModel no longer has submissions defined with the model like in the static, hard-coded submissions in Listing 3.


Listing 7. JavaScript for dynamic XForms submit
       
...
    <xforms:model id="theSubmitModel" schema="theSubmits.xsd">
    	<xforms:instance id="theSubmitInstance" src="theSubmits.xml"/>
    </xforms:model>
    
    <xforms:model id="theIncidentModel" schema="theIncident.xsd">
      <xforms:instance id="theIncidentInstance" src="theIncident.xml"/>
    </xforms:model>

	<script type="">
	function buildSubmits()
	{
	  var oModel = document.getElementById("theSubmitModel");
	  var oInstance = oModel.getInstanceDocument("theSubmitInstance");

	  var pModel = document.getElementById("theIncidentModel");
	  var pInstance = pModel.getInstanceDocument("theIncidentInstance");
 	
	  var theSubmits = oInstance.getElementsByTagName("theSubmits");
	  var theSubmitList = theSubmits[0].getElementsByTagName("theSubmitList");
	  var theAction = theSubmitList[0].getElementsByTagName("theAction");

   	  var submitTrigger = document.getElementById("dynamicSubmit");

	  for(var i = 0; i < theAction.length ;i++) {
	n = document.createElementNS("http://www.w3.org/2002/xforms", "xforms:submission");
	n.setAttribute("id" , theAction[i].getAttribute("theId"));
	n.setAttribute("method" , theAction[i].getAttribute("theMethod"));		
	n.setAttribute("action" , theAction[i].textContent);
	pModel.appendChild(n);

	m = document.createElementNS("http://www.w3.org/2002/xforms", "xforms:send");
	  m.setAttribute("submission" , theAction[i].getAttribute("theId")) ;
		submitTrigger.appendChild(m);
	  }
	}
	</script>    
...
    <xforms:trigger>
	    <xforms:label>Submit</xforms:label>    
    	<xforms:action ev:event="DOMActivate" id="dynamicSubmit">
    	</xforms:action>
    </xforms:trigger>
    
  	<script type="">
		buildSubmits();
  	</script>   
...
      

DOM

The Document Object Model, or DOM, is is a platform- and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of documents.

The DOM Inspector shown in Figure 2 is FireBug 0.4, which is an extension for Mozilla Firefox 1.5. It is a great utility for DOM programming and debugging.

See the Resources section for DOM links.

The JavaScript sets up two model and instance variables for the two XForms data models, the theSubmitModel and the theIncidentModel, respectively, and then iterates through the theSubmitModel instance and adds the submissions to the theIncidentModel instance's XForms model definition in the DOM as well as adding the submission actions to the XForms submit button trigger in the DOM. The trigger to be updated does have to be uniquely identified, and in this case the trigger has an ID of dynamicSubmit to uniquely identify it. These updates happen in memory in the DOM and are never written in the XForms code itself on disk, but may been seen inline in the DOM with a DOM Inspector, as shown in Figure 2.


Figure 2. Dynamic submits shown in the DOM
Dynamic Submits shown in the DOM

Submission editing form

Since the submissions are now being kept in a separate data model and file on disk, this separate instance of submits now becomes a candidate for its own XForms document, which can be easily generated from the XML instance and backing schema itself with the XML Forms Generator (see the Resources section for a link to the XML Forms Generator Eclipse plugin). The resultant XForms documents are shown in Figure 3.


Figure 3. Submission Editing XForms
Submission Editing XForm

Conclusion

XForms is an excellent technology choice for creating data-driven forms capable of applying schema-driven constraints to XML instance data submissions where homogenous data is being collected from many sources that needs to be correlated and summarized quickly and efficiently. By decoupling the form filler site unique data submission requirements from the logical form itself and the submitted instance you get the control of a single form and a single set of data entry constraints with the flexibility of mutliple, site-unique submission options. This decoupled approach with a runtime joining in the DOM of the form and the mutliple, site-specific submission requirements can be done with JavaScript programming by reading mutliple submissions option from a separate model and writing those options to the XForms submitted instance model and XForms submit button trigger in the DOM.

XForms 1.0 requires the use of JavaScript programming against the DOM to support this kind of dynamic mutliple submission target scenario, but there is a declarative solution being considered by the W3C XForms Working Group for the XForms 1.1 Working Draft. Specifically, a resource child element for the XForms submission element that will allow a URI to be used for dynamically calculating the submission based on instance data.



Download

DescriptionNameSizeDownload method
Complete examples for this articledynamicXFormsSubmit.zip5KB HTTP

Information about download methods


Resources

Learn

Get products and technologies

Discuss

About the authors

Kevin E. Kelly is an IBM senior technical staff member who works on emerging software standards for the Web and the healthcare industry. Mr. Kelly is chairman of the Worldwide Web Consortium (W3C) Compound Document Formats (CDF) Working Group, member of the W3C Hypertext Coordination Group, and has been a member of the W3C XForms Working Group. He is also a member of the Health Level 7 (HL7) organization and works on Service Oriented Architecture (SOA) initiatives in HL7 and the Object Management Group (OMG). His focus is on developing open standards-based technologies for faster, more efficient standards adoption through XML-based and model-driven technologies.

Jan Joseph Kratky, the development lead for the XML Forms Generator and Visual XForms Designer, is a member of the W3C XForms Working Group. A Sun Certified Java Programmer and Sun Certified Web Component Developer, Mr. Kratky has worked with Java technologies since 1997 and with Eclipse technologies since 2001. He is currently a software engineer with IBM Emerging Software Standards in Research Triangle Park, N.C.

Steve Speicher is an IBM Senior Software Engineer working on emerging standards, both infrastructure based and industry verticals (healthcare). He is a member of the W3C Compound Document Formats Working Group, he led the development of the Compound XML Document Toolkit, and he uses Model-Driven Development (MDD) to improve the development of standards. He has previously worked on change and release management tools in the Rational division and in IBM internal tools.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=172775
ArticleTitle=Dynamic XForms submissions
publish-date=11072006
author1-email=kevin.kelly@us.ibm.com
author1-email-cc=clarkega@us.ibm.com
author2-email=kratky@us.ibm.com
author2-email-cc=dwxed@us.ibm.com
author3-email=sspeiche@us.ibm.com
author3-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers