XSD How To?

Welcome to The Coding College! In this tutorial, we’ll walk you through the basics of creating and using XML Schema Definition (XSD) to validate your XML documents. By the end, you’ll understand how to define an XSD, link it to an XML document, and use it to ensure your XML is well-formed and valid.

What Is XSD?

An XML Schema Definition (XSD) specifies the structure and rules of an XML document. It’s used to validate that the XML document conforms to specific data types, element order, and attribute requirements.

Key Features of XSD:

  • Written in XML syntax.
  • Supports data types like string, integer, and date.
  • Enables constraints like minimum/maximum values and string lengths.
  • Allows defining relationships between elements.

How to Create an XSD

Follow these steps to create and use an XSD:

1. Define the XSD Structure

An XSD file typically includes:

  • Elements: Defines the tags and their data types.
  • Attributes: Adds metadata to elements.
  • Data Types: Specifies the type of data (e.g., string, integer).
  • Constraints: Adds rules like minimum/maximum values or lengths.

Basic XSD Template

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!-- Define elements, attributes, and rules here -->
</xs:schema>

2. Reference the XSD in an XML Document

Link your XSD to your XML file using the xsi:noNamespaceSchemaLocation or xsi:schemaLocation attribute.

Example: XML Document

<?xml version="1.0" encoding="UTF-8"?>
<note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="note.xsd">
  <to>John</to>
  <from>Jane</from>
  <message>Hello, world!</message>
</note>

Explanation:

  • xmlns:xsi: Declares the XML Schema Instance namespace.
  • xsi:noNamespaceSchemaLocation: Links to the XSD file.

3. Create the XSD File

Define the structure of your XML document in the XSD file.

Example: XSD File (note.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="note">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="message" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

4. Validate the XML Document

Use tools like Oxygen XML Editor, Altova XMLSpy, or online XSD validators to check if the XML document is valid according to the XSD.

How to Define Common XSD Features

1. Define Simple Elements

A simple element contains text but no child elements or attributes.

Example

<xs:element name="age" type="xs:integer"/>
  • Defines an <age> element with an integer value.

2. Define Complex Elements

A complex element can contain other elements or attributes.

Example

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="age" type="xs:integer"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
  • Defines a <person> element with child elements <name> and <age>.

3. Define Attributes

Attributes add metadata to elements.

Example

<xs:attribute name="id" type="xs:string" use="required"/>
  • Defines an id attribute that is mandatory.

4. Use Restrictions (Facets)

Add rules to elements, such as value ranges or string lengths.

Example

<xs:element name="score">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="100"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
  • Ensures the score value is between 0 and 100.

5. Enumerations

Restrict an element’s value to a specific set of options.

Example

<xs:element name="color">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Red"/>
      <xs:enumeration value="Green"/>
      <xs:enumeration value="Blue"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
  • Restricts the color element to “Red,” “Green,” or “Blue.”

6. Default and Fixed Values

Assign default or fixed values to elements or attributes.

Example

<xs:element name="status" type="xs:string" default="Active"/>
<xs:attribute name="role" type="xs:string" fixed="Admin"/>

7. Multiple Occurrences

Define how many times an element can appear using minOccurs and maxOccurs.

Example

<xs:element name="item" type="xs:string" maxOccurs="unbounded"/>
  • The <item> element can appear any number of times.

Complete XSD Example

Here’s a complete example tying everything together:

XML Document

<?xml version="1.0" encoding="UTF-8"?>
<store xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="store.xsd">
  <product id="001">
    <name>Smartphone</name>
    <price>699.99</price>
    <inStock>true</inStock>
  </product>
  <product id="002">
    <name>Laptop</name>
    <price>1299.99</price>
    <inStock>false</inStock>
  </product>
</store>

XSD File (store.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="store">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="product" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string"/>
              <xs:element name="price" type="xs:decimal"/>
              <xs:element name="inStock" type="xs:boolean"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

Advantages of Using XSD

  1. Precise Validation: Ensures XML documents are accurate and consistent.
  2. Data Types: Supports various types, such as numbers, dates, and booleans.
  3. Extensibility: Can handle complex structures with ease.

Conclusion

Learning how to create and use XSD is essential for working with XML in real-world applications. It helps ensure your data is well-structured and adheres to specific rules.

For more tutorials and resources, visit The Coding College. Keep coding and building better XML applications!

Leave a Comment