zoukankan      html  css  js  c++  java
  • 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>

     2:通过文件路径引入

     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时,不必也不能添加前缀。(使用具体元素的时候)

      xmlns:xs="http://www.w3.org/2001/XMLSchema"  加上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"/> //use 是固定是否要写的属性

          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" 指示器(anyallchoicesequencegroup 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 指示器: 分为元素组 属性组  目的: 对某系元素 属性进行封装

    <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 这些已久足够了

  • 相关阅读:
    4.回归类算法目标值连续型
    springcloud笔记
    5.聚类算法kmeans
    FastdFS文件系统
    ElasticSearch
    cors解决跨域
    Swagger
    miaosha2:高并发抢购方案
    关于javascript中的constructor与prototype
    eclipse布署项目到weblogic时启动两次的问题
  • 原文地址:https://www.cnblogs.com/dgwblog/p/11842442.html
Copyright © 2011-2022 走看看