<?xml version="1.0"?> <book> <title>XML</title> <isbn>123</isbn> </book>Can be validated against the following XML Schema:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string" /> <xs:element name="isbn" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>Note: The XML Schema namespace "
http://www.w3.org/2001/XMLSchema
" is the standard namespace for XML Schema definitions.
The prefix "xs
" is conventional but not required - you can use any prefix.
To perform actual validation, you need an XML parser or validator that supports XML Schema validation (such as those found in programming languages like Java or Python).targetNamespace
" attribute
to specify a namespace ("http://www.mtitek.com/ns/books
") that can be used by XML documents.<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.mtitek.com/ns/books" targetNamespace="http://www.mtitek.com/ns/books" elementFormDefault="qualified"> ... </xs:schema>The XML Schema can also define a namespace (which may be the same as the one specified for the "
targetNamespace
" attribute).
But this one is used only within the schema to identify schema elements.xmlns
" attribute without a prefix declares the default namespace for the schema document itself, making it easier to reference elements defined in the target namespace without prefixes.targetNamespace
" attribute in the XML Schema.
The XML document must also define the namespace "http://www.w3.org/2001/XMLSchema-instance
",
which is used to specify the location of the XML Schema (via the "schemaLocation
" attribute).
The prefix "xsi
" is standard for referring to the schema, but any prefix name will work correctly.schemaLocation
" attribute consists of two parts (separated by a space):
The first part is the namespace (must match the value of "targetNamespace
" in the schema),
and the second part is the location of the XML Schema file.booksSchemaFile.xsd
"),
or absolute, by specifying an explicit path to a local resource (hard drive: "file:///C:/xs/booksSchemaFile.xsd
")
or a remote resource (intranet or internet: "http://www.mtitek.com/xs/booksSchemaFile.xsd
").<?xml version="1.0"?> <book xmlns="http://www.mtitek.com/ns/books" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mtitek.com/ns/books booksSchemaFile.xsd"> ... </book>Note: For XML documents that don't use namespaces, you can use "
xsi:noNamespaceSchemaLocation
" instead of "xsi:schemaLocation
".
This attribute takes only the schema location (no namespace part).elementFormDefault
" attribute in the schema:qualified
",
then all elements in the XML document must be namespace-qualified (either with a prefix or through a default namespace declaration).
This means every element, including child elements, must belong to the target namespace.unqualified
",
then only the root element needs to be qualified, and child elements should not have namespace qualification.attributeFormDefault
" attribute in the schema:qualified
", then attributes must be qualified.unqualified
", then attributes must be unqualified.<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.mtitek.com/ns/books" targetNamespace="http://www.mtitek.com/ns/books" elementFormDefault="qualified" attributeFormDefault="qualified"> <xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string" /> <xs:element name="isbn" type="xs:string" /> </xs:sequence> <xs:attribute name="edition" type="xs:positiveInteger" /> </xs:complexType> </xs:element> </xs:schema>
<?xml version="1.0"?> <b:book xmlns:b="http://www.mtitek.com/ns/books" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mtitek.com/ns/books booksSchemaFile.xsd" b:edition="1"> <b:title>XML</b:title> <b:isbn>123</b:isbn> </b:book>
anyType
", which is the base for both simple and complex types.anySimpleType
", which is the root of all simple types.anySimpleType
boolean
Fixed-size integer types: unsignedByte (0 to 255) byte (-128 to 127) unsignedShort (0 to 65,535) short (-32,768 to 32,767) unsignedInt (0 to 4,294,967,295) int (-2,147,483,648 to 2,147,483,647) unsignedLong (0 to 18,446,744,073,709,551,615) long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) Primitive types: decimal (decimal numbers with arbitrary precision) float (32-bit floating point) double (64-bit floating point) Derived integer types: integer (arbitrary precision integers, derived from decimal) positiveInteger (1, 2, 3, ... - derived from nonNegativeInteger) nonNegativeInteger (0, 1, 2, 3, ... - derived from integer) negativeInteger (..., -3, -2, -1 - derived from nonPositiveInteger) nonPositiveInteger (..., -3, -2, -1, 0 - derived from integer)
Primitive types: date (YYYY-MM-DD, e.g., 2025-06-25) dateTime (YYYY-MM-DDTHH:MM:SS, e.g., 2025-06-25T14:30:00) time (HH:MM:SS, e.g., 14:30:00) duration (PnYnMnDTnHnMnS, e.g., P1Y2M3DT4H5M6S = 1 year, 2 months, 3 days, 4 hours, 5 minutes, 6 seconds) Derived date/time types: gDay (---DD, e.g., ---15) gMonth (--MM, e.g., --06) gMonthDay (--MM-DD, e.g., --06-25) gYear (YYYY, e.g., 2025) gYearMonth (YYYY-MM, e.g., 2025-06)
Basic string types: string (any string - primitive type) normalizedString (string with tabs/newlines converted to spaces) token (normalized string with no leading/trailing whitespace and collapsed internal spaces) Name types (derived from token): Name (valid XML name) NCName (non-colonized name - XML name without namespace prefix) QName (qualified name with optional namespace prefix) Token types: NMTOKEN (name token - valid XML name token) NMTOKENS (space-separated list of NMTOKENs) language (language code like "en-US", derived from token) XML-specific types: ID (unique identifier within document) IDREF (reference to an ID) IDREFS (space-separated list of IDREFs) ENTITY (reference to an unparsed entity) ENTITIES (space-separated list of entities) NOTATION (reference to a notation) Other types: anyURI (URI reference - primitive type) base64Binary (base64-encoded binary data - primitive type) hexBinary (hexadecimal-encoded binary data - primitive type)
<!-- Simple pattern restriction --> <xs:simpleType name="restrictedString"> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z]{3}" /> </xs:restriction> </xs:simpleType> <!-- Simple pattern restriction combined with other restrictions --> <xs:simpleType name="restrictedStringWithLength"> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z]*" /> <xs:length value="3" /> </xs:restriction> </xs:simpleType> <!-- Numeric restriction with range --> <xs:simpleType name="restrictedNumeric"> <xs:restriction base="xs:decimal"> <xs:minInclusive value="0" /> <xs:maxInclusive value="6" /> <xs:fractionDigits value="2" /> </xs:restriction> </xs:simpleType> <!-- Enumeration restriction --> <xs:simpleType name="restrictedEnumeration"> <xs:restriction base="xs:string"> <xs:enumeration value="true" /> <xs:enumeration value="false" /> </xs:restriction> </xs:simpleType>
<!-- Simple list --> <xs:simpleType name="listOfStrings"> <xs:list itemType="xs:string" /> </xs:simpleType>
<!-- Simple union --> <xs:simpleType name="stringOrInt"> <xs:union memberTypes="xs:string xs:int" /> </xs:simpleType>Note: Validation uses the first matching type. For example the example above "xs:string xs:int", the string "123" will be treated as a string, not an integer.
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.mtitek.com/ns/customDataTypes" targetNamespace="http://www.mtitek.com/ns/customDataTypes" elementFormDefault="qualified"> <xs:element name="customData"> <xs:complexType> <xs:sequence> <xs:element name="info" type="xs:string" /> <xs:element name="restriction" type="restrictedString" /> <xs:element name="list" type="listOfStrings" /> <xs:element name="union" type="stringOrInt" /> </xs:sequence> </xs:complexType> </xs:element> <!-- Restriction example --> <xs:simpleType name="restrictedString"> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z]{3}" /> </xs:restriction> </xs:simpleType> <!-- List example --> <xs:simpleType name="listOfStrings"> <xs:list itemType="xs:string" /> </xs:simpleType> <!-- Union example --> <xs:simpleType name="stringOrInt"> <xs:union memberTypes="xs:string xs:int" /> </xs:simpleType> </xs:schema>
<?xml version="1.0"?> <c:customData xmlns:c="http://www.mtitek.com/ns/customDataTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mtitek.com/ns/customDataTypes customDataTypesFile.xsd"> <c:info>Custom Data Types Example</c:info> <c:restriction>ABC</c:restriction> <c:list>X Y Z</c:list> <c:union>W</c:union> </c:customData>
<xs:element id="" Unique identifier within the schema name="" The name of the element (required for element declarations) type="" The data type of the element (can be built-in or user-defined) minOccurs="" Minimum number of times the element can occur (default: 1) maxOccurs="" Maximum number of times the element can occur (default: 1, use "unbounded" for unlimited) default="" Default value if element is not present fixed="" Fixed value that the element must have nillable="" Whether the element can have a nil value (xsi:nil="true") abstract="" Whether the element is abstract (cannot appear in instance documents, only substituted) substitutionGroup="" Head element of the substitution group this element belongs to form="" Whether the element is qualified or unqualified (overrides schema default) block="" Prevents certain types of derivation or substitution (values: "extension", "restriction", "substitution", "#all") final="" Prevents derivation of new elements from this element (values: "extension", "restriction", "#all") > ... </xs:element>
<xs:complexType name="BookType"> <xs:sequence> <xs:element name="title" type="xs:string" /> <xs:element name="isbn" type="xs:string" /> </xs:sequence> </xs:complexType>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="book" type="BookType" /> <xs:element name="library"> <xs:complexType> <xs:sequence> <xs:element ref="book" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>Note: When using "ref", you reference an existing global element. You cannot specify "name", "type", or most other attributes on the referencing element; only occurrence constraints ("minOccurs", "maxOccurs"), "id", and annotation elements can be used.
<xs:element name="title" type="xs:string" />
<xs:element name="isbn"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="format" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>Example:
<isbn format="ISBN-13">123-0123456789</isbn>
<xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string" /> <xs:element name="isbn" type="xs:string" /> </xs:sequence> <xs:attribute name="edition" type="xs:positiveInteger" /> </xs:complexType> </xs:element>
<xs:element name="bookInfo"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="note" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> <xs:element name="comment" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element>Examples:
<bookInfo>Just text with no child elements.</bookInfo>
<bookInfo> <note>One note</note> </bookInfo>
<bookInfo> This is a mixed content <note>first note</note> more text <comment>first comment</comment> and more text <note>second note</note> another text <comment>second comment</comment> end of content. </bookInfo>
<xs:element name="bookEdition"> <xs:complexType> <xs:attribute name="edition" type="xs:string" /> </xs:complexType> </xs:element>Examples:
<bookEdition />
<bookEdition edition="4th Edition" />
<xs:element name="isbn" type="xs:string" nillable="true" />XML documents must declare the XML Schema Instance namespace to use "xsi:nil":
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <isbn xsi:nil="true" /> </book>
<xs:element name="title" type="xs:string" default="MTI" /> <xs:element name="isbn" type="xs:string" default="123" />
<xs:element name="title" type="xs:string" fixed="MTI" /> <xs:element name="isbn" type="xs:string" fixed="123" />Note: Default and fixed attributes are mutually exclusive and only apply to elements with simple content (including elements with simple content and attributes).
<xs:element name="title" type="xs:string" /> <!-- Required, exactly once (default) --> <xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1" /> <!-- Required, exactly once (explicit) --> <xs:element name="title" type="xs:string" minOccurs="0" /> <!-- Optional, max one --> <xs:element name="title" type="xs:string" maxOccurs="unbounded" /> <!-- Required, one or more --> <xs:element name="title" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> <!-- Optional, zero or more --> <xs:element name="title" type="xs:string" minOccurs="3" maxOccurs="6" /> <!-- Required, between 3 and 6 occurrences -->
<xs:element id="" name="" type="" minOccurs="1" <!-- Control element cardinality --> maxOccurs="1" <!-- Control element cardinality (use "unbounded" for unlimited) --> nillable="false" <!-- Allows xsi:nil="true" in instance documents --> form="unqualified" <!-- "qualified" or "unqualified" (overrides elementFormDefault) --> ref="" default="" block="" <!-- Prevents substitution: "substitution", "extension", "restriction", or "#all" --> final="" <!-- Prevents derivation: "extension", "restriction", or "#all" --> fixed="" substitutionGroup="" <!-- Allows element substitution in valid instances --> abstract="false"> <!-- If true, element cannot appear in instance documents (used as base for substitution) --> (annotation?, (simpleType | complexType)?, ((unique | key | keyref))*) </xs:element>
<xs:any id="" namespace="##any" <!-- Controls which namespaces are allowed: ##any, ##other, ##targetNamespace, ##local, or URI list --> processContents="strict" <!-- Controls validation level: "strict", "lax", "skip" --> minOccurs="1" maxOccurs="1"> (annotation?) </xs:any>
<xs:attribute id="" type="" name="" default="" fixed="" use="optional" <!-- "required", "optional", or "prohibited" --> form="unqualified" <!-- "qualified" or "unqualified" (overrides attributeFormDefault) --> ref=""> <!-- References a global attribute declaration --> (annotation?, simpleType?) </xs:attribute>
<xs:anyAttribute id="" namespace="##any" <!-- Controls which namespaces are allowed: ##any, ##other, ##targetNamespace, ##local, or URI list --> processContents="strict"> <!-- Controls validation level: "strict", "lax", "skip" --> (annotation?) </xs:anyAttribute>
<xs:attributeGroup id="" name="" ref=""> (annotation?, ((attribute | attributeGroup)*, anyAttribute?)) </xs:attributeGroup>
<xs:simpleType id="" name="" final=""> <!-- Prevents derivation: "restriction", "list", "union", or "#all" --> (annotation?, (restriction | list | union)) </xs:simpleType>
<xs:complexType id="" name="" abstract="false" <!-- If true, type cannot be used directly in instance documents --> mixed="false" <!-- If true, allows mixed content (text and elements) --> block="" <!-- Prevents certain types of derivation: "extension", "restriction", or "#all" --> final=""> <!-- Prevents derivation by extension or restriction: "extension", "restriction", or "#all" --> (annotation?, ((simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))) </xs:complexType>
<xs:simpleContent id=""> (annotation?, (restriction | extension)) </xs:simpleContent>
<xs:complexContent id="" mixed="false"> <!-- If true, allows mixed content (text and elements) --> (annotation?, (restriction | extension)) </xs:complexContent>
<xs:sequence id="" minOccurs="1" maxOccurs="1"> (annotation?, (element | group | choice | sequence | any)*) </xs:sequence>
<xs:choice id="" minOccurs="1" maxOccurs="1"> (annotation?, (element | group | choice | sequence | any)*) </xs:choice>
<xs:all id="" minOccurs="1" <!-- Can only be 0 or 1 (not higher values) --> maxOccurs="1"> <!-- Must always be 1 for xs:all --> (annotation?, element*) </xs:all>
<xs:group id="" name="" ref="" minOccurs="1" maxOccurs="1"> (annotation?, (all | choice | sequence)?) </xs:group>
<xs:restriction id="" base=""> <!-- The type being restricted (required) (example: xs:string) --> (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)) </xs:restriction>
<xs:extension id="" base=""> <!-- The type being extended (required) (example: xs:int) --> (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))) </xs:extension>
<xs:list id="" itemType=""> <!-- The type of each item in the list (example: xs:string) --> (annotation?, simpleType?) </xs:list>
<xs:union id="" memberTypes=""> <!-- Space-separated list of type names --> (annotation?, simpleType*) </xs:union>
<xs:minExclusive id="" value="" fixed="false"> (annotation?) </xs:minExclusive>
<xs:maxExclusive id="" value="" fixed="false"> (annotation?) </xs:maxExclusive>
<xs:minInclusive id="" value="" fixed="false"> (annotation?) </xs:minInclusive>
<xs:maxInclusive id="" value="" fixed="false"> (annotation?) </xs:maxInclusive>
<xs:totalDigits id="" value="" fixed="false"> (annotation?) </xs:totalDigits>
<xs:fractionDigits id="" value="" fixed="false"> (annotation?) </xs:fractionDigits>
<xs:length id="" value="" fixed="false"> (annotation?) </xs:length>
<xs:minLength id="" value="" fixed="false"> (annotation?) </xs:minLength>
<xs:maxLength id="" value="" fixed="false"> (annotation?) </xs:maxLength>
<xs:pattern id="" value=""> <!-- Regular expression pattern (e.g., "[A-Za-z0-9]+", "\d{3}-\d{2}-\d{4}") --> (annotation?) </xs:pattern>
<xs:whiteSpace id="" value="preserve" <!-- Whitespace handling strategy: "preserve", "replace", "collapse" --> fixed="false"> <!-- If true, derived types cannot change this facet --> (annotation?) </xs:whiteSpace>
<xs:enumeration id="" value=""> (annotation?) </xs:enumeration>
<xs:key id="" name=""> (annotation?, (selector, field+)) </xs:key>
<xs:keyref id="" name="" refer=""> <!-- References the name of a key or unique constraint --> (annotation?, (selector, field+)) </xs:keyref>
<xs:unique id="" name=""> (annotation?, (selector, field+)) </xs:unique>
<xs:selector id="" xpath=""> <!-- XPath expression to select context nodes (e.g., ".//book") --> (annotation?) </xs:selector>
<xs:field id="" xpath=""> <!-- XPath expression to select field value (e.g., "@id" or "title") --> (annotation?) </xs:field>
<xs:annotation id=""> (appinfo | documentation)* </xs:annotation>
<xs:documentation source="" <!-- URI reference to external documentation --> xml:lang=""> <!-- Language code (e.g., "en", "fr", "de") --> (#PCDATA | any)* </xs:documentation>
<xs:appinfo source=""> <!-- URI reference to external application information --> (#PCDATA | any)* </xs:appinfo>
<xs:import id="" namespace="" <!-- Target namespace of the imported schema; use ##targetNamespace or ##local --> schemaLocation=""> <!-- Location of the schema file (optional) --> (annotation?) </xs:import>
<xs:include id="" schemaLocation=""> <!-- Location of the schema file (required) --> (annotation?) </xs:include>
<xs:redefine id="" schemaLocation=""> (annotation?, (simpleType | complexType | group | attributeGroup)*) </xs:redefine>
<xs:notation id="" name="" public="" <!-- Public identifier for the notation --> system=""> <!-- System identifier (URI) for the notation --> (annotation?) </xs:notation>
use="optional" <!-- Default: Attribute is optional --> use="required" <!-- Attribute must be present --> use="prohibited" <!-- Attribute must not be present (used in restrictions) -->
namespace="##any" <!-- Default: Any namespace including no namespace --> namespace="##other" <!-- Any namespace other than the target namespace --> namespace="##targetNamespace" <!-- Only the target namespace --> namespace="##local" <!-- Only elements/attributes with no namespace --> namespace="http://example.com" <!-- Specific namespace URI --> namespace="##any ##local" <!-- Multiple values separated by spaces -->
<!-- Strict validation --> <xs:any namespace="##other" processContents="strict" /> <!-- Default: Must obtain schemas to validate --> <!-- Lax validation --> <xs:any namespace="##any" processContents="lax" /> <!-- Validate only if schema is available --> <!-- Skip validation --> <xs:any namespace="##local" processContents="skip" /> <!-- No validation - any well-formed content allowed -->
block="#all" <!-- Blocks all derivations --> block="extension" <!-- Blocks derivation by extension --> block="restriction" <!-- Blocks derivation by restriction --> block="substitution" <!-- Blocks substitution -->
final="#all" <!-- Prevents all derivations --> final="extension" <!-- Prevents derivation by extension --> final="restriction" <!-- Prevents derivation by restriction --> final="list" <!-- Prevents derivation by list (simple types only) --> final="union" <!-- Prevents derivation by union (simple types only) -->