XML Schema offers another way to define the structure of XML documents.
例子1:
<?xml version="1.0" ?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
</xsd:schema>
"http://www.w3.org/2000/10/XMLSchema" 是这个名称空间得real name,不是一个url.
xsd是这个名称空间得nickname.
例子2:
xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<order xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="example.xsd">
9 shirts
</order>
相关得schema example.xsd:
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
<xsd:element name="order" type="xsd:string" /> <!-->建立一个元素,类型是string<-->
</xsd:schema>
简单类型
xsd:string
xsd:decimal
xsd:integer
xsd:positiveInteger
xsd:negativeInteger
xsd:nonPositiveInteger
xsd:nonNegativeInteger
xsd:float <!-->32 bit<-->
xsd:double <!-->64 bit<-->
xsd:date <!-->YYYY-MM-DD; 1970-01-28<-->
xsd:time <!-->hh:mmss.sss; 05:45:00<-->
xsd:timeInstant <!-->YYYY-MM-DDThh:mmss.sss; 2000-09-12T17:21:35<-->
xsd:timeDuration
xsd:month <!-->YYYY-MM<-->
xsd:year <!-->YYYY<-->
xsd:century <!-->YY<-->
xsd:recurringDate <!-->--MM-DD<-->
xsd:recurringDay <!-->---DD<-->
xsd:boolean <!-->true,false,0,1<-->
xsd:language <!-->ISO639; ex.EN is english...<-->
xsd:uri-reference <!-->specify a url; ex.http://www.wire-man.com<-->
xsd:NMTOKEN <!-->force the text to be a valid XML name<-->
创建自己得简单类型
<?xml version="1.0" ?>
- <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
<xsd:element name="color"> <!-->建立一个元素<-->
- <xsd:simpleType> <!-->建立一个新得简单元素<-->
- <xsd:restriction base="xsd:string"> <!-->基于string类型,加入额外限制<-->
<xsd:enumeration value="purple" /> <!-->定义颜色可选项<-->
<xsd:enumeration value="orange" />
<xsd:enumeration value="blue" />
<xsd:enumeration value="bordeaux" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>
强制文本规范pattern
<xsd:element name="phone_number">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="(\d{3})\s\d{3}-\d{4}"/> <!-->xsd:pattern<-->
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
\d any digit
\D any nondigit
\s white space, carriage return, new line, or return
\S any non-white space character
(ab)* anything in parentheses may appear zero or more times
(ab)+ anything in parentheses may appear one or more times
(ab)? anything in parentheses may appear zero or one times
a{n} "a" must appear n times in a row
限制数值范围
<xsd:element name="numShirtsDiscountOrder">
<xsd:simpleType>
<xsd:restriction base="xsd:integer"> <!-->基于integer类型,加入额外限制<-->
<xsd:maxInclusive value="2000" /> <!-->数值最大值<-->
<xsd:minInclusive value="500" /> <!-->数值最小值<-->
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
限制数值精度
<xsd:element name="scienceNum">
<xsd:simpleType>
<xsd:restriction base="xsd:decimal">
<xsd:precision value="6" /> <!-->数值最大总位数<-->
<xsd:scale value="3"/> <!-->数值最大小数点后位数<-->
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
ex.
<scienceNum>123.456</scienceNum>
<scienceNum>7</scienceNum>
限制string长度
<xsd:element name="state">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="2"/> <!-->设置string得长度xsd:length<-->
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="state">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="2" /> <!-->设置string得最小长度<-->
<xsd:maxLength value="13" /> <!-->设置string得最大长度<-->
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
创建list
<xsd:element name="sizes">
<xsd:simpleType>
<xsd:list itemType="xsd:string" />
<xsd:length value="5"> <!-->设置list长度<-->
or
<xsd:minLength value="2"> <!-->设置list得最小长度<-->
<xsd:maxLength value="7"> <!-->设置list得最大长度<-->
</xsd:simpleType>
</xsd:element>
ex.
<sizes>XL XXL S XS M</sizes>
组合简单类型
<xsd:element name="orderDate">
<xsd:simpleType>
<xsd:union>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="yesterday" />
<xsd:enumeration value="today" />
<xsd:enumeration value="tomorrow" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType>
<xsd:restriction base="xsd:date" />
</xsd:simpleType>
</xsd:union>
</xsd:simpleType>
</xsd:element>
ex.
<orderDate>yesterday</orderDate>
<orderDate>2001-04-19</orderDate>
定义元素content
<xsd:element name="color" type="xsd:string" fixed="blue" />
<xsd:element name="size" type="xsd:string" default="M" />
重用用户简单类型
<xsd:simpleType name="phoneNumber"> <!-->定义类型名字<-->
<xsd:restriction base="xsd:string">
<xsd:pattern value="(\d{3})\s\d{3}-\d{4}" />
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="mainPhone" type="phoneNumber" />
<xsd:element name="cellPhone" type="phoneNumber" />
<xsd:element name="faxPhone" type="phoneNumber" />