A DTD defines the structure and syntax of an XML document and, to some extent, provides constraints on the types of data contained in XML elements.
XML schemas are designed to extend the functionality and usefulness of a DTD.
Consider the following element declaration from a DTD:
<!ELEMENT PHONE-NUMBER (#PCDATA) >
A validating XML parser would consider the following two entries in an XML document to be both well-formed and valid:
<PHONE-NUMBER> (512) 555-9981 </PHONE-NUMBER>
<PHONE-NUMBER> HXW2VQ </PHONE-NUMBER>
A phone number value of HXW2VQ is certainly not within the range of values that are considered valid phone numbers. In cases where XML element content must be constrained to certain values, XML schemas offer distinct advantages over DTDs. With schemas, you not only can define structure and syntax for XML documents (as in DTDs), but you can use many other useful features.
These features include setting data types for elements, the minimum number of times and element can occur, the maximum number of times an element can occur, and placing restrictions on the ranges of values for elements. While DTDs are written in EBNF, schemas are written in XML itself. This makes schemas easier to understand.
Elements declared in an XML schema are two types: 1) complex and 2) simple.
Complex types are elements that may contain sub-elements or attributes.
Simple types are elements that contain numbers, strings, dates, and so on, but no sub-elements.
Simple types are declared using the
simpleType
element. Many simple types are built into the XML schema recommendation.
The following diagram contains a list of these simple types.
Simple Type Names |
Meaning |
strings |
Represents a string of characters |
int |
Represents an integer value |
boolean |
Represents a true or false value |
decimal |
Represents a decimal number |
date |
Represents a date (YYYY-MM-DD format) |
time |
Represents a time (hh:mm:ss format) |
float |
Represents a floating-point number |
double |
Represents a double-precision number |
duration |
Represents a duration (e.g., P1Y2M3DT4H5M6S) |
anyURI |
Represents a Uniform Resource Identifier |
QName |
Represents a qualified name |
hexBinary |
Represents binary data in hexadecimal format |
base64Binary |
Represents binary data in Base64 format |
gYear |
Represents a specific year (e.g., 2024) |
gYearMonth |
Represents a year and month (e.g., 2024-11) |
gMonthDay |
Represents a month and day (e.g., --11-02) |
gDay |
Represents a specific day of the month (e.g., ---02) |
gMonth |
Represents a specific month (e.g., --11) |
unsignedInt |
Represents a non-negative integer |
unsignedShort |
Represents a non-negative short integer |
unsignedLong |
Represents a non-negative long integer |
unsignedByte |
Represents a non-negative byte value |
positiveInteger |
Represents a positive integer value |
nonPositiveInteger |
Represents a non-positive integer value |
nonNegativeInteger |
Represents a non-negative integer value |
negativeInteger |
Represents a negative integer value |
Complex types contain simple types declared using the
element
keyword and attributes declared using the
attribute
element.
Examine the following
series of imagesbelow to see how an XML schema is created.
Recall that XML schemas are created using XML itself.
As such we say that a complex type in an XML schema is created using the element element and the attribute element.
- XML Schemas and XML Structure:
- XML Schemas are used to define the structure, content, and data types of XML documents.
- An XML Schema is itself written in XML, meaning it uses XML syntax to describe the rules and constraints for other XML documents.
- By using XML to define the schema, we can establish a standardized way to validate and enforce the structure of data in XML documents.
- Complex Types in XML Schema:
- In XML Schemas, elements can be either simple or complex. A complex type is an element that can contain sub-elements (child elements), attributes, or a mixture of both.
- Complex types are used when an XML element needs to have a more elaborate structure. This structure can include:
- Nested elements (hierarchical relationships)
- Attributes (additional metadata or properties)
- Using the <element> Tag:
- Using the <complexType> Tag:
- Attributes in Complex Types:
- Attributes in XML provide additional information about elements. They are defined within complex types to specify properties that are associated with the element.
- In the above example,
id
is an attribute of the person
element and is used to uniquely identify each person
.
Summary:
- Complex types in XML Schema are defined using the
<element>
tag to create structured elements and the <complexType>
tag to outline the content model (sub-elements and attributes).
- This hierarchical and flexible structure enables you to define detailed and rich data models that reflect the intended structure of your XML documents.
The next lesson concludes this module.