[转]XML详解--Schema
ma呢。因為DTD安全度太低了,也就是說它的約束定義能力不足,無法對XML實例文檔做出
更細(xì)致的語義限制。其實細(xì)心的人會發(fā)現(xiàn),在DTD中,只有一個數(shù)據(jù)類型,就是PCDATA(用
在元素中)和CDATA(用在屬性中),在里面寫日期也行,數(shù)字還行,字符更是沒問題。而
Schema正是針對這些DTD的缺點(diǎn)而設(shè)計的,Schema是完全使用XML作為描述手段,具有很強(qiáng)
的描述能力,擴(kuò)展能力和處理維護(hù)能力等。下面讓我們看一個簡單的例子吧:
hello.xml
-------------------
<?xml version="1.0"?>
<greeting>Hello World!!</greeting>
說明:
一個根元素:greeting;且這個元素不含屬性,無子元素,內(nèi)容是字符串。
hello.xsd
----------
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
? <xsd:element name="greeting" type="xsd:string"/>
</xsd:schema>
說明:
XML Schema文檔后綴名是.xsd,完全符合XML語法,根元素是schema,命名空間xmlns:xsd
="http://www.w3.org/2001/XMLSchema,用元素<element>定義實例文檔中的元素,如gre
eting。xsd:string就是定義的數(shù)據(jù)類型了,其中的數(shù)據(jù)類型有很多,比如:int,double,
dateTime,Boolean,long,integer,float,等,總之Java等語言里有的數(shù)據(jù)類型它都有,但
要以“xsd:”開頭。
讓我們再看一個里面有子無素的例子:
customer.xml
-----------
? <customer>
??? <name>teiki</name>
??? <address>No.237, Road Waitan, Shanghai</address>
? </customer>
則可以寫出以下的XML Schema文檔:
customer.xsd
----------------
1: <?xml version="1.0"?>
? 2: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
??? 3: <xsd:element name="customer">
????? 4: <xsd:complexType>
??????? 5: <xsd:sequence>
????????? 6: <xsd:element name="name" type="xsd:string"/>
????????? 7: <xsd:element name="address" type="xsd:string" />
????????? 8:?</xsd:sequence>
??????? 9: </xsd:complexType>
????? 10:?</xsd:element>
??? 11:?</xsd:schema>
說明:
實例文檔customer.xml中,<customer>元素含有兩個子元素,在Schema中凡是有兩個以上
的子元素,就認(rèn)為是復(fù)雜類型的,所以我們在Schema文檔中采用ComplexType來定義該元素
。表示有多個XML子元素。
sequence表示子元素依次出現(xiàn)的順序。
如果有多層子元素怎么辦呢,同樣的道理,有幾層寫幾層,一直往下嵌就OK了,這里我要
說的是另一種方法,結(jié)構(gòu)看上去會清晰一些。
address.xml
---------------
?? <customer>
????? <name>Teiki</name>
????? <address>
??????? <!-- address追加一個地址子元素 -->
??????? <prefecture>Zhejiang</prefecture>
??????? <city>Hangzhou</city>
??????? <street>Xilu Road, No.121, 7F</street>
????? </address>
??? </customer>
下面就是采用ref元素來編寫的這個Schema文檔:
address2.xsd
----------------------
??? 1: <?xm l version="1.0"?>
??? 2: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
????? 3:
????? 4: <xsd:element name="customer">
??????? 5: <xsd:complexType>
????????? 6: <xsd:sequence>
??????????? 7: <xsd:element name="name" type="xsd:string"/>
??????????? 8: <xsd:element ref="address"/>
??????????? 9: </xsd:sequence>
????????? 10: </xsd:complexType>
??????? 11: </xsd:element>
????? 12:
????? 13: <xsd:element name="address">
??????? 14: <xsd:complexType>
????????? 15: <xsd:sequence>
??????????? 16: <xsd:element name="prefecture" type="xsd:string"/>
??????????? 17: <xsd:element name="city" type="xsd:string" />
??????????? 18: <xsd:element name="street" type="xsd:string" />
??????????? 19: </xsd:sequence>
????????? 20: </xsd:complexType>
??????? 21: </xsd:element>
????? 22:
????? 23: </xsd:schema>
說明:
如果按正常的寫法,應(yīng)該把13-21行的內(nèi)容替換到每8行去,但這里使用ref元素可以直接
將其指向另一個模塊,使文檔更加具有可讀性。
如果元素中包含屬性怎么辦呢?一樣簡單,只要在定義完子元素的后面再著定義屬性就行
了。定義屬性用“attribute”,還是舉個例子吧
customer2.xml
---------------
??? <customer id="001718">
????? <name>Teiki</name>
????? <address>No.237, Road Waitan, Shanghai</address>
??? </customer>
這個例子和上面的一個例子差不多,只是在元素customer中設(shè)了一個屬性id。
customer2.xsd
------------------
1: <?xml version="1.0"?>
?
??? 2: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
????? 3:
????? 4: <xsd:element name="customer">
??????? 5: <xsd:complexType>
????????? 6: <xsd:sequence>
??????????? 7: <xsd:element name="name" type="xsd:string"/>
??????????? 8: <xsd:element name="address" type="xsd:string" />
??????????? 9: </xsd:sequence>
????????? 10: <!-- 增加屬性定義 -->
????????? 11: <xsd:attribute name="id" type="xsd:string"/>
????????? 12: </xsd:complexType>
??????? 13: </xsd:element>
????? 14:
????? 15: </xsd:schema>
說明:
需要注意的一點(diǎn)是,屬性和元素不是一家的,所以要把它放在sequence外面寫,但它們都
在customer的孩子,所以要寫在complexType的里面。
下面再看一個例子:
order4-1.xsd
----------------------
1:<?xml version="1.0"?>
?
?
??? 2:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
????? 3:
????? 4: <xsd:element name="order">
??????? 5: <xsd:complexType>
????????? 6: <xsd:sequence>
??????????? 7: <xsd:element ref="orderItem" maxOccurs="10"/>
??????????? 8: </xsd:sequence>
????????? 9: </xsd:complexType>
??????? 10: </xsd:element>
????? 11:
????? 12: <xsd:element name="orderItem">
??????? 13: <xsd:complexType>
????????? 14: <xsd:sequence>
??????????? 15: <xsd:choice>
????????????? 16: <xsd:element name="id" type="idType"/>
????????????? 17: <xsd:element name="name" type="xsd:string"/>
????????????? 18: </xsd:choice>
??????????? 19: <xsd:element name="quantity" type="quantityType"/>
??????????? 20: </xsd:sequence>
????????? 21: </xsd:complexType>
??????? 22: </xsd:element>
????? 23:
????? 24: <xsd:simpleType name="idType">
??????? 25: <xsd:restriction base="xsd:string">
????????? 26: <xsd:enumeration value="7-5058-3496-7"/>
????????? 27: <xsd:enumeration value="7-5005-6450-3"/>
????????? 28: <xsd:enumeration value="7-3020-6069-7"/>
????????? 29: </xsd:restriction>
??????? 30: </xsd:simpleType>
????? 31:
????? 32: <xsd:simpleType name="quantityType">
??????? 33: <xsd:restriction base="xsd:integer">
????????? 34: <xsd:minInclusive value="1"/>
????????? 35: <xsd:maxInclusive value="10"/>
????????? 36: </xsd:restriction>
??????? 37: </xsd:simpleType>
????? 38:
????? 39: </xsd:schema>
上面的例子中,maxOccurs代表:相同元素最多出現(xiàn)的次數(shù),與些相反的是minOccurs代表:
出現(xiàn)的最少次數(shù)。默認(rèn)情況下兩個都為“1”,如果把minOccurs設(shè)為“0”,表示該元素可
有可無。choice代表:可選的元素,也就是在這里面寫的元素只能選其中之一,不能全寫
。simpleType代表自定義數(shù)據(jù)類型,也就是name里的并不是真正的數(shù)據(jù)類型,而是根據(jù)自
己的意愿定制的。restriction代表對某一數(shù)據(jù)類型做約束,也就是只能取其中范圍之內(nèi)符
合要求的,比如第25-29行中,里面又有個元素enumeration代表枚舉,也就是只能在枚舉
的那幾個中選一個,而下在的36-36行,里面又有一對元素minInclusive和maxInclusive
代表類型的取值范圍,也就是只能取大于等于minInclusive并且小于等于maxInclusive的
數(shù)。
把這個Schema用在XML中是這樣的:
?
?<?xml version="1.0" encoding="UTF-8"?>??? <order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:/ProgramCode/Year2/XML/practice/temp.xsd">
????? <orderItem>
??????? <id>7-5005-6450-3</id>
??????? <quantity>3</quantity>
????? </orderItem>
????? <orderItem>
??????? <name>xyb</name>
??????? <quantity>3</quantity>
????? </orderItem>
????? <!-- 注意下面這個是錯的,因為超出了規(guī)定的范圍 -->
????? <orderItem>
??????? <id>7-5005-6450-3</id>
??????? <quantity>13</quantity>
????? </orderItem>
????? <!? 從上面的注釋到此,是錯誤的 -->
??? </order>
說明:
紅色的部分是導(dǎo)入Schema的命令,綠色的是Schema文件的路徑,如果用XMLSPY寫時,會自
動加上。
對屬性的設(shè)置,基本上和元素的差不多。比如:
<xsd:element name="orderItem">
????? <xsd:complexType>
??????? <xsd:sequence></xsd:sequence>
??????? <xsd:attribute name="id" type="idType" use="required"/>
??????? <xsd:attribute name="quantity" type="xsd:integer" default="1"/>
????? </xsd:complexType>
????? <xsd:simpleType name="idType">
??????? <xsd:restriction base="xsd:string">
????????? <xsd:pattern value="/d{1}-/d{4}-/d{4}-/d{1}"/>
??????? </xsd:restriction>
????? </xsd:simpleType>
??? </xsd:element>
這里我們講id屬性類型作為一種自定義數(shù)據(jù)類型idType。它的格式就是上面用到的也就是
類似于“7-5005-6450-3”樣的。
而且,用attribute元素的use屬性來定義是否是必須的屬性。
required是必須值,optional是可選值,prohibited是無屬性值。default屬性是默認(rèn)值。
總結(jié)
以上是生活随笔為你收集整理的[转]XML详解--Schema的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 海龟交易法则_【手把手教你
- 下一篇: asp.net ajax控件工具集 Au