XSD的学习

一 基础概念

schema:root element of each xml schema.and contain several attributes.like xmlns:xs,targetNameSpace,xmlns,elementFormDefault.
element:it have single element and complext element.And the element type can be string,decimal,integer,boolean,date,time.
attribute:used in the element.like element,it can use many types.
restrictions:<xs:restriction>,<xs:minInclusive>,<xs:maxInclusive>,<xs:simpleType>,<xs:pattern>
simpleType:element,attribute,restriction.
group:a group element.
attributeGroup:a set of attribute declarations.
choice:allow only one element contained in the <choice> declaration.
complexType:a complexType is an XML element that contains other elements or attributes.
union:defines a simple type as a collection (union) of values from specified simple data types
unique:an element or an attribute value must be unique within the scope.
sequence:the child elements must appear in a sequence

二:具体的XSD

<xs:schema xmlns:xs="">

<xs:group name="custGroup">
<xs:sequence>
<xs:element name="customer" type="xs:string"/>
<xs:element name="orderdetails" type="xs:string"/>
<xs:element name="billto" type="xs:string"/>
<xs:element name="shipto" type="xs:string"/>
</xs:sequence>
</xs:group>


<xs:element name="name" type="xs:string" default="Zhangsan>
<xs:attribute name="sex" type="xs:string" default="male" use="required"></xs:attribute>

</xs:element>

<xs:element name="age" type="xs:integer">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
<xs:element>

<xs:complexType name="ordertype">
<xs:group ref="custGroup"/>
<xs:attribute name="status" type="xs:string"/>
</xs:complexType>

<xs:attributeGroup name="personattr">
<xs:attribute name="attr1" type="string"/>
<xs:attribute name="attr2" type="integer"/>
</xs:attributeGroup>

<xs:complexType name="person">
<xs:attributeGroup ref="personattr"/>
</xs:complexType>

<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>

<xs:element name="jeans_size">
<xs:simpleType>
<xs:union memberTypes="sizebyno sizebystring" />
</xs:simpleType>
</xs:element>

<xs:simpleType name="sizebyno">
<xs:restriction base="xs:positiveInteger">
<xs:maxInclusive value="42"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="sizebystring">
<xs:restriction base="xs:string">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
</xs:restriction>
</xs:simpleType>

<xs:unique name="IDUnique">
<xs:selector xpath="学生"/>
<xs:field xpath="@学号"/>
</xs:unique>

<xs:element name="personinfo">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

原文地址:https://www.cnblogs.com/SoulSpirit/p/2935609.html