Hub Nexus
著者著者はまだいません引き受ける

改善できるところがありますか?変更を提案しましょう。

支援

マガジン XForms · 137 ページ中 106 ページ目

Motivation

XML Schemas contain a large amount of data element constraint information other than just the data type. These restrictions on a specific data type are called "facets". Different data types have different types of facets. For example strings may have minimum and maximum length values and integers may have minimum and maximum numeric values.

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-Smiles implementation of XForms.

Note that the second field is not at least three characters long so the field is invalid and the background it set to pink according to the CSS style for invalid fields.

XML Schema (schema.xsd)

Here is a simple XML Schema with four strings. The first one is only allowed to be two characters long. The second must have a minimum length of three characters. The third string must not be over five characters long and the fourth one must be between five and seven characters long to be valid.

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

A demonstration of XML Schema string facets.

name="string-length-2">

base="xs:string">
value="2"/>

name="string-min-3">

base="xs:string">
value="3"/>

name="string-max-5">

base="xs:string">
value="5"/>

name="string-5-7">

base="xs:string">
value="5"/>
value="7"/>

Sample Instance (instance.xml)

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema.xsd">
MN
abcd
abcd
abcdef

Sample Program

The following program loads both the XML Schema and initial instance data from two external files (schema.xsd and instance.xml). The initial data in this example is valid but you can change the data to test the validation.

<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>Facet validation using X-Smiles.</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: medium;
padding-right: 5px;
width: 130px;
}

*:required {
background-color: yellow;
}

*:invalid {
background-color: pink;
}
</style>
<xf:model id="test" schema="schema.xsd">
<xf:instance src="instance.xml" />
</xf:model>
</head>
<body>
<xf:group model="test" nodeset="/Data">
<p>
<xf:input ref="string-length-2" incremental="true">
<xf:label>Length exactly 2:</xf:label>
</xf:input>
</p>
<p>
<xf:input ref="string-min-3" incremental="true">
<xf:label>Length 3 or more:</xf:label>
</xf:input>
</p>
<p>
<xf:input ref="string-max-5" incremental="true">
<xf:label>Length 5 or less:</xf:label>
</xf:input>
</p>
<p>
<xf:input ref="string-5-7" incremental="true">
<xf:label>Length 5 to 7:</xf:label>
</xf:input>
</p>
</xf:group>
</body>
</html>

The two lines that load the XML Schema and instance data are:

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

Also note that the input fields have the incremental attribute set to true. This allows character-by-character validation. The form is revalidated with each key stroke.

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.

このページを含むマガジンXForms

言語English

ライセンス: CC BY-SA 4.0 · 出典 en.wikibooks.org

1

0

0

0

Spinner Logo

コメント

Spinner Logo
バージョン: 2CC0 1.0 — public domain
The runaway star that left the Tarantula Nebula
バージョン: 2CC0 1.0 — public domain
The Blackwell School, where segregation had no law behind it
バージョン: 2CC0 1.0 — public domain
The Eagle Nebula, seen in the infrared
バージョン: 2CC0 1.0 — public domain
The house where the Equal Rights Amendment was written
バージョン: 2CC0 1.0 — public domain
The Aleutians, the forgotten front of the Second World War
バージョン: 2CC0 1.0 — public domain
The Cosmic Cliffs are not cliffs