Attributes Entitites   «Prev  Next»

Lesson 7XML schemas
ObjectiveExplain the purpose of XML schemas.

XML Schemas

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.

Creating XML schemas

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.

Creating XML Schemas
1) An XML schema declaration begins with the schema element that specifies a URI for a namespace.
1) An XML schema declaration begins with the schema element that specifies a URI for a namespace. The de facto prefix for XML schemas is xsd.

2) The first statement declares an element named Order.
2) The first statement declares an element named Order. Its element type is OrderType. The second statement declares an element named comment. The comment element type is string. Note that string is prefixed with xsd, the de facto namespace prefix for XML schemas.


3) The next group of statements defines the OrderType declared previously.
3) The next group of statements defines the OrderType declared previously. Order type consists of several elements, which must be present in the sequence specified by the <xsd:sequence> element. The <xsd:element ref="comment" minOccurs="0" /> indicates that this element is a reference to the comment element.

4) This group of statements defines a simple type named SKU
4) This group of statements defines a simple type named SKU. Using the <xsd:restriction base="string"> element this simple type is restricted to type string. In addition, this string simple type must follow a pattern as indicated by the
<xsd:pattern value="\d[3]-[A-Z][2] "/> .



XML Schemas consist of XML

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.
  1. 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.
  2. 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)

  3. Using the <element> Tag:
    • The <element> tag is fundamental in XML Schema and is used to define an element within an XML document. When creating a complex type, you use the <element> tag to specify the name, type, and structure of that element.
    • For example:
      <xs:element name="person" type="personType"/>
              
    • In this case, an element named person is defined with a complex type personType that will specify the structure of person.
  4. Using the <complexType> Tag:
    • The <complexType> tag is used within the XML Schema to define the structure of a complex type. It allows you to specify which elements and attributes can exist within that type.
    • Here's an example:
      <xs:element name="person">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="firstName" type="xs:string"/>
            <xs:element name="lastName" type="xs:string"/>
            <xs:element name="age" type="xs:int"/>
          </xs:sequence>
          <xs:attribute name="id" type="xs:string" use="required"/>
        </xs:complexType>
      </xs:element>
              
    • In this example:
      • The person element is defined as a complex type.
      • It has a sequence of sub-elements: firstName, lastName, and age.
      • It also has an attribute id that is required.
  5. 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.

SEMrush Software