XML SchemaUsage
1. Define the elements in an XML document
2. Define the attributes in an XML document.
3. Define what kind of sub-nodes are available for a node, how many sub-nodes are available, and the sequence in which the sub-nodes appear
4. Define the Data Type of the element or attribute
5. Define the default value or fixed value of an element or attribute
XML SchemaRoot element:
<? XML version = "1.0"?>
<Xs: schema xmlns: xs = "http://www.w3.org/2001/XMLSchema" indicates that definitions such as data types come from W3
Targetnamespace = "http://www.w3schools.com" indicates the namespace from which the elements to be defined in the document come from
Xmlns = "http://www.w3schools.com" indicates what is the default namespace for this document
Elementformdefault = "qualified"> indicates that each element of the XML document must have a namespace specified
...... Define the subject ......
</Xs: schema>
How to define a Simple Element
<Xs: element indicates that an element is to be defined.
Name = "color" indicates the name of the element to be defined
Type = "Xs: string" indicates the Data Type of the element to be defined.
Default = "red" indicates the default value of the defined element.
Fixed = "red"/> indicates the fixed value of the element to be defined. This element can only be set to "red ".
The preceding defines a simple element. For example, <color> Red </color>
How to define an attribute
<Xs: attribute
Name = "Birthday" indicates the name of the property to be defined
Type = "Xs: Date" indicates the Data Type of the attribute to be defined.
Default = "indicates the default value of the property to be defined
Fixed = "indicates the fixed value of the property to be defined
Use = "required"/> indicates whether this attribute must be specified. If this attribute is not specified, it does not conform to the schema. By default, the use = "required" attribute does not exist, indicating that the attribute is optional.
How to define restrictions on elements or attribute values
1. maximum and minimum limits
<Xs: element name = "Age">
<Xs: simpletype>
<Xs: Restriction base = "XS: integer">
<Xs: min1_sive value = "0"/> greater than or equal to 0. <Xs: minexclusive> indicates the minimum value but does not include the specified value.
<Xs: maxcompute sive value = "120"/> less than or equal to 120, <Xs: maxexclusive>
</Xs: Restriction>
</Xs: simpletype>
</Xs: Element>
2. Enumeration restriction, which indicates that values can only be set among the specified values.
<Xs: element name = "car" type = "cartype"/>
<Xs: simpletype name = "cartype">
<Xs: Restriction base = "XS: string">
<Xs: enumeration value = "Audi"/>
<Xs: enumeration value = "golf"/>
<Xs: enumeration value = "BMW"/>
</Xs: Restriction>
</Xs: simpletype>
3. Pattern restrictions: the string format must meet the specified matching pattern.
Example |
Description |
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpleType> </Xs: Element> |
Indicates that only one value can be obtained from lowercase letters. |
<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z][A-Z]"/> </xs:restriction> </xs:simpleType> </xs:element> |
Three uppercase letters are required. |
<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> </xs:restriction> </xs:simpleType> </xs:element> |
It must contain three letters, either uppercase or lowercase. |
<xs:element name="choice"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[xyz]"/> </xs:restriction> </xs:simpleType> </Xs: Element> |
It must be one of XYZ. |
<xs:element name="prodid"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> </xs:restriction> </xs:simpleType> </xs:element> |
The value range of a number is 0-99999. |
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction> </xs:simpleType> </xs:element> |
It indicates a sequence consisting of 0 or multiple lower-case characters. |
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z][A-Z])+"/> </xs:restriction> </xs:simpleType> </xs:element> |
It must be multiple letters. |
<xs:element name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> </xs:element> |
Indicates one of male or female |
<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9]{8}"/> </xs:restriction> </xs:simpleType> </xs:element> |
It must be 8 letters/numbers |
4. String Length restrictions
<Xs: element name = "password">
<Xs: simpletype>
<Xs: Restriction base = "XS: string">
<Xs: length value = "8"/>
</Xs: Restriction>
</Xs: simpletype>
</Xs: Element>
The length must be 8.
<Xs: element name = "password">
<Xs: simpletype>
<Xs: Restriction base = "XS: string">
<Xs: minlength value = "5"/>
<Xs: maxlength value = "8"/>
</Xs: Restriction>
</Xs: simpletype>
</Xs: Element>
Indicates that the length is between 5 and 8.
6. Restrictions on white space characters
Example |
Description |
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType> </Xs: Element> |
Unchanged, indicating that the XML processor will not remove or replace any blank characters |
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="replace"/> </xs:restriction> </xs:simpleType> </xs:element> |
Press enter, line feed, and tab will be replaced with spaces |
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="collapse"/> </xs:restriction> </xs:simpleType> </xs:element> |
Remove more than one space. The processing method is the same as that in HTML. |
How to define complex types
A complex type is a type that specifies that the element contains attributes or child elements.
1. Define complex types that only contain child elements
<Xs: element name = "person">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "firstname" type = "XS: string"/>
<Xs: element name = "lastname" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
2. Define complex types that only contain attributes
<Xs: element name = "product" type = "prodtype"/>
<Xs: complextype name = "prodtype">
<Xs: attribute name = "prodid" type = "XS: positiveinteger"/>
</Xs: complextype>
3. Define complex types that only contain content
<Xs: element name = "shoesize" type = "shoetype"/>
<Xs: complextype name = "shoetype">
<Xs: simplecontent>
<Xs: Extension base = "XS: integer">
<Xs: attribute name = "country" type = "XS: string"/>
</Xs: Extension>
</Xs: simplecontent>
</Xs: complextype>
4. Define complex types that contain content and child elements
<Xs: element name = "letter">
<Xs: complextypeMixed = "true">
<Xs: sequence>
<Xs: element name = "name" type = "XS: string"/>
<Xs: element name = "orderid" type = "XS: positiveinteger"/>
<Xs: element name = "shipdate" type = "XS: Date"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
The XML corresponding to the above definition
<Letter>
Dear mr. <Name> john smith </Name>.
Your order <orderid> 1032 </orderid>
Will be shipped on <shipdate> 2001-07-13 </shipdate>.
</Letter>
5. Define complex types that contain attributes and child elements
Use indicator
The indicators in XSD include
1. sequence indicator
1) All
Indicates that child elements can appear in any order, and each element must appear once.
<Xs: element name = "person">
<Xs: complextype>
<Xs: All>
<Xs: element name = "firstname" type = "XS: string"/>
<Xs: element name = "lastname" type = "XS: string"/>
</Xs: All>
</Xs: complextype>
</Xs: Element>
2) Choice
Indicates that one or more sub-elements can appear.
<Xs: element name = "person">
<Xs: complextype>
<Xs: Choice>
<Xs: element name = "employee" type = "employee"/>
<Xs: element name = "member" type = "member"/>
</Xs: Choice>
</Xs: complextype>
</Xs: Element>
3) sequence
Indicates that child elements must appear in sequence
<Xs: element name = "person">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "firstname" type = "XS: string"/>
<Xs: element name = "lastname" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
2. Number of occurrences indicator minoccurs, maxoccurs
<Xs: element name = "person">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "full_name" type = "XS: string"/>
<Xs: element name = "child_name" type = "XS: string"
Maxoccurs = "10" minoccurs = "0"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
3. Group indicators)
Used to define a group of related elements
<Xs: group name = "persongroup">
<Xs: sequence>
<Xs: element name = "firstname" type = "XS: string"/>
<Xs: element name = "lastname" type = "XS: string"/>
<Xs: element name = "Birthday" type = "XS: Date"/>
</Xs: sequence>
</Xs: group>
<Xs: element name = "person" type = "personinfo"/>
<Xs: complextype name = "personinfo">
<Xs: sequence>
<Xs: group ref = "persongroup"/>
<Xs: element name = "country" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>
Defines a set of related attributes.
<Xs: attributegroup name = "personattrgroup">
<Xs: attribute name = "firstname" type = "XS: string"/>
<Xs: attribute name = "lastname" type = "XS: string"/>
<Xs: attribute name = "Birthday" type = "XS: Date"/>
</Xs: attributegroup>
<Xs: element name = "person">
<Xs: complextype>
<Xs: attributegroup ref = "personattrgroup"/>
</Xs: complextype>
</Xs: Element>
AnyKeywords
Indicates that any element can exist.
<Xs: element name = "person">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "firstname" type = "XS: string"/>
<Xs: element name = "lastname" type = "XS: string"/>
<Xs: Any minoccurs = "0"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
AnyattributeKeywords
<Xs: element name = "person">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "firstname" type = "XS: string"/>
<Xs: element name = "lastname" type = "XS: string"/>
</Xs: sequence>
<Xs: anyattribute/>
</Xs: complextype>
</Xs: Element>
SubstitutiongroupKeywords
Indicates that one element has the same definition as another alternative element.
<Xs: element name = "name" type = "XS: string"/>
<Xs: element name = "navn" substitutiongroup = "name"/>
<Xs: complextype name = "custinfo">
<Xs: sequence>
<Xs: Element ref = "name"/>
</Xs: sequence>
</Xs: complextype> <Xs: element name = "customer" type = "custinfo"/>
<Xs: element name = "Kunde" substitutiongroup = "customer"/>
The examples in this article are from w3school.
How to define an XSD File