zoukankan      html  css  js  c++  java
  • XML Schema验证

    一、什么事Schema(XSD)

      XML Schema是微软定义的一套用来验证XML技术。是一套预先规定的XML元素和属性创建的,这些元素和属性定义了XML文档的结构和内容模式。

      DTD的局限性:

      1、DTD不遵循XML语法。

      2、DTD的数据类型有限,与数据库类型不一致。

      3、DTD不可以扩展。

      4、DTD是不支持命名空间的。

      Schema的优势:

      1、Schema是一种XML语法结构,编写更加方便。

      2、Schema可以支持数据类型。

      3、Schema是可以扩展的。

      4、Schema支持命名空间。

    二、Schema文档结构

      Schema文档本身是一个XML文档,所以必须满足XML文档结构。在每个Schema文档中必须包含一个根元素<xs:schema>。  

    <?xml version="1.0"?>
     <!--每个Schema中都必须包含xs:schema根节点,然后在根节点中定义内容!-->
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3school.com.cn"
    xmlns="http://www.w3school.com.cn"
    elementFormDefault="qualified">
        定义内容
    </xs:schema>

      说明:

      xmlns:xs="http://www.w3.org/2001/XMLSchema"显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:

      targetNamespace="http://mynamespace/myschema" 显示被此 schema 定义的元素验证的XML来自的命名空间。

      xmlns="http://www.w3school.com.cn" :指定默认的命名空间是 。

      elementFormDefault="qualified" :指目标XML是否遵循本Schema,qualified表示遵循,unqualified表示不遵循。

      

    三、在XML中引用Schema文档  

    <?xml version="1.0"?>
    <!--引用Schema文档-->
    <note xmlns="http://www.w3school.com.cn"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3school.com.cn note.xsd">
    <to>George</to>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
    </note>

      说明:

      xmlns="http://www.w3school.com.cn" :规定了默认命名空间的声明。此声明会告知 schema 验证器,在此 XML 文档中使用的所有元素都被声明于 "http://www.w3school.com.cn" 这个命名空间。

      xsi:schemaLocation="http://www.w3school.com.cn note.xsd":指定文件路径。

    四、Schema数据类型:

      Schema中支持丰富的数据类型,可以简单分为简单类型和复合类型。

      1、简单类型包括:

        1)内置的数据类型。

          a、基本的数据类型

    基本的数据类型
    数据类型 描述
    string 字符串
    boolean  布尔类型 
    decimal  特定精度的数字 
    float  单精度浮点数 
    double  双精度浮点数 
    duration  表示持续时间/日期格式 
    dateTime  完整日期格式
    time 代表时分秒 
    date  代表日期 

          b、扩展的数据类型     

    扩展的数据类型
    数据类型 描述
    ID 用于唯一表示元素
    IDREF 应用ID元素的属性或属性
    ENTITY
    实体类型
    long
    表示长整型:-9223372036854775808~9223372036854775807
    int 表示整型:-2147483648~--2147483647
    short 表示短整型:-32768~32767
    byte
    整型:-128~127

        2)用户自定义的简单类型(通过simpleType定义)

      2、复合类型(通过complexType定义)

      

    五、Schema中的元素类型

      1、根元素:schema。

      包含已经定义的schema。

      属性:

      xmlns:schema的命名空间。

      targetNamespace:要验证的XML文件的命名空间。

      elementFormDefault:要验证的XML是否遵循当前的验证命名空间。

     

      2、用于定义元素和属性的元素:element、attribute、group、attributeGroup。

      1)element:声明一个元素

      属性:

      name:需要限定XML文档元素的名称。

      type:需要限定XML文档元素的类型。

      ref:引用外部定义的元素

      minOccurs:元素最小出现的次数。

      maxOccurs:元素最大出现的次数。

      例:声明student节点,类型为string类型,最小出现1次,最多出现3次。

      <xs:element name="student" type="xs:string" minOccurs="1" maxOccurs="unbounded" />

       

      2)group:声明一个分组

      将多个元素声明放到一个分组中,然后在其他元素中通过group引用。

      属性:name分组的名称

      例:将name和age定义为一个分组,然后在student中引用这个分组。

    <!--外部定义一个标记-->
        <xs:element name="student">
            <xs:complexType>
                <!--引用分组标记-->
                <xs:group ref="stuinfo" maxOccurs="unbounded"></xs:group>
            </xs:complexType>
        </xs:element>
        
        <!--定义一个分组标记-->
        <xs:group name="stuinfo">
            <xs:sequence>
                <!--自定义的子元素:name和age,name类型为string,age类型为byte,只能出现1次-->
                <xs:element name="name" type="xs:string"></xs:element>
                <xs:element name="age" type="xs:byte"></xs:element>
            </xs:sequence>
        </xs:group>

      3)attribute元素:

      用于声明一个属性:

      属性:

      name:属性名称

      type:属性类型

      user:是否必选,required必选,optional可选,默认可选

      default:默认值

      fixed:固定值

      注意:默认值和固定值不能同时出现。

      示例:属性的用法

      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="students">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="student" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:attribute name="name" type="xs:string" />
                            <xs:attribute name="age" type="xs:byte" />
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>

      XML文档:

    <?xml version="1.0" encoding="UTF-8"?>
    <students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="students.xsd">
        <student name="zhangsan" age="29"/>
        <student name="lisi" age="19"/>
    </students>

       4)attributeGroup属性组

      和前面的元素组类型,在外部定义属性组,然后在元素中引用属性组。

      示例:

      Schema文档:

    <!--定义属性组:stuattgroup-->
    <xs:attributeGroup name="stuattgroup">
        <xs:attribute name="name" type="xs:string"/>
        <xs:attribute name="age" type="xs:byte"/>
    </xs:attributeGroup>
    
    <!--引用属性组-->
    <xs:element name="student" maxOccurs="unbounded">
        <xs:complexType>
            <xs:attributeGroup ref="stuattgroup"></xs:attributeGroup>
        </xs:complexType>
    </xs:element>    

      

      3、用于定义简单类型:simpleType。

      它决定了元素和属性值得约束和相关信息。

      属性:name

      常用的两种方式:

      1)restriction:约束

      对现有类型进行扩充

      示例:设置年龄必须在18-100之间

    <xs:attributeGroup name="stuattgroup">
        <xs:attribute name="name" type="xs:string"/>
        <!--将原来的xs:int换位age类型-->
        <xs:attribute name="age" type="age"/>
    </xs:attributeGroup>
    <!--扩展int类型,设置int取值只能是18到100,包含18和100-->
    <xs:simpleType name="age">
        <xs:restriction base="xs:int">
            <xs:minInclusive value="18" />
            <xs:maxInclusive value="100" />
        </xs:restriction>
    </xs:simpleType>

      2)list:列表

      从一个特定的数据类型的集合中选择定义一个简单类型元素。

      Schema文档:

    <!--Schema文档-->
    <!--定义列表数据,其每项子元素为自定义扩展类型-->
    <xs:simpleType name="stuScore">
        <xs:list itemType="stuScoreItem"></xs:list>
    </xs:simpleType>
    <!--定义扩展类型限定0-100之间-->
    <xs:simpleType name="stuScoreItem">
        <xs:restriction base="xs:int">
            <xs:minInclusive value="0" />
            <xs:maxInclusive value="100" />
        </xs:restriction>
    </xs:simpleType>
    <!--声明Scores节点,包含list格式数据-->
    <xs:element name="scores" type="stuScore">        
    </xs:element>
    
    <!--XML文档中的数据-->
    <scores xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="scores.xsd">
    10 20 300
    </scores>

     

      4、用于定义复杂类型:complexType。

      需要使用在节点下包含子节点的情况。

      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="students">
            <xs:complexType>
                <xs:sequence>
                    <!--通过ref来应用student标记 maxbounded表示不限定出现的次数-->
                    <xs:element ref="student" maxOccurs="unbounded"></xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        
        <!--外部定义一个标记-->
        <xs:element name="student">
            <xs:complexType>
                <xs:sequence>
                    <!--自定义的子元素:name和age,name类型为string,age类型为byte,只能出现1次-->
                    <xs:element name="name" type="xs:string"></xs:element>
                    <xs:element name="age" type="xs:byte"></xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>

      XML文档:

    <?xml version="1.0" encoding="UTF-8"?>
    <students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="students.xsd">
        <student>
            <name>张三</name>
            <age>20</age>
        </student>
        <student>
            <name>李四</name>
            <age>20</age>
        </student>
    </students>

       

      5、用于进行约束:choice、list、sequence、restriction。

      1)choice

      把一组属性声明组合到一起,一边被复合类型所应用,XML中只能出现限定选项中的一个元素。

      例:

      Schema文档:

    <!--设定交通工具只能是自行车,小汽车,摩托车中的一项-->
    <xs:element name="交通工具">
        <xs:complexType>
            <xs:choice>
                <xs:element name="自行车" type="xs:string"></xs:element>
                <xs:element name="小汽车" type="xs:string"></xs:element>
                <xs:element name="摩托车" type="xs:string"></xs:element>
            </xs:choice>
        </xs:complexType>
    </xs:element>

      XML文档:

    <交通工具 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vehicle.xsd">
        <自行车></自行车>
        <!--
        <小汽车></小汽车>
        <摩托车></摩托车>
        包含任意一个,包含多个会报错!
        -->
    </交通工具>   

      

      2)sequence

      表示元素必须按照规定的序列进行显示。

     

     

     

      

  • 相关阅读:
    2017-2018-1 20155306 《信息安全系统设计基础》第11周学习总结
    2017-2018-1 20155306 实验三 实时系统
    2017-2018-1 20155306 《信息安全系统设计基础》第9周学习总结
    2017-2018-1 20155306 mypwd的实现
    2017-2018-1 20155306 《信息安全系统设计基础》第8周课上测试+课下实践
    2017-2018-1 20155306 《信息安全系统设计基础》第8周学习总结
    2017-2018-1 20155306 《信息安全系统设计基础》第7周学习总结
    20155212——man指令的使用及mypwd的实现
    2017-2018-1 20155213 《信息安全系统设计基础》第七周学习总结
    2017-2018-1 20155213 20155303 实验二 固件程序设计
  • 原文地址:https://www.cnblogs.com/zhengcheng/p/4279092.html
Copyright © 2011-2022 走看看