天天看點

Xml之Schema XSD限制{詳細}

問題:

學習Schema其他标簽的定義 限制

​​引入的方式:​​

​​基本格式:​​

​​1建構schema:​​

​​1.1 最基本的機關元素​​

​​1.2 元素屬性​​

​​1.3 simpleType 定義類型​​

​​1.4 複合結構類型​​

​​1.5訓示器​​

​​1.6 擴充元素 屬性​​

​​1.7 元素替換與阻止​​

​​2:了解一些類型​​

​​2.1 時間類型​​

​​2.2 數值資料​​

​​2.3 布爾 二進制資料​​

引入的方式:

1:通過網絡路徑映入

<?xml version="1.0" encoding="UTF-8"?>
<Teams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.example.org/NewXMLSchema"
    xsi:noNamespaceSchemaLocation="NewXMLSchema.xsd">
    <Team>
        <Teamname>Roma</Teamname>
        <Country>Italy</Country>
        <Member Name="AAA" Age="34" Sex="Male"/>
        <Member Name="AAA" Age="34" Sex="Male"/>
    </Team>
</Teams>      

通過檔案路徑引入

 xsi:noNamespaceSchemaLocation="{location}" #  XSD檔案名 本地檔案

 xsi:schemaLocation="{namespace} {location}" # 帶全路徑的XSD檔案

<?xml version="1.0" encoding="UTF-8"?>
<Teams xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.example.org/NewXMLSchema"
    xsi:schemaLocation="http://www.dgwcom.com/NewXMLSchema.xsd">
    <Team>
        <Teamname>Roma</Teamname>
        <Country>Italy</Country>
        <Member Name="AAA" Age="34" Sex="Male"/>
        <Member Name="AAA" Age="34" Sex="Male"/>
    </Team>
</Teams>      

基本格式:

 targetNamespace:

 在引入的時候需要寫的命名空間

  elementFormDefault

 非全局的元素當設定為qualified時,必須添加命名空間的字首。

 非全局的元素當設定為unqualified時,不必也不能添加字首。(使用具體元素的時候)

加上xs 那麼目标元素也要寫命名空間xm

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.dgwcom.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
    <xs:element name="note">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="to" type="xs:string"/>
          <xs:element name="from" type="xs:string"/>
          <xs:element name="heading" type="xs:string"/>
          <xs:element name="body" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
</xs:schema>      

1建構schema:

   1.1 最基本的機關元素

<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
# 對應元素
    <element name="lastname" type="string" />
    <element name="age" type="integer" />
    <element name="dateborn" type="date" />
# 常用屬性
<element 
name="name"  元素名稱
default=""  預設值
type=""  類型
fixed="" 固定值
id=""  定義id
ref  引用别的元素
maxOcuor 最大範圍
min0cuor 最小範圍
></element>
https://www.runoob.com/schema/schema-el-element.html      

       1.2 元素屬性

<lastname lang="EN">Smith</lastname>
# 對應屬性
<attribute name="lang" type="string" default="EN"></attribute>
<xs:attribute name="lang" type="xs:string" fixed="EN"/> //固定值
<xs:attribute name="lang" type="xs:string" use="required"/>      

      1.3 simpleType 定義類型

兩種構造方式

# 方式1
 <element name="xx" type="agetype"></element>
 <simpleType name="agetype">
        <restriction base="integer">
            <minInclusive value="0" />
            <maxInclusive value="120" />
        </restriction>
    </simpleType>
# 方式2
    <element name="xx222">
        <simpleType>
            <restriction base="integer">
                <maxInclusive value="0"></maxInclusive>
                <minInclusive value="120"></minInclusive>
            </restriction>
        </simpleType>
    </element>
 # 控制範圍的相對性
     控制範圍内
    <maxInclusive value="0"></maxInclusive>
    <minInclusive value="120"></minInclusive>
     控制範圍外
    <maxExclusive value=""></maxExclusive>
    <minExclusive value=""></minExclusive>      

例子

# 清單限定 車的類型
    <simpleType name="cartype">
        <restriction base="string">
            <enumeration value="Audi"></enumeration>
            <enumeration value="Golf"></enumeration>
            <enumeration value="BMW"></enumeration>
        </restriction>
    </simpleType>
# 正則限定
    <!-- 小寫字母 -->
    <simpleType name="letter">
        <restriction base="string">
            <pattern value="[a-z]"></pattern>
        </restriction>
    </simpleType>
     <!-- 字母數字 -->
         <simpleType name="letter">
        <restriction base="string">
            <pattern value="([a-z][A-Z]0-9])+"></pattern>
        </restriction>
    </simpleType>
 # 空白字元的限定
   <!--  對空白字元的處理 -->
    <simpleType name="address">
        <restriction>
            <whiteSpace value="preserve"></whiteSpace>
           1:preserve : XML 處理器不會移除任何空白字元
           2:collapse : XML 處理器将移除所有空白字元(換行、回車、空格以及制表符會被替換為空格,開頭和結尾的空格會被移除,而多個連續的空格會被縮減為一個單一的空格
           3:replace  :  XML 處理器将移除所有空白字元(換行、回車、空格以及制表符):
        </restriction>
    </simpleType>
 # 對長度的限定
 <!-- 對長度的限定 -->
    <simpleType name="NameType">
        <restriction base="integer">
        <maxLength value="1"></maxLength>
        <minLength value="5"></minLength>
        </restriction>
    </simpleType>
 # 所有屬性
 限定    描述
enumeration    定義可接受值的一個清單
fractionDigits    定義所允許的最大的小數位數。必須大于等于0。
length    定義所允許的字元或者清單項目的精确數目。必須大于或等于0。
maxExclusive    定義數值的上限。所允許的值必須小于此值。
maxInclusive    定義數值的上限。所允許的值必須小于或等于此值。
maxLength    定義所允許的字元或者清單項目的最大數目。必須大于或等于0。
minExclusive    定義數值的下限。所允許的值必需大于此值。
minInclusive    定義數值的下限。所允許的值必需大于或等于此值。
minLength    定義所允許的字元或者清單項目的最小數目。必須大于或等于0。
pattern    定義可接受的字元的精确序列。
totalDigits    定義所允許的阿拉伯數字的精确位數。必須大于0。
whiteSpace    定義空白字元(換行、回車、空格以及制表符)的處理方式。      

1.4 複合結構類型

<employee>
          <firstname>John</firstname>
          <lastname>Smith</lastname>
</employee>
# 對應執行個體
<element name="employee">
        <complexType>
            <sequence>
                    <element name="firstname" type="string"></element>
                    <element name="lastname" type="string"></element>
            </sequence>
        </complexType>
 </element>
 # 對有屬性值的描述的 
 <!-- <shoesize country="france">35</shoesize> -->
    <element name="shoesize">
        <complexType>
            <simpleContent>
                <extension base="integer">
                    <attribute name="country"  type="string"></attribute>
                </extension>
            </simpleContent>
        </complexType>
    </element>
 # 空内容元素
<!-- <product prodid="1345" /> -->
    <element name="product">
        <complexType>
            <attribute name="prodid" type="positiveInteger"></attribute>
        </complexType>
    </element>
 # 有元素 又有屬性
     <!-- 
        <user>
            <name age='12' address=' '>Tom</name>
        </user>
     -->
     <element name="user">
         <complexType>
             <sequence>
                 <element name="name" type="string">
                     <complexType>
                         <attribute name="age" type="int"></attribute>
                         <attribute name="address" type="string"></attribute>
                     </complexType>
                 </element>
             </sequence>
         </complexType>
     </element>
  ## 對上面的還有種寫法
<element name="name"> 
      <complexType> 
            <complexContent> 
                  <extension base="string"> 
                    <attribute name="age" type="int"/>  
                    <attribute name="address" type="string"/> 
                  </extension> 
            </complexContent> 
      </complexType> 
</element>
 # 複合類型的寫法 基本上  complexType  simpleType  都可以單獨包裝出來 作為一個共有類型
   <element name="letter">
         <complexType mixed="true">
             <sequence>
                 <element name="name" type="string"></element>
                 <element name="orderid" type="positiveInteger"></element>
                 <element name="shipdate" type="date"></element>
             </sequence>
         </complexType>
     </element>
  # 多個元素嵌套
       <element name="Persons">
         <complexType>
             <sequence>
                 <element name="user" maxOccurs="unbounded">
                     <complexType>
                         <sequence>
                             <element name="name" type="string"/>
                             <element name="age" type="string" />
                         </sequence>
                     </complexType>
                 </element>
             </sequence>
         </complexType>
     </element>      

1.5訓示器

order順序訓示器

1: all   不按照給出的順序出現, 但是每個元素 可出現一次 或者不出現
2: sequence 必須按照給出的順序出現
3: choise   多個ele選一
<xs:element name="person">
  <xs:complexType>
    <xs:all>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>
<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="person">
   <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>      

Occurrence 訓示器

  對于所有的 "Order" 和 "Group" 訓示器(any、all、choice、sequence、group name 以及 group reference),其中的 maxOccurs 以及 minOccurs 的預設值均為 1。

<maxOccurs> 訓示器可規定某個元素可出現的最大次數:
<minOccurs> 訓示器可規定某個元素能夠出現的最小次數:
<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>      

訓示器: 分為元素組 屬性組  目的: 對某系元素 屬性進行封裝

<group name=""></group> // 元素組
<attributeGroup name=""></attributeGroup> //屬性組

# 元素組
# 屬性組
<attributeGroup name="telAtt">
        <attribute name="tel" type="integer"></attribute>
    </attributeGroup>
    
    <group name="personEle">
        <sequence>
            <element name="address" type="string" />
        </sequence>
    </group>

    <element name="Persons">
        <complexType>
            <sequence>
                <element name="user" maxOccurs="unbounded">
                    <complexType>
                        <sequence>
                            <element name="name" type="string" />
                            <element name="age" type="string">
                                <complexType>
                                    <attributeGroup ref="telAtt"></attributeGroup>
                                </complexType>
                            </element>
                            <group ref="personEle"></group>
                        </sequence>
                    </complexType>
                </element>
            </sequence>
        </complexType>
    </element>      

1.6 擴充元素 屬性

分為元素擴充<any>  屬性擴充<anyAttribute>

# Person 中擴充一個child
<xs:element name="person"> // 
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
    <xs:anyAttribute/>
  </xs:complexType>
</xs:element>
<xs:element name="children">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="childname" type="xs:string"  maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element> //以上兩個xsd必須定義

# 具體使用 
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com children.xsd">
<person>
  <firstname>Hege</firstname>
  <lastname>Refsnes</lastname>
  <children>
    <childname>Cecilie</childname>
  </children>
</person>

<person>
  <firstname>Stale</firstname>
  <lastname>Refsnes</lastname>
</person>
</persons>

# 參數擴充 姓名中擴充性别
<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
    <xs:anyAttribute/>
  </xs:complexType>
</xs:element>
<xs:attribute name="gender"> //擴充的檔案
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="male|female"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>
## 具體例子
<person gender="female">
  <firstname>Hege</firstname>
  <lastname>Refsnes</lastname>
</person>
<person gender="male">
  <firstname>Stale</firstname>
  <lastname>Refsnes</lastname>
</person>
</persons>      

  1.7 元素替換與阻止

# 替換的例子
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>   //navn替換 name
#阻止替換的例子
<xs:element name="name" type="xs:string" block="substitution"/> // 被阻止替換
<xs:element name="navn" substitutionGroup="name"/>
# 替換後使用的 都是有效的
<customer>
  <name>John Smith</name>
</customer>
或類似這樣:
<kunde>
   <navn>John Smith</navn>
</kunde>      

2:了解一些類型

 2.1 時間類型

# 1
<xs:element name="start" type="xs:date"/>
文檔中的元素看上去應該類似這樣:
<start>2002-09-24</start>
# 2
<xs:element name="start" type="xs:time"/>
文檔中的元素看上去應該類似這樣:
<start>09:00:00</start>
# 3
<xs:element name="startdate" type="xs:dateTime"/>
文檔中的元素看上去應該類似這樣:
<startdate>2002-05-30T09:00:00</startdate>
# 4 持續時間 不建議使用 xs:duration
問法規則:
    P 表示周期(必需)
    nY 表示年數
    nM 表示月數
    nD 表示天數
    T 表示時間部分的起始 (如果您打算規定小時、分鐘和秒,則此選項為必需)
    nH 表示小時數
    nM 表示分鐘數
    nS 表示秒數
<xs:element name="period" type="xs:duration"/>
文檔中的元素看上去應該類似這樣:
<period>P5Y</period>
上面的例子表示一個 5 年的周期。
或者類似這樣:
<period>P5Y2M10D</period>
上面的例子表示一個 5 年、2 個月及 10 天的周期。
或者類似這樣:
<period>P5Y2M10DT15H</period>
上面的例子表示一個 5 年、2 個月、10 天及 15 小時的周期。
或者類似這樣:
<period>PT15H</period>      

2.2 數值資料

@# 1  xs:decimal
<xs:element name="prize" type="xs:decimal"/>
文檔中的元素看上去應該類似這樣:
<prize>999.50</prize>
或者類似這樣:
<prize>+999.5450</prize>
或者類似這樣:
<prize>-999.5230</prize>
或者類似這樣:
<prize>0</prize>
或者類似這樣:
<prize>14</prize>
# 2 
<xs:element name="prize" type="xs:integer"/>
文檔中的元素看上去應該類似這樣:
<prize>999</prize>
或者類似這樣:
<prize>+999</prize>
或者類似這樣:
<prize>-999</prize>
或者類似這樣:
<prize>0</prize      

2.3 布爾 二進制資料

# 1  xs:boolean
<xs:attribute name="disabled" type="xs:boolean"/>
文檔中的元素看上去應該類似這樣:
<prize disabled="true">999</prize>
注意: 合法的布爾值是 true、false、1(表示 true) 以及 0(表示 false)。
# 2  xs:hexBinary
    - base64Binary (Base64 編碼的二進制資料)
    - hexBinary (十六進制編碼的二進制資料)
<xs:element name="blobsrc" type="xs:hexBinary"/>
文檔中的元素看上去應該類似這樣:
<name>0x16</name>
# 3
下面是一個關于某個 scheme 中 anyURI 聲明的例子:
<xs:attribute name="src" type="xs:anyURI"/>
文檔中的元素看上去應該類似這樣:
<pic src="http://www.w3schools.com/images/smiley.gif" />      

最後:參考手冊​​https://www.runoob.com/schema/schema-elements-ref.html​​ 這些已久足夠了