XSD Example
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema" > <!-- The "Book" element --> < xs:element name = "Book" > < xs:complexType > < xs:sequence > <!-- Child elements with default values --> < xs:element name = "Title" type = "xs:string" default = "Unknown Title" /> < xs:element name = "Author" type = "xs:string" default = "Unknown Author" /> < xs:element name = "PublicationYear" type = "xs:gYear" default = "2022" /> < xs:element name = "Price" type = "xs:decimal" /> </ xs:sequence > </ xs:complexType > </ xs:element > </ xs:schema > |
Explanation of Example Code:
- The xs:schema element declares the XML Schema namespace.
- The xs:element element defines the “Book” element as the root element.
- Inside the “Book” element, we use xs:complexType to define the type of content it can contain.
- The xs:sequence element specifies that the child elements must appear in the specified order.
- We define child elements, such as “Title,” “Author,” “PublicationYear,” and “Price,” each with their data types using the xs:element element.
- defualt attribute is used to give defualt value.
- xs:string is used for text data, xs:gYear for a four-digit year, and xs:decimal for a decimal number.
XSD File Formats
XSD stands for XML Schema Definition, it is like a detailed plan for organizing and checking XML documents. It is a versatile language that provides a framework to structure and validate XML data effectively. it is a set of guidelines and limitations designed to ensure the reliability, consistency, and correctness of data stored in XML files. it specifies what elements, attributes, and data types are allowed in an XML document, serving as a rulebook that XML documents must adhere to. It also empowers developers to define constraints, like setting the acceptable value ranges for elements and enforcing specific formatting rules for data. XML Schema Definition (XSD) files are saved with the .xsd file extension.