XML Schema Types: Simple and Complex Data Structures
In the world of XML, ensuring data integrity is paramount. While Document Type Definitions (DTDs) provide basic structure, XML Schema (XSD) offers a far more powerful approach by constraining the actual textual values that can appear within elements and attributes. This precision allows developers to ensure that a date field contains a valid date or that a price field contains a decimal number, rather than arbitrary text.
Key Facts
- XSD provides 19 primitive data types as the foundation for all other types.
- Simple types constrain the values of elements or attributes.
- Complex types define the structure, including child elements and attributes.
- New types can be created via restriction, list, or union mechanisms.
- XSD 1.1 introduces assertions using XPath 2.0 for arbitrary constraints.
Simple Types
Simple types, often referred to as data types, are used to limit the values allowed in an element or attribute. Unlike complex types, simple types cannot contain other elements or attributes.
Primitive Data Types
XSD defines 19 primitive data types that serve as the building blocks for data validation. These include anyURI, base64Binary, boolean, date, dateTime, decimal, double, duration, float, hexBinary, gDay, gMonth, gMonthDay, gYear, gYearMonth, NOTATION, QName, string, and time.
Beyond these primitives, the specification defines 25 derived types, and users can define their own custom derived types within their schemas.
Constructing New Simple Types
Developers can create specialized data types from primitives using three primary mechanisms:
- Restriction: Reducing the set of permitted values (e.g., setting a minimum or maximum value, using regular expressions, or limiting string length and decimal digits).
- List: Allowing a sequence of values.
- Union: Allowing a choice of values from several different types.
With the advent of XSD 1.1, assertions were added, enabling the use of XPath 2.0 expressions to specify arbitrary constraints on the data.
[ไม่มีภาพประกอบ]
Complex Types
Complex types are used to describe the permitted content of an element. This includes the element's child elements, text children, and its associated attributes. A complex type definition consists of a content model and a set of attribute uses.
Content Models
The content model determines what can reside inside an element. There are four primary varieties:
- Element-only content: No text is allowed, except for whitespace or text contained within a child element.
- Simple content: Text is permitted, but child elements are forbidden.
- Empty content: Neither text nor child elements are allowed.
- Mixed content: Both text and child elements are permitted to appear.
Deriving Complex Types
Complex types can be evolved from existing ones through two methods:
- Restriction: Disallowing certain elements, attributes, or values that the base type originally permitted.
- Extension: Adding new attributes and elements to the base type.
Similar to simple types, XSD 1.1 allows complex types to be constrained by assertions—XPath 2.0 expressions that must evaluate to true based on the element's content.
[ไม่มีภาพประกอบ]
Summary of XSD Type Structures
| Feature | Simple Types | Complex Types |
|---|---|---|
| Primary Purpose | Constrain textual values | Define element structure and content |
| Child Elements | Not allowed | Allowed (depending on content model) |
| Attributes | Cannot have attributes | Can define attribute uses |
| Creation Methods | Restriction, List, Union | Restriction, Extension |
| XSD 1.1 Feature | XPath 2.0 Assertions | XPath 2.0 Assertions |
Frequently Asked Questions
What is the main difference between a simple type and a complex type?
A simple type constrains the value of an element or attribute and cannot contain other elements or attributes. A complex type defines the overall structure of an element, including its child elements and attributes.
How many primitive data types are available in XSD?
XSD provides 19 primitive data types, ranging from basic strings and booleans to more specific types like dateTime, decimal, and anyURI.
What are the four types of content models for complex types?
The four models are element-only content (no text), simple content (text only), empty content (neither text nor elements), and mixed content (both text and elements).
How does XSD 1.1 improve data validation?
XSD 1.1 introduces assertions, which allow developers to use XPath 2.0 expressions to create arbitrary, complex constraints that must evaluate to true for the XML to be valid.
What is the difference between restriction and extension in complex types?
Restriction narrows the base type by disallowing certain elements or attributes, while extension expands the base type by allowing additional elements or attributes to be included.