Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 3 min read

Support

Motivation

Many times the data types of your data are defined in an external XML Schema file. To use this information, rather than having to explicitly bind the instance to a data type, you can just load the XML Schema file to initialize instance data in the model. The data types will be inferred from the XML Schema file.

To do this you must add a schema attribute to the model where the value of the argument is the XML Schema source file. Note that the argument is schema NOT src. src is used to read in the instance data.

id="test" schema="data-types.xsd">
src="instance-data.xml"/>

Screen Image

Here is a screen image of a sample program. Note that input form for the date and boolean data types are automatically inferred from the XML Schema and different controls are placed in the user interface.

Note that in this example, the order of the controls is also changed.

Load Datatypes from XML Schema

XML Schema diagram

The following is an XML Schema diagram with data types visible. Note that the data type is displayed directly under the data element. This diagram also shows optional data elements using a dashed line and the cardinality of the data elements.

Sample Program

<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>Loading data from an XML Schema file</title>
<style type="text/css">
@namespace xf url("http://www.w3.org/2002/xforms");
xf|input {
display: table-row;
line-height: 2em;
}

xf|label {
display: table-cell;
text-align: right;
font-family: Ariel, Helvetica, sans-serif;
font-weight: bold;
font-size: small;
padding-right: 5px;
width: 150px;
}

*:required {
background-color: yellow;
}

*:invalid {
background-color: pink;
}
</style>
<xf:model id="test" schema="data-types.xsd">
<xf:instance src="instance-data.xml"/>
</xf:model>
</head>
<body>
<xf:group model="test" nodeset="/Data">
<xf:input ref="date">
<xf:label>Date:</xf:label>
</xf:input>
<xf:input ref="string-required">
<xf:label>Required String:</xf:label>
</xf:input>
<xf:input model="test" ref="string-optional">
<xf:label>Optional String:</xf:label>
</xf:input>
<xf:repeat nodeset="string-unbounded">
<xf:input ref=".">
<xf:label>Unbounded String:</xf:label>
</xf:input>
</xf:repeat>
<xf:input ref="integer">
<xf:label>Integer:</xf:label>
</xf:input>
<xf:input ref="positive-integer">
<xf:label>Positive Integer:</xf:label>
</xf:input>
<xf:input ref="short">
<xf:label>Short:</xf:label>
</xf:input>
<xf:input ref="byte">
<xf:label>Byte:</xf:label>
</xf:input>
<xf:input ref="unsignedByte">
<xf:label>Unsigned Byte:</xf:label>
</xf:input>
<xf:input ref="boolean">
<xf:label>Boolean:</xf:label>
</xf:input>
</xf:group>
</body>
</html>

XML Schema

xmlns:xs="http://www.w3.org/2001/XMLSchema">
name="Data">

A listing of 10 XML Schema data types

name="date" type="xs:date"/>
name="string-required" type="xs:string"/>
name="string-optional" type="xs:string" minOccurs="0"/>
name="string-unbounded" type="xs:string" maxOccurs="unbounded"/>
name="integer" type="xs:integer"/>
name="positive-integer" type="xs:positiveInteger"/>
name="short" type="xs:short"/>
name="byte" type="xs:byte"/>
name="unsignedByte" type="xs:unsignedByte"/>
name="boolean" type="xs:boolean"/>

XML Instance

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="data-types.xsd">
2006-09-21
The quick brown fox jumped over the lazy dog.
This string is optional.
One of many.
Two of many.
Three of many.
-123
2
4096
127
255
true

Testing

One of the ways to test if the data types are loaded correctly from the XML Schema file is to attempt to enter an invalid value of a specific data type. XForms should automatically check to see if the input field meets the data requirements specified in the XML Schema file.

For example in the positive integer fields you can attempt to enter a "-1". After you do this and enter a "tab" the background of the form should display in pink as a warning. This is how the screen should look:

The way you display invalid controls is controled by the following line in the CSS file:

*:invalid {
background-color: red;
}

This is known as a pseudo element. Although there are no data elements called "invalid", XForms just adds this property to each input control and the style sheet then adds whatever properties you give it in the style sheet.

Discussion

This program also uses a CSS to display the data elements.

Known bugs

Note that under the bindings do not work if there is no sample instance data.

In addition to this section, it may not be supported in other Xform platforms.
Tested in XSLTForms, it does not work, with latest version as of 06/09/10
Used FormFaces, it worked, but could not render the css correctly.

There are also cross-browser compatibility issues as well.

References

http://betterform.wordpress.com/using-schema-datatypes/ betterForm inline schema types

Where this page came from

This page was imported from Wikibooks. From “XForms” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

Get each new page of

XForms

Following means one email when a page is added, nothing when a page changes.

Following means one email when a page is added, nothing when a page changes.

Comments

Spinner Logo

Next in XForms

CC BY-SA 4.0
XForms, Validate with schema types
CC BY-SA 4.0
This program demonstrates how XForms can validate a data set based on the facets within and XML Schema. ## Warnings Note: This program does not run under the FireFox 0.6 extension. The X-Smiles browser was used to demonstrate this program. ## Screen Image This screen image was taken from the [X-Smil
CC BY-SA 4.0
XForms, SVG
CC BY-SA 4.0
But unlike most JavaScript functions, it will not be loaded into the browser until it is needed. ## Sample Program Here is an example of using the load element within an XForms trigger: First you will need a small JavaScript function to test like the following: <script type="text/javascript"> var My
CC BY-SA 4.0
XForms, Pie Chart
CC BY-SA 4.0
For a simple Pie Chart these might include: cht=p &chd=t:10,20,30,40,20 &chl=Amount|Indicator|Code|Date|Text &chs=400x300 Where: ** - is the Chart API's base URL The ampersand (&) separates parameters. **cht=p** is a code for the chart's type. For example p=2D Pie Chart and p3-3D Pie Chart **chd=t:1