This tip assumes that you are familiar with XML, and at least have a passing familiarity with XML Schema. The example uses XForms, but an understanding of XForms is not required. (If you want to run the example, you can download the XForms extension for Firefox at http://www.mozilla.org/projects/xforms/. )
The ability to validate XML, or ensure that it satisfies some restriction based on type, is inherent in the language itself; Document Type Definitions are part of the XML specification. Before long, it was obvious that more flexibility and power were needed, and thus was born XML Schema. XML Schema enables you to create definitions with a large degree of specificity. You can specify that an element must contain a specific number of additional elements, or an attribute must contain a datetime value, text that follows a certain pattern, or any variety of other structures.
For example, consider this XForms form (see Listing 1).
Listing 1. The basic form
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<head>
<title>Contact Form</title>
<xforms:model>
<xforms:instance id="content">
<contact xmlns="">
<name />
<email />
<phone />
</contact>
</xforms:instance>
<xforms:submission id="submitContact" method="post"
action="." />
</xforms:model>
<link href="gen_default.css" rel="stylesheet"/>
</head>
<body>
<h1 align="center">Contact information</h1>
<xforms:input ref="/contact/name">
<xforms:label>Name: </xforms:label>
</xforms:input>
<br />
<xforms:input ref="/contact/email">
<xforms:label>Email: </xforms:label>
</xforms:input>
<br />
<xforms:input ref="/contact/phone">
<xforms:label>Phone: </xforms:label>
</xforms:input>
<br />
<xforms:submit submission="submitContact">
<xforms:label>Submit</xforms:label>
</xforms:submit>
</body>
</html>
|
The form is very simple, looking for just the name, e-mail address, and phone number of the user, as you can see in Figure 1.
Figure 1. The form
In this case, it is helpful to constrain the entries for the e-mail and phone number to be valid e-mail addresses and phone numbers—or at least to be in the proper form for them. To do that, you need to add a schema to the document (see Listing 2).
Listing 2. Adding a schema
...
<xforms:model>
<xsd:element name="contact">
<xsd:complexType>
<xsd:element name="name" xsd:type="xsd:string" />
<xsd:element name="email" />
<xsd:element name="phone" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xforms:instance id="content">
<contact xmlns="">
<name />
<email />
<phone />
</contact>
</xforms:instance>
...
|
You can then add events that tell the user when one of these entries is invalid (see the source code for how to do that).
Notice that the e-mail and phone number definitions currently have no type attached. This is because checking for these types is, frankly, more trouble than most programmers like to engage in. E-mail addresses can have several forms, even though there is a standard format. Ditto for phone numbers, with seemingly endless variations, such as "with area code", "without area code", "with extension", and so on, and that doesn't even take into account country codes.
Who wants to do all that? I certainly don't. Fortunately, there exists a library of common types such as phone numbers and e-mail addresses. You can download the XML schema standard type library from http://www.codesynthesis.com/projects/xsstl/. Adding it to your schema is a matter of importing the appropriate namespaces and documents (see Listing 3).
Listing 3. Importing the schema definitions
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:stl="http://www.codesynthesis.com/xmlns/xsstl">
<head>
<title>Contact Form</title>
<xforms:model>
<xsd:schema targetNamespace=""
xmlns:stl="http://www.codesynthesis.com/xmlns/xsstl">
<xsd:import
namespace="http://www.codesynthesis.com/xmlns/xsstl"
schemaLocation="xsstl.xsd"/>
<xsd:element name="contact">
<xsd:complexType>
<xsd:element name="name" xsd:type="xsd:string" />
<xsd:element name="email" xsd:type="stl:EmailAddress" />
<xsd:element name="phone" xsd:type="stl:PhoneNumber" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xforms:instance id="content">
<contact xmlns="">
<name />
<email />
<phone />
</contact>
</xforms:instance>
<xforms:bind nodeset="/contact/email"
type="stl:EmailAddress"/>
<xforms:bind nodeset="/contact/phone"
type="stl:PhoneNumber"/>
<xforms:submission id="submitContact" method="post"
action="." />
...
|
You can use the technique you see here in any schema document, not just one embedded in
an XForms form. Be sure to define the appropriate namespace alias (in this case, stl:), and to import the actual namespace and schema document. You can then use these types just as you would any other schema-defined types. In this case, the form uses the EmailAddress and PhoneNumber types.
This example imports the main document, making all of the types available. You can, however, only import the documents you actually need. The XML Schema Standard Type Library contains several documents with a single type:
- email-address.xsd: The
EmailAddresstype constrains data to a standard e-mail address. - percentage.xsd: The
Percentagetype represents a number between 0 and 100, inclusive. - iso3166-country-code.xsd: The
ISO3166CountyCodetype both constrains the data to be a two-letter string and requires it to be one of the actual ISO 3166-1 country codes. - rfc822-date-time.xsd: The
RFC822DateTimetype requires data to be properly formatted as specified by RFC822.
Other documents contain variations on a single type:
- ipv4-address.xsd: This file contains variations on a proper endpoint, such as
IPv4Address,Port, andIPv4Endpoint, which represents a proper IP address (in dot notation) and a port. It also includesIPv4EndpointStruct, a struct version of the data rather than a single string. - phone-number.xsd: This document specifies the various parts of a phone number,
PhoneCountryCode,PhoneAreaCode,PhoneSubscriberNumber, andPhoneExtensionNumber, as well asPhoneNumber, a single string combining them all and requiring at least the subscriber number, andPhoneNumberStruct, a struct that separates the parts into individual elements. - us-state-code.xsd: This document includes several variations on the definition of a "US state", including
USStateTerritoryCode(which encompasses the 50 states and 9 US territories),USStateCode(just the states and Washington, DC),USTerritoryCode(just the territories, such as the US Virgin Islands, but not DC),USContinentalStateCode(all the states except Hawaii, plus DC) andUSContiguousStateCode(all the states except for Hawaii and Alaska, plus DC).
The XML Schema Standard Type Library is an example of creating modular definitions to make your life easier. When you import them into your own schema, you can use these types without the trouble of defining them yourself.
| Description | Name | Size | Download method |
|---|---|---|---|
| Sample code | x-tipxsslt-source.zip | 1KB | HTTP |
Information about download methods
Learn
-
XML
Schema validation in Xerces-Java 2 (Nicholas Chase, developerWorks, July 2002): Learn about XML Schema and the process of using schema validation with Xerces-Java 2.0.
-
Introduction to
XForms, Part 1: The new Web standard for forms (Chris Herborth, developerWorks, September 2006): Read an article that covers how XForms actually works and shows you how to set up XForms with Firefox and Microsoft Internet Explorer so you can view your XForms samples.
-
XML Schema
Recommendation: Read about the XML Schema facilities, and how to create schemas using the XML Schema language.
-
XForms Recommendation: Be
informed about XHTML forms, its division into three parts—XForms model, instance
data, and user interface— to separates presentation from content, allows reuse,
reduces round-trips to the server, and offers device independence and less scripting.
-
IBM XML certification: Find out how you can become an IBM-Certified Developer in XML and related technologies.
-
XML technical library: See the developerWorks XML Zone for a wide range of technical articles and tips, tutorials, standards, and IBM Redbooks.
-
developerWorks technical events and webcasts: Stay current with technology in these sessions.
- The technology
bookstore: Browse for books on these and other technical topics.
Get products and technologies
-
XML Schema Standard
Type Library: Download this collection of universally-useful data types defined in the W3C XML Schema language.
-
XForms extension for
Firefox: Download this extension for implementing the W3C XForms 1.0 SE recommendation
in Mozilla.
-
IBM trial software: Build your next development project with trial software available for download directly from developerWorks.
Discuss
-
XForms
technology forum: Get your XForms questions answered.
-
developerWorks blogs: Check out these blogs and get involved in the developerWorks community.
-
XML zone discussion forums: Participate in any of several XML-related discussions.
Nicholas Chase has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, an Oracle instructor, and the Chief Technology Officer of an interactive communications company. He is the author of several books, including XML Primer Plus (Sams).
Comments (Undergoing maintenance)





