Xsd Misc in XML
? XSD Miscellaneous Features in XML Schema (XSD)
Beyond the basic definitions of elements, attributes, and types, XSD supports a variety of additional features to handle complex validation and structure needs.
1. Annotations & Documentation
Add comments and documentation inside XSD for clarity:
<xs:annotation> <xs:documentation> This element defines a customer record. </xs:documentation></xs:annotation>2. Enumerations (Restricted Values)
Restrict an element or attribute to a fixed set of values:
<xs:simpleType name="ColorType"> <xs:restriction base="xs:string"> <xs:enumeration value="Red"/> <xs:enumeration value="Green"/> <xs:enumeration value="Blue"/> </xs:restriction></xs:simpleType><xs:element name="color" type="ColorType"/>3. Default and Fixed Values
default: Provides a default if none is specified in XMLfixed: Value must always be this, cannot be changed
<xs:element name="currency" type="xs:string" default="USD"/><xs:element name="country" type="xs:string" fixed="USA"/>4. Mixed Content
Allows elements to contain both text and child elements:
<xs:complexType mixed="true"> <xs:sequence> <xs:element name="bold" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence></xs:complexType>5. Substitution Groups
Allow one element to be substituted by others:
<xs:element name="animal" abstract="true"/><xs:element name="dog" substitutionGroup="animal"/><xs:element name="cat" substitutionGroup="animal"/>In XML, <dog> or <cat> can replace <animal>.
6. Key, KeyRef, and Unique Constraints
Define unique keys and references (like primary keys and foreign keys):
<xs:key name="personKey"> <xs:selector xpath="person"/> <xs:field xpath="id"/></xs:key><xs:keyref name="managerRef" refer="personKey"> <xs:selector xpath="person"/> <xs:field xpath="managerId"/></xs:keyref>7. Import and Include
Reuse or modularize schemas:
<xs:include schemaLocation="common.xsd"/><xs:import namespace="http://example.com/ns" schemaLocation="other.xsd"/>8. Pattern Restrictions
Validate values against regex patterns:
<xs:simpleType name="ZipCodeType"> <xs:restriction base="xs:string"> <xs:pattern value="\d{5}(-\d{4})?"/> </xs:restriction></xs:simpleType>