• 5.xml约束技术--------schema


    1.schema约束

      (1)dtd语法:<!ELEMENT 元素名称 约束>

      (2)schema符合xml的语法,xml语句

      (3)一个xml文件中只能有一个dtd,但是可以有多个schema,多个schema是使用名称空间区分(相当于java中的包名)

      (4)dtd里面有PCDATA类型,但是在schema里面可以支持更多的数据类型

          比如 年龄 只能是整数,在schema可以直接定义一个整数

      (5)schema语法更加复杂,所以schema目前不能代替dtd

    2.schema快速入门

      (1)创建一个schema文件  后缀名是 .xsd

        注意:根节点是<schema>,并且schema文件也是一个xml文件

      (2)在schema文件中定义了一些属性(如果用Eclipse工具会自动帮你写上)

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/1" 
    xmlns:tns="http://www.example.org/1" 
    elementFormDefault="qualified">
    </schema>

        xmlns="http://www.w3.org/2001/XMLSchema"

          - 表示当前xml文件是一个schema约束文件(这个属性的值是确定的,不能改变,这个就是一个标志)

        targetNamespace="http://www.example.org/1"

          - 定义schema地址,被约束文件可以通过这个地址引入使用

          - 格式:一般是使用域名加时间(http://www.xxx.cn/20181111),这样是为了防止地址名字一样

        xmlns:tns="http://www.example.org/1"

          - 这个删除

        elementFormDefault="qualified"

      (3)定义schema文件

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/1" 
    elementFormDefault="qualified">
        <element name="persion">
            <complexType>
                <sequence>
                    <element name="name" type="string"></element>
                    <element name="age" type="int"></element>
                </sequence>
            </complexType>
        </element>
    </schema>

         - 复杂元素:

          <element name="persion">
              <complexType>
                 <sequence>     
                 </sequence>
              </complexType>
           </element> 

         - 简单元素

          <element name="name" type="string"></element>
           <element name="age" type="int"></element>

         - 解读

          <sequence> 表示按顺序的(也就是xml文件中元素定义,只能按照这个顺序定义,否则报错)

          name="age" 表示xml元素的标签名为age

          type="string" 表示数据类型

    3.xml文件引入schema文件

    <?xml version="1.0" encoding="UTF-8"?>
    <persion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.example.org/1"
    xsi:schemaLocation="http://www.example.org/1 1.xsd"
    >
        <name></name>
        <age></age>
    </persion>

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        - 表示这是一个被约束的xml文件(标志)

        - :xsi  这个是别名,为了区分下面的xmlsn属性

      xmlns="http://www.example.org/1"

        - 这个就是引入schema文件,这个值就是schema的地址(包名)

      xsi:schemaLocation="http://www.example.org/1 1.xsd"

        - schema地址 空格 schema路径

    4. 元素约束(复杂元素的指示器)   

    <sequence> : 表示元素的出现顺序

    <any> : 表示任意元素

    <choice> : 元素只能出现其中的一个

    maxOccurs="unbounded" : 表示元素的出现次数(unbounded表示无限)

    <element name="age" type="string" maxOccurs="3"></element>

    <all> : 元素只能出现一次

    <element name="persion">
            <complexType>
                <all>
                    <element name="name" type="string"></element>
                    <element name="age" type="string"></element>
                </all>
            </complexType>
        </element>

    5.约束元素属性

      <attribute> : 这个可以定义属性的约束,但是只能定义复杂元素

        - 写在<complexType>标签里面

    <element name="persion">
            <complexType>
                <sequence>
                    <element name="name" type="string"></element>
                    <element name="age" type="string"></element>
                </sequence>
                <attribute name="id" type="int" use="required"></attribute>
            </complexType>
        </element>

      解读:<attribute name="id" type="int" use="required"></attribute>

      name="id" : 属性名字

      type="int": 属性值的数据类型

      use="required" 是否一定要定义这个属性

         - required  表示一定

         - Prohibited 特性无法使用

         - Optional 特性是可选的并且可以具有任何值。 这是默认值

    6.使用2个schema文件约束

    1.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/one" 
    elementFormDefault="qualified">
        <element name="persion">
            <complexType>
                <sequence>
                    <element name="name" type="string"></element>
                    <element name="age" type="string"></element>
                </sequence>
            </complexType>
        </element>
    </schema>

    2.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/two" 
    elementFormDefault="qualified">
        <element name="name" type="int"></element>
    </schema>

    xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <persion xmlns="http://www.example.org/one"
    xmlns:two="http://www.example.org/two"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/one one.xsd http://www.example.org/two two.xsd">
    
        <two:name>12</two:name>
        <name>张三</name>
        <age>180</age>
    </persion>
  • 相关阅读:
    HOG特征提取+python+opencv
    统计模型计算量~pytorch
    CycleGAN训练~训练图像进行拼接
    缺陷检测~分类网络
    缺陷检测~分类网络
    缺陷检测~检测网络
    pytorch中的fc和fc逆操作
    pytorch中的view和view逆操作
    传统的图像特征提取
    TensorFlow安装+入门操作
  • 原文地址:https://www.cnblogs.com/zjdbk/p/9166622.html
Copyright © 2020-2023  润新知