Sobes.tech
Junior

What is an XSD schema?

sobes.tech AI

Answer from AI

XSD (XML Schema Definition) schema is a language for describing the structure and permissible content of XML documents. It defines rules that an XML document must adhere to in order to be considered valid.

Main elements of an XSD schema:

  • Elements (<element>): Define element names, their data types, and allowable quantities.
  • Attributes (<attribute>): Define attribute names, their data types, and whether they are required.
  • Simple types (<simpleType>): Define basic data types (e.g., string, integer, boolean) and restrictions for them (e.g., minInclusive, maxLength).
  • Complex types (<complexType>): Define the structure of elements containing other elements or attributes.
  • Groups (<group>, <attributeGroup>): Allow reuse of element or attribute definitions.
  • Sequences (<sequence>): Specify the order of elements.
  • Choice (<choice>): Indicates that one of several elements can appear.
  • All (<all>): Indicates that all listed elements can appear in any order (rarely used in practice due to limitations).

XSD schemas offer more powerful validation capabilities compared to DTD (Document Type Definition), including:

  • Strict data typing.
  • Namespaces.
  • Mechanisms for extension and restriction of types.

Example of a simple XSD schema:

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

  <xs:complexType name="BookType">
    <xs:sequence>
      <xs:element name="Title" type="xs:string"/>
      <xs:element name="Author" type="xs:string"/>
      <xs:element name="YearOfPublication" type="xs:integer"/>
    </xs:sequence>
    <xs:attribute name="isbn" type="xs:string" use="required"/>
  </xs:complexType>

  <xs:element name="Book" type="BookType"/>

</xs:schema>