XML Schema offers another way to define the structure of XML documents.
例子1:
<?xml version="1.0" ?>
<xsd:schema
xmlns:xsd=" </xsd:schema>
"xsd是這個名稱空間得nickname.
例子2:
xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<order xmlns:xsi=" xsi:noNamespaceSchemaLocation="example.xsd">
9 shirts
</order>
相關得schema example.xsd:
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd=" <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=" <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" />