XML學習筆記(三)

1、Schema

1.1、什麼是Schema

Schema:原稱爲XSD(XML Schema Definition),是由微軟提出的規範,現成爲W3C規範
XML Schema是用一套內置的XML元素和屬性創建的,這些元素和屬性定義了XML文檔的結構和內容模式。
XML Schema規定XML文檔實例的結構和每個元素/屬性的數據類型

1.2、體驗dtd和schema的不同之處

首先創建一個xml文檔

<?xml version="1.0" encoding="UTF-8"?>
<student>
    <name>貂蟬</name>
    <age>20</age>
</student>

先使用內部的dtd對xml文檔做約束

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE student[
<!ELEMENT student (name,age)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>
<student>
    <name>貂蟬</name>
    <age>20</age>
</student>

那麼如果採用schema如何來編寫呢?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="name" type="xs:string"></xs:element>
    <xs:element name="age" type="xs:int"></xs:element>
    <!--定義一個數據類型-->
    <xs:complexType name="stuType">
        <xs:sequence>
            <xs:element ref="name"></xs:element>
            <xs:element ref="age"></xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="student" type="stuType"></xs:element>
</xs:schema>

編寫一個xml引用此schema

<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Administrator\Desktop\demo\xsd01.xsd">
    <name>關羽</name>
    <age>20</age>
</student>

1.3、dtd與Schema的比較

dtd的語法的侷限性:

1、dtd使用的是一種語法,而xml使用的是另外一種語法

2、dtd的數據類型是有限的,與我們講的數據庫中數據類型不一致

3、dtd不可以擴展,並且不支持命令空間

Schema的好處:

1、Schema也是遵守xml的語法

2、schema大大擴充了數據類型,也可以自定義自己的數據類型

3、它也支持元素的繼承,支持元素組合屬性組的概念

4、開發性,多個schema的聲明可以應用到一個xml文檔中。

1.4、Schema文檔的結構

schema文檔的結構

mark

引用schema的文檔

mark

1.5、Schema中的數據類型(知道即可)

數據類型分類:

簡單類型

​ 內置的數據類型(built-in data types)

​ l基本的數據類型(Primitive Data Types)

​ l擴展的數據類型(Deriverd Data Types)

​ 用戶自定義數據類型(通過simpleType定義)

複雜類型(通過complexType定義)

基本數據類型

數據類型 描述
string 表示字符串
boolean 布爾型
decimal 代表特定精度的數字
float 表示單精度32位浮點數
double 表示雙精度64位浮點數
duration 表示持續時間
dateTime 代表特定的時間
time 代表特定的時間,但是是每天重複的
date 代表日期
hexBinary 代表十六進制數
anyURI 代表一個URI,用來定位文件
QName 代表限定(qualified)名稱,即有前綴的字符串

擴展的數據類型

數據類型 描述
ID 用於唯一標識元素
IDREF/ IDREFS 參考ID類型的元素或屬性
ENTITY/ENTITIES 實體類型
NMTOKEN NMTOKEN類型
NMTOKENS NMTOKEN類型集
long 表示整型數,大小介於-9223372036854775808 和9223372036854775807之間
int 表示整型數,大小介於-2147483648和 2147483647之間
short 表示整型數,大小介於-32768和32767之間
byte 表示整型數,大小介於-128和127之間
NOTATION 代表 NOTATION類型,QName的派生類型

其他數據類型

數據類型 描述
gYear/gYearMonth 指定格式的日期
gMonth/gMonthDay 指定格式的日期
gDay 指定格式的日期
base64Binary 使用base64-encoded編碼的2進制數據
language 語言字符,string的派生
Name 有效的XML字符串,string的派生
NCName Name的派生,字符串中不允許“:”
negativeInteger 表示負整數,<= -1
positiveInteger 表示正整數,>=1
nonNegativeInteger >=0,另有nonPositiveInteger表示<=0
unsignedLong 無符號數,還有un…Int、Short、Byte

數據類型的約束

特性 描述
enumeration 在指定的數據集中選擇,限定用戶的選值
fractionDigits 限定最大的小數位,用於控制精度
length 指定數據的長度
maxExclusive / minExclusive 指定數據的最大/小值(小於/大於)
maxInclusive / minInclusive 指定數據的最大/小值(小於等於/大於等於)
maxLength / minLength 指定長度的最大/小值
pattern 指定數據的顯示規範,具體規則見下頁
whiteSpace 空白字符的處理:preserve/replace/collapse
precision 十進制數據允許的位數
scale 小數部分的位數

正則描述

規範 描述
\d 任意一個數字,如:Chap\dàChap1、Chap9……
* 前的字符任意重複,如:abàb、ab、aaaaab……
[ ] [ ]內的字符集中任意一個,如:[xyz]aàxa、ya、za
?前的字符出現0或1次
+ + 前的字符至少出現1次
- 指定範圍,如:[a-d]yàay、by、cy、dy
{n} { }前的字符出現n次,如:a{3}xàaaax
{min,max} { }前的字符至少出現min次,至多max次,如:a{1,3}xàax、aax、aaax

1.5、Schema中的元素類型

•schema、element、attribute、group、attributeGroup

•simpleType

•simpleContent

•complexType

1.5.1、element

案例:xsd02.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="name" type="xs:string"></xs:element>
<xs:element name="type" type="xs:string"></xs:element>
<!--定義是一個類型-->
<xs:complexType name="heroType">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="name"></xs:element>
        <xs:element ref="type"></xs:element>
    </xs:sequence>
</xs:complexType>

<xs:element name="hero" type="heroType"></xs:element>
<!--定義了一個類型,這個類型中包含多個hero元素-->
<xs:complexType name="herosType">
    <xs:sequence maxOccurs="unbounded">
        <xs:element ref="hero"></xs:element>
    </xs:sequence>
</xs:complexType>

<xs:element name="heros" type="herosType"></xs:element>

</xs:schema>

xml文件引用

<?xml version="1.0" encoding="UTF-8"?>
<heros xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Administrator\Desktop\demo\xsd02.xsd">
    <hero>
        <name>關羽</name>
        <type>戰士</type>
    </hero>
    <hero>
        <name>曹操</name>
        <type>戰士</type>
    </hero>
    <hero>
        <name>亞瑟</name>
        <type>戰士</type>
    </hero>
    <hero>
        <name>安其拉</name>
        <type>法師</type>
    </hero>
</heros>

1.5.2、group元素

把一組元素聲明組合在一起,以便它們能夠一起被複合類型應用

案例,使用group組來進行改寫代碼

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!--定義元素-->
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="type" type="xs:string"></xs:element>

<!--定義一個元素組-->
<xs:group name="myElGroup">
    <xs:sequence>
        <xs:element ref="name"></xs:element>
        <xs:element ref="type"></xs:element>
    </xs:sequence>
</xs:group>
<!--定義是一個類型-->
<xs:complexType name="heroType">
    <xs:group ref="myElGroup"></xs:group>
</xs:complexType>

<xs:element name="hero" type="heroType"></xs:element>
<!--定義了一個類型,這個類型中包含多個hero元素-->
<xs:complexType name="herosType">
    <xs:sequence maxOccurs="unbounded">
        <xs:element ref="hero"></xs:element>
    </xs:sequence>
</xs:complexType>

<xs:element name="heros" type="herosType"></xs:element>

</xs:schema>

實際上xml文件並不需要發生任何的改變

1.5.3、attribute和attributeGroup

案例1:直接定義屬性

<xs:complexType name="heroType">
    <xs:group ref="myElGroup"></xs:group>
    <xs:attribute name="age" type="xs:int" use="required"></xs:attribute>
    <xs:attribute name="birthday" type="xs:date" use="optional"></xs:attribute>
    <xs:attribute name="addr" type="xs:string" use="prohibited"></xs:attribute>
</xs:complexType>

然後我們在使用attributeGroup來定義屬性組

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!--定義元素-->
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="type" type="xs:string"></xs:element>

<!--定義一個元素組-->
<xs:group name="myElGroup">
    <xs:sequence>
        <xs:element ref="name"></xs:element>
        <xs:element ref="type"></xs:element>
    </xs:sequence>
</xs:group>
<xs:attributeGroup name="myAttrGroup">
    <xs:attribute name="age" type="xs:int" use="required"></xs:attribute>
    <xs:attribute name="birthday" type="xs:date" use="optional"></xs:attribute>
    <xs:attribute name="addr" type="xs:string" use="prohibited"></xs:attribute>
</xs:attributeGroup>
<!--定義是一個類型-->
<xs:complexType name="heroType">
    <xs:group ref="myElGroup"></xs:group>
    <xs:attributeGroup ref="myAttrGroup"></xs:attributeGroup>
</xs:complexType>

<xs:element name="hero" type="heroType"></xs:element>
<!--定義了一個類型,這個類型中包含多個hero元素-->
<xs:complexType name="herosType">
    <xs:sequence maxOccurs="unbounded">
        <xs:element ref="hero"></xs:element>
    </xs:sequence>
</xs:complexType>
<xs:element name="heros" type="herosType"></xs:element>
</xs:schema>

1.5.4、simpleType

•作用:定義一個簡單類型,它決定了元素和屬性值的約束和相關信息

•屬性:name

•內容:應用已經存在的簡單類型,三種方式:

restriction:限定一個範圍

list:從列表中選擇

union:包含一個值的結合

案例1:schema文件定義:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:simpleType name="myAge">
        <xs:restriction base="xs:int">
            <xs:minInclusive value="0"></xs:minInclusive>
            <xs:maxInclusive value="100"></xs:maxInclusive>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="sname" type="xs:string"></xs:element>
    <xs:element name="age" type="myAge"></xs:element>
    <xs:complexType name="stuType">
        <xs:sequence minOccurs="1" maxOccurs="unbounded">
            <xs:element ref="sname"></xs:element>
            <xs:element ref="age"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="student" type="stuType">  
    </xs:element>
</xs:schema>

引用

<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Administrator\Desktop\demo\xsd03.xsd">
    <sname>貂蟬</sname>
    <!--此時age中內容必須符合myage的定義要求-->
    <age>100</age>
</student>

案例2:

<xs:simpleType name="mybirthday">
        <xs:list itemType="xs:date"></xs:list>
</xs:simpleType>

引用

<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Administrator\Desktop\demo\xsd03.xsd">
    <sname>貂蟬</sname>
    <!--此時age中內容必須符合myage的定義要求-->
    <age>100</age>
    <birthday>2012-01-22 2018-02-22</birthday>
</student>

案例3:

<?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>
      <xs:simpleType>
        <xs:restriction base="roadbikesize"/>
      </xs:simpleType>
      <xs:simpleType>
        <xs:restriction base="mountainbikesize"/>
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
</xs:attribute>

<!--定義了一個simpleType類型-->
<xs:simpleType name="roadbikesize">
  <xs:restriction base="xs:positiveInteger">
    <xs:enumeration value="16"/>
    <xs:enumeration value="20"/>
    <xs:enumeration value="28"/>
  </xs:restriction>
</xs:simpleType>

<!--定義了一個simpleType類型-->
<xs:simpleType name="mountainbikesize">
  <xs:restriction base="xs:string">
    <xs:enumeration value="small"/>
    <xs:enumeration value="medium"/>
    <xs:enumeration value="large"/>
  </xs:restriction>
</xs:simpleType>

<xs:element name="name" type="xs:string"></xs:element>
<xs:complexType name="stuType">
    <xs:sequence minOccurs="1" maxOccurs="5">
        <xs:element ref="name"></xs:element>
    </xs:sequence>
    <xs:attribute ref="allframesize"></xs:attribute>
</xs:complexType>
<xs:element name="student" type="stuType"></xs:element>

</xs:schema>

引用文件

<?xml version="1.0" encoding="UTF-8"?>
<!--allframesize的值可以是roadbikesize或者mountainbikesize-->
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Administrator\Desktop\demo\xsd04.xsd" allframesize="small">
    <name></name>
</student>

1.5.5、complexType和simpleContent元素

complexType:定義一個複合類型,它決定了一組元素和屬性值的約束和相關信息

simpleContent:應用於complexType,對它的內容進行約束和擴展。

案例:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="stuType">

        <xs:simpleContent>
            <!--內容只能放小數-->
         <xs:extension base="xs:decimal">
             <!--stuType有一個屬性sizing-->
            <xs:attribute name="sizing" use="required">
                <!--屬性是基於simpleType類型的-->
                <xs:simpleType>
                 <xs:restriction base="xs:string">
                    <xs:enumeration value="US"/>
                    <xs:enumeration value="Euro"/>
                    <xs:enumeration value="UK"/>
                 </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
         </xs:extension>
        </xs:simpleContent>

    </xs:complexType>

    <xs:element name="student" type="stuType"></xs:element>
</xs:schema>

引用schema文件

<?xml version="1.0" encoding="UTF-8"?>
<student sizing="US" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Administrator\Desktop\demo\xsd05.xsd">
    10.1
</student>

總結一下:

SimpleType:基於已有數據類型的擴展

ComplexType:定義一個複合類型

SimpleContent:對ComplexType的內容做限定,這個複合類型沒有子元素,只能存在文本或者屬性

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章