SimpleType detailed
The simpletype element function is to define a simple type that determines the constraints and related information of an element's attribute value.
Restriction define a constraint condition
Test5.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified" attributeformdefault= "unqualified" >
<xs:simpletype name= "MyType" >
<xs:restriction base= "Xs:integer" >
<xs:mininclusive value = "0"/>
<xs:maxinclusive value = "/>"
</xs:restriction>
</xs:simpleType>
<xs:element name= "Hello" byte= "MyType" >
</xs:element>
</xs:schema>
Test5.xml
<?xml version= "1.0" encoding= "UTF-8"?>
xsi:nonamespaceschemalocation= "Test5.xsd" >
55
List to define a simple type element from a collection of specific data types. Test6.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified" attributeformdefault= "unqualified" >
<xs:simpletype name= "MyType" >
<xs:list itemtype= "Xs:integer"/>
</xs:simpleType>
<xs:element name= "Hello" type= "MyType"/>
</xs:schema>
Test6.xml
<?xml version= "1.0" encoding= "UTF-8"?>
xsi:nonamespaceschemalocation= "Test6.xsd" >
1 2 3
The Union chooses to define a simple type element from a collection of a particular simple data type.
Test7.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified" attributeformdefault= "unqualified" >
<xs:attribute name= "Allframesize" >
<xs:simpleType>
<xs:union membertypes= "Roadbikesize mountainbikesize"/>
</xs:simpleType>
</xs:attribute>
<xs:simpletype name= "Roadbikesize" >
<xs:restriction base= "Xs:positiveinteger" >
<xs:enumeration value= "/>"
<xs:enumeration value= "/>"
<xs:enumeration value= ">"
</xs:restriction>
</xs:simpleType>
<xs:simpletype name = "Mountainbikesize" >
<xs:restriction base= "Xs:string" >
<xs:enumeration value= "small"/>
<xs:enumeration value= "Medium"/>
<xs:enumeration value= "Large"/>
</xs:restruction>
</xs:simpleType>
<xs:element name = "Hello" >
<xs:complexType>
<xs:sequence>
<xs:element name= "Welcome" type= "Xs:string"/>
</xs:sequence>
<xs:attribute ref= "allframesize" use= "required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Test7.xml
<?xml version= "1.0" encoding= "UTF-8"?>
xsi:nonamespaceschemalocation= "test7.xsd" allframesize= "small" >
<welcome>hello world</welcome>
The complextype element function is to define a conforming type, which determines the attributes of the element to be constrained and related information.
Note: The difference between simpletype and complextype:
The elements in the simpletype type do not contain child elements and attributes.
When you need to declare a child element and/or attribute of an element, use complextype;
Use simpletype when you need to define a new data type based on the built-in base data type.
SimpleContent is applied to complextype to constrain and extend its content (the content of the element itself).
Note that the simplecontent element does not contain child elements, and extension represents the type of the element.
SimpleContent is used on the complextype element to qualify the type element of the complextype, indicating that the complextype has no child elements, and that the complextype needs to have attributes, otherwise it becomes simpletype.
Test8.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified" attributeformdefault= "unqualified" >
<xs:element name= "Shoesize" >
<xs:complexType>
<xs:simpleContent>
<xs:extension base= "Xs:decinal" >
<xs:attribute name= "Sizing" use= "required" >
<xs:simpleType>
<xs:restriction base= "Xs:string" >
<xs:enumeration value= "US" ></xs:enumeration>
<xs:enumeration
Value= "Europe" ></xs:enumeration>
<xs:enumeration value= "UK"/xs:enumeration>
<xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
Test8.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<shoesize xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "test8.xsd" sizing= "Enrope" >
1.2
</shoeSize>
The
Choice element allows a unique element to be selected from a group.
test9.xsd
<?xml version= "1.0" encoding= "UTF-8"?
<xs:schema xmlns:xs= "http://www.w3.org/2001/ XmlSchema "
elementFormDefault =" qualified " attributeformdefault=" unqualified ";
<xs:complextype name= "MyType";
<xs: Choice minoccurs= "1" maxoccurs= "1";
<xs:element name= "Hello" type= "xs:string"/>
<xs:element name= "World" type= "xs:string"/>
</xs:choice>
</xs:complextype>
<xs: Element name= "HelloWorld" type= "MyType"/>
</xs:shema>
Test9.xml
<?xml version= "1.0" encoding= "UTF-8"?
xsi:nonamespaceschemalocation=" test9.xsd ";
The
Sequence function is to give a set of data a specific sequence.
test10.xsd
<?xml version= "1.0" encoding= "UTF-8"?
<xs:schema xmlns:xs= "http://www.w3.org/2001/ XmlSchema "
elementFormDefault =" qualified " attributeformdefault=" unqualified ";
<xs: ComplexType name= "MyType";
<xs:sequence minoccurs= "1" maxoccurs= "1";
<xs: Element name= "Hello" type= "xs:string"/>
<xs:element name= "World" type= "xs:string"/>
</xs:sequence>
</xs:complextype>
<xs:element name= " HelloWorld "type=" MyType "/>
</xs:shema>
Test10.xml
<?xml version= "1.0" encoding= "UTF-8"?>
xsi:nonamespaceschemalocation= "Test10.xsd" >
<world>hehe</world>
Shema Classic Example Test11.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified" attributeformdefault= "unqualified" >
<xs:element name= "PurchaseOrder" type= "Purchaseordertype"/>
<xs:element name= "comment" type= "xs:string"/>
<xs:complextype name= "Purchaseordertype" >
<xs:sequence>
<xs:element name= "ShipTo" type= "USAddress"/>
<xs:element name= "BillTo" type= "USAddress"/>
<xs:element name= "comment" type= "0"/>
<xs:element name= "Items" type= "items"/>
</xs:sequence>
<xs:attribute name= "OrderDate" type= "Xs:date"/>
</xs:complexType>
<xs:complextype name= "USAddress" >
<xs:sequence>
<xs:element name= "name" type= "xs:string"/>
<xs:element name= "Street" type= "xs:string"/>
<xs:element name= "City" type= "xs:string"/>
<xs:element name= "City" type= "xs:string"/>
<xs:element name= "City" type= "xs:string"/>
</xs:sequence>
<xs:attribute name= "Country"
Type= "Xs:nmtoken"
fixed= "US"/>
</xs:complexType>
<xs:complextype name= "Items" >
<xs:sequence>
<xs:element name= "item" minoccurs= "0"
maxoccurs= "unbounded" >
<xs:complexType>
<xs:sequence>
<xs:element name= "ProductName" type= "xs:string"/>
<xs:element name= "Quantity"/>
<xs:simpleType>
<xs:restriction base= "Xs:positiveinteger" >
<xs:maxexclusive value= "/>"
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name= "Usprice" type= "xs:string"/>
<xs:element ref= "comment" minoccurs= "0" >
<xs:element name= "ShipDate" type= "xs:date" minoccurs= "0"/>
<xs:sequence>
<xs:attribute name= "PartNum" type= "SKU" use= "required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<!--Stock keeping unit,a code for identifying products-->
<xs:simpletype name= "SKU" >
<xs:restriction base= "Xs:integer" >
<xs:mininclusive= "2"/>
<xs:maxinclusive= "Ten"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Test11.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<purchaseorderxmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "Test11.xsd" >
<shipTo>
<name/>
<street/>
<city/>
<state/>
<zip>2.5</zip>
</shipTo>
<billTo>
<name/>
<street/>
<city/>
<state/>
<zip>1.2</zip>
</billTo>
</items>
</n1;purchaseOrder>
The root element of the document can be explicitly specified by DOCTYPE, because the element followed by DOCTYPE is the root element of the document.
There is no way to define the root element of the target XML document through schema, XMLSpy is to infer that the element contains other elements to select the element that contains the most other element as the root of the document, but we can explicitly specify the root element of the document without having to follow XMLSpy's build.