Xsd Example in XML
? XSD Example in XML
Here’s a complete working example of an XML document and its corresponding XSD schema. This demonstrates how an XML document is structured and how the XSD defines rules for validation.
? 1. XML Example
<?xml version="1.0" encoding="UTF-8"?><bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bookstore.xsd"> <book> <title>XML Fundamentals</title> <author>Jane Doe</author> <year>2025</year> <price>29.99</price> </book></bookstore>? 2. XSD File (bookstore.xsd)
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="bookstore"> <xs:complexType> <xs:sequence> <xs:element name="book" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> <xs:element name="year" type="xs:gYear"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>? Explanation
| Element | Description |
|---|---|
<bookstore> | Root element containing one or more <book>s |
<book> | Has child elements like title, author, year, price |
type | Uses built-in types: xs:string, xs:gYear, etc. |
maxOccurs | Allows multiple <book> entries |
? How to Use It
Save the XML file as
bookstore.xmlSave the XSD file as
bookstore.xsdin the same folderUse an XML validator (like in Visual Studio Code, Notepad++, or an online tool) to validate
Would you like a downloadable .zip with these two files, or an example with attributes or restrictions next?