Schema学习总结

XML
Schema是用于描述和规范XML文档的逻辑结构的一种语言,它最大的作用就是验证XML文件逻辑结构的正确性。可以理解成与DTD(文档类型定义)功能差不多,但是Schema在当前的WEB开发环境下优越很多。因为它本身就是一个有效的XML文档,因而可以更直观地了解XML的结构。除此之外,Schema支持命名空间,内置多种简单和复杂的数据类型,并支持自定义数据类型。由于存在这么多的优点,所以Schema渐渐成为XML应用的统一规范。

Schema是用于描述和规范XML文档的逻辑结构的一种语言,它最大的作用就是验证XML文件逻辑结构的正确性。可以理解成与DTD(文档类型定义)功能差不多,但是Schema在当前的WEB开发环境下优越很多。因为它本身就是一个有效的XML文档,因而可以更直观地了解XML的结构。除此之外,Schema支持命名空间,内置多种简单和复杂的数据类型,并支持自定义数据类型。由于存在这么多的优点,所以Schema渐渐成为XML应用的统一规范。

1、Schema的语法结构

下面是Schema的基础语法结构,通过下面来声明当前XML文档是一个Schema文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <!-- schema中子元素的定义-->
</xs:schema>

例如:一个关于书籍信息XML文档的Schema定义,如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <xs:element name="书本" type="书本类型"/>
    <xs:complexType name="书本类型">
        <xs:sequence>
            <xs:element name="名称" type="xs:int"/>
            <xs:element name="作者" type="xs:string"/>
            <xs:element name="出版日期" type="xs:date"
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Schema文件修饰的XML文件格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<书本 xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="D:\studen.xsd">
    <名称>《Java编程思想》</名称>
    <作者>Bruce Eckel</作者>
    <出版日期>2007-10-21</出版日期>
</书本>

2、XML Schema中的数据类型

(1)简单数据类型

a、Primitive原始数据类型

数据类型        描述

string          表示字符串

boolean         表示布尔值,真或假

decimal         表示十进制的数字

float           表示单精度32位浮点数

double          表示双精度64位浮点数

timeDuration    表示持续时间

dateTime        表示特定时间

time            表示特定时间,每天是重复的

date            表示日期

anyURI          表示一个URI,用来定位文件

recurring Duration 表示一个特定的间隔之后重现的持续时间

b、Derived派生数据类型

数据类型        描述

interger        用一个可以选择符号+或-表示的十进制数字的一个序列(由decimal导出)

long            表示在-263和+263-1之间的一个值(由Integer导出)

nonNegativeInteger 表示一个大于或等于0的整数(由Integer导出)

positiveInteger 表示一个大于0的整数(由nonNegativeInteger导出)

int             表示一个-231~230的数(有long导出)

time            表示每天事件重现的一个实例(由recurringDuration导出)

date            表示从特定一天午夜开始,在下一天午夜结束的一个时间段(由timeDuration导出)

c、simpleType元素的常用子元素

特性            描述

enumeration     指定数据集中选择,限定用户的选择

fractionDigits  最大小数位

length          数据长度

maxExclusive    指定数据的最大值(<)

maxInclusive    指定数据的最大值(<=)

maxLength       最大长度

minExclusive    指定数据的最小值(>)

minInclusive    指定数据的最小值(>=)

minLength       最小长度

Pattern         指定数据显示的规范

3、自定义简单数据类型

Schema支持用户自定义类型,这也是我们所需要的,由于业务的复杂性,这就要求有种灵活的方式去定义类型,能够满足业务的需要。自定义简单数据类型的语法如下:

<xs:simpleType name="自定义数据类型的名称">
<xs:restriction base="所基于的内置数据类型的名称">
自定义数据类型的内容模式
</xs:restriction>
</xs:simpleType>

例如:创建修饰学生信息XML文档的Schema文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <xs:element name="student" type="studentType"/>
    <xs:complexType name="studentType">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="sex" type="sexType"/>
            <xs:element name="age" type="ageType"/>
        </xs:sequence>
    </xs:complexType>
    <xs:simpleType name="ageType">
        <xs:restriction base="xs:int">
            <xs:maxExclusive value="150"/>
            <xs:minExclusive value="0"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="sexType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="男"/>
            <xs:enumeration value="女"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

对应的XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="D:\student.xsd">
    <name>zhangsan</name>
    <sex>男</sex>
    <age>11</age>
</student>

实例2、创建修饰学生信息XML文档的Schema文件,将所有的类型定义信息嵌套在一个类型里面,如果类型只被使用一次,这是可以的;如果需要反复使用,则类型不能够被复用,因此不推荐这么做。如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <xs:element name="student" type="studentType"/>
    <xs:complexType name="studentType">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="sex">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="男"/>
                        <xs:enumeration value="女"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            
            <xs:element name="age">
                <xs:simpleType>
                    <xs:restriction base="xs:int">
                        <xs:maxExclusive value="150"/>
                        <xs:minExclusive value="0"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

对应的XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="D:\studen.xsd">
    <name>zhangsan</name>
    <sex>男</sex>
    <age>11</age>
</student>

4、attributeGroup元素

attributeGroup元素的作用是将一组属性定义在一个范围下面,通过group名称直接访问即可,对于属性重用很方便。具体语法如下:

<attributeGroup name="group name">
属性1的声明
属性2的声明
...
</attributeGroup>

例如:通过定义学生信息的Schema,通过attributeGroup来定义学生的ID、QQ属性,如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <xs:element name="student" type="studentType"/>
    <xs:complexType name="studentType">
        <xs:group ref="studentGroup"/>
        <xs:attributeGroup ref="studentAttribute"/>
    </xs:complexType>
    <xs:group name="studentGroup">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="sex" type="xs:string"/>
            <xs:element name="age" type="xs:int"/>
        </xs:sequence>
    </xs:group>
    <xs:attributeGroup name="studentAttribute">
        <xs:attribute name="ID" type="xs:int" use="required"/>
        <xs:attribute name="QQ" type="xs:string"/>
    </xs:attributeGroup>
</xs:schema>

对应的XML文档如下:

<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="D:\studen.xsd" ID="1" QQ="984057999">
    <name>张三</name>
    <sex>男</sex>
    <age>31</age>
</student>

5、all元素

all元素定义的子元素可以按照任意顺序出现。如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <xs:element name="student" type="studentType"/>
    <xs:complexType name="studentType">
        <xs:all>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="sex" type="xs:string"/>
            <xs:element name="age" type="xs:int"/>
        </xs:all>
        <xs:attributeGroup ref="studentAttribute"/>
    </xs:complexType>
    <xs:attributeGroup name="studentAttribute">
        <xs:attribute name="ID" type="xs:int" use="required"/>
        <xs:attribute name="QQ" type="xs:string"/>
    </xs:attributeGroup>
</xs:schema>

对应的XML文档如下:

<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="D:\studen.xsd" ID="1" QQ="984057999">
    <name>张三</name>
    <age>31</age>
    <sex>男</sex>
</student>

6、sequence元素

sequence元素定义的子元素必须按照指定的顺序出现。如:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <xs:element name="student" type="studentType"/>
    <xs:complexType name="studentType">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="sex" type="xs:string"/>
            <xs:element name="age" type="xs:int"/>
        </xs:sequence>
        <xs:attributeGroup ref="studentAttribute"/>
    </xs:complexType>
    <xs:attributeGroup name="studentAttribute">
        <xs:attribute name="ID" type="xs:int" use="required"/>
        <xs:attribute name="QQ" type="xs:string"/>
    </xs:attributeGroup>
</xs:schema>

对应的XML文档如下:

<?xml version="1.0" encoding="UTF-8"?>
<student xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="D:\studen.xsd" ID="1" QQ="984057999">
    <name>张三</name>
    <sex>男</sex>
    <age>31</age>
</student>

7、choice元素

choice元素表示多个元素中选择一个(即单选,如:性别)。例如:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <xs:element name="name">
        <xs:complexType>
            <xs:choice>
                <xs:element name="one" type="xs:string"/>
                <xs:element name="two" type="xs:string"/>
                <xs:element name="three" type="xs:string"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>

对应的XML文档如下:

<?xml version="1.0" encoding="UTF-8"?>
<name xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="studen.xsd">
<two>two</two>
</name>

8、attribute元素

attribute元素定义指定元素的属性,具体格式如下:

<attribute name="属性名"
       default="默认值"
       fixed="固定值"
       type="数据类型名"
       use="optional | required">

例如:创建一个用户信息,包含id、age属性,其中id属性是必选的,如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema">
    <xs:element name="name">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="allName" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required"/>
            <xs:attribute name="age" type="xs:int"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

对应的XML文档如下:

<?xml version="1.0" encoding="UTF-8"?>
<name xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="D:\studen.xsd" id="s00001">
    <allName>李小明</allName>
</name>
不傲才以骄人,不以宠而作威。——诸葛亮
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号