Sobes.tech
Junior — Middle

Can you explain what an XSD schema is and how it is used to validate XML documents?

sobes.tech AI

Answer from AI

XSD (XML Schema Definition) is a language for describing the structure and constraints of XML documents. It defines which elements and attributes can appear in XML, their data types, order, quantity, and other rules.

Using XSD to validate XML:

  • The XML document is linked to the XSD schema via a reference (for example, in the xsi:schemaLocation attribute).
  • An XML parser reads the document and checks its compliance with the rules specified in the XSD.
  • If the structure, data types, or required elements do not match the schema, the parser issues a validation error.

This ensures that the XML document is correct and conforms to the expected format before further processing.

Example of a simple XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="person">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="age" type="xs:int"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This XSD describes an XML with an element person, which contains name and age.

In Java, validation can be performed using javax.xml.validation.SchemaFactory and Validator.

Can you explain what an XSD schema is and how it is… - sobes.tech