为什么要使用命名空间

XML的元素名是不固定的,当量个不同的文档使用同样的名称描述两个不同类型的元素的时候,就会发生命名冲突。

<table>
    <tr>
        <td>apples</td>
    </tr>
</table>    
<table>
    <name>qidong</name>
</table>

这两个父标签虽然是table 但是内容根本不一样、不是同一类型。

使用前缀解决命名空间冲突问题

命名空间的语法定义

xmlns:[prefix]="URI" 元素和属性都可以应用命名空间

<h:table  xmlns:h="http://www.solo365.cn/xx/xx">

<h:tr></h:tr>

</h:table>

<f:table  xmlns:f="http://test.solo365.cn/xx/xx">

<f:name></f:name>

</f:table>

前缀是自己自定义,也可以把h换成hh随自己。

Schema的介绍

什么是Schema?

XML Schema是用一套预先规定的XML元素和属性创建的,这些元素和属性定义了XML文档的结构和内容模式。作用比DTD更强大的功能和更细粒度的数据类型,另外Schema还可以自己定义数据类型。

XML Schema规定XML文档示例的结构和每个元素/属性的数据类型.

为什么要用Schema?

DTD的局限性

  • DTD不遵守XML语法(写XML文档实例时候用一种语法,写DTD的时候用另外一种语法)
  • DTD数据类型有限(与数据库数据类型不一致)
  • DTD不可扩展
  • DTD不支持命名空间(命名冲突)

Schema的新特性

  • Schema基于XML语法
  • Schema可以用能处理XML文档的工具处理
  • Schema大大扩充了数据类型,可以自定义数据类型
  • Schema支持元素的继承
  • Schema指出属性组

Schema的文档结构

<?xml version="1.0"?>
    所有Schema文档使用schema作为跟元素

   用于构造schema的元素和数据类型来自http://www.w3.org/2001/XMLSchema命名空间

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://mynamespace/myschema">

    本schema定义的元素和数据类型属性属于http://mynamespace/myschem命名空间

    <!-- 放入实际内容 -->
</xs:schema>


targetNamespace是属于自己的命名空间、因为在schema里面是可以自己定义类型的。这个语句之后是可以随便定义的.

Schema的数据类型

内置的数据类型

  • 基本的数据类型
  • 扩展的数据类型

基本数据类型

数据类型描述
string表示字符串
boolean布尔型
decimal代表特定精度的数字
float表示单精度32位浮点数
double表示双精度64位浮点数
duration表示持续时间
dateTime代表特定的时间
time代表特定的时间,但是是每天重复的
date代表日期
hexBinary代表十六进制数
anyURI代表一个URI,用来定位文件
NOTATION代表 NOTATION类型
ID用于唯一标识元素
IDREF参考ID类型的元素或属性
ENTITY实体类型
NMTOKENNMTOKEN类型
NMTOKENSNMTOKEN类型集
long表示整型数,大小介于-9223372036854775808 和9223372036854775807之间
int表示整型数,大小介于-2147483648和 2147483647之 间
short表示整型数,大小介于-32768和32767之间
byte表示整型数,大小介于-128和127之间

Schema数据类型的特性

特性描述
enumeration在指定的数据集中选择,限定用户的选值
fractionDigits限定最大的小数位,用于控制精度
length指定数据的长度
maxExclusive指定数据的最大值(小于)
maxInclusive指定数据的最大值(小于等于)
maxLength指定长度的最大值
minExclusive指定最小值(大于)
minInclusive指定最小值(大于等于)
minLength指定最小长度
Pattern指定数据的显示规范

定义一个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="书本" type="书本类型"/>
    <xs:complexType name="书本类型">
        <xs:sequence>
            <xs:element name="作者" type="xs:string"/>
            <xs:element name="名称" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

完成XML验证的代码

<?xml version="1.0" encoding="UTF-8"?>
<书本 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Users/a364482611/Desktop/xmlexamples/schema1.xsd">
    <作者>启东</作者>
    <名称>阿萨德</名称>
</书本>

schema中的Element元素

作用:声明一个元素

<xs:element name=”cat” type=”xs:string”/>

意思:我声明个一元素叫cat 类型是string类型,空间是xs。

元素的属性

  • name
  • type
  • ref 引用
  • minOccurs 最小出现次数
  • maxOccurs 最大出现次数 maxOccurs="unbounded" 为不限制最大次数
  • substitutionGroup
  • fixed
  • default 解读上一个schema代码示例

complexType元素

sequence:序列

作用:定义一个复合类型,它决定了一组元素和属性值的约束和相关信息

属性:name

<xs:element name="书本" type="书本类型"/>
<xs:complexType name="书本类型">
      <xs:sequence>
         <xs:element name="作者" type="xs:string"/>
         <xs:element name="名称" type="xs:string"/>
      </xs:sequence>
</xs:complexType>

意思:自定义一个类型,名称为书本类型,这个类型是个序列、里面包含两个元素、作者和名称。也等同于下面

<xs:element name="书本" >
<xs:complexType >
    <xs:sequence>
    <xs:element name="作者" type="xs:string"/>
    <xs:element name="名称" type="xs:string"/>
    </xs:sequence>
</xs:complexType>
</xs:element>

因为、定义的书本元素里面包含了类型、所以书本元素就不用声明类型、也正是这样complexType不用声明name属性。此代码示例的效果等同于上一个效果。

<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="作者" type="xs:string">
<xs:element name="名称" type="xs:string">
</xs:sequence>

以书本的XML代码示例,如果sequence 出现了这两属性,那么在xml代码中 必须出现1次被包含序列的元素。

group元素

作用:把一组元素声明组合在一起,以便他们能够一起被复合类型应用

属性:name/ref

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:group name="myGroup">
      <xs:sequence>
          <xs:element name="name" type="xs:string"/>
          <xs:element name="birthday" type="xs:date"/>
          <xs:element name="age" type="xs:int"/>
      </xs:sequence>
  </xs:group>
    <xs:element name="person">
        <xs:complexType>
            <xs:group ref="myGroup"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

定义分组、起到分门别类的左右,这三个元素是一个人的基本属性、可以同时出现。也可以分组属性。这个例子和上个例子意思基本一样。xml实现代码

<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="schema1.xsd">
     <name>东哥</name>
     <birthday>1990-10-01</birthday>
    <age>28</age>
</person>

attribute元素

作用:声明一个属性

属性:name/type/ref/use

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:attribute name="interest" type="xs:int"/>
    <xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="hello" type="xs:string"/>
                <xs:element name="word" type="xs:string"/>
            </xs:sequence>
            <xs:attribute ref="interest" use="optional"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

use相当与DTD的特点,分别为optional(可选的,如果不写use默认可选),prohibited(禁止使用的),required(必须使用)

<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="schema1.xsd">
    <hello>sss</hello>
    <word>www</word>
</person>

attributeGroup元素

作用:把一组属性声明组合在一起,以边可以被复合类型应用

属性:name/ref

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:attributeGroup name="myAtrributeG">
        <xs:attribute name="hello" type="xs:string" use="required"/>
        <xs:attribute name="word"  type="xs:string" use="optional"/>
    </xs:attributeGroup>
    <xs:element name="myElement">
        <xs:complexType>
            <xs:attributeGroup ref="myAtrributeG" />
        </xs:complexType>
    </xs:element>
</xs:schema>

跟group元素一样的意思、但这个schema在根元素里面,没有定义元素,所以在跟元素里面不应该包含空格 。

<?xml version="1.0" encoding="UTF-8"?>
<myElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="schema1.xsd" hello="asd" />

或者

<?xml version="1.0" encoding="UTF-8"?>
<myElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="schema1.xsd" hello="asd" word="d"></myElement>

simpleType元素

作用:定义一个简单类型,它决定了元素和属性的约束和相关信息

属性:name

内容:应用已经存在的简单类型,三种方式: 1. restrict 限定一个范围,2. list 从列表中选择 。 3. union 包含一个值的结合

restrict

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="myType">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="0"/>
            <xs:maxInclusive value="100"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="hello" type="myType"></xs:element>
</xs:schema>

这个简单类型说明了 我的类型名称叫myType,这个是基于整数,范围是0-100。然后被定义的hello元素所引用,所以hello的类型为整数0-100。

<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="schema1.xsd"  >100</hello>

上面的特性表中有很多介绍如果 xs:minExclusive value=”0″/> 那么在hello元素就必须大于0了。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="myType">
        <xs:restriction base="xs:integer">
            <xs:enumeration value="5"/>
            <xs:enumeration value="7"/>
            <xs:enumeration value="9"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="hello" type="myType"></xs:element>
</xs:schema>

这样是枚举类型、那么在hello标签里面只能是5,7,9其中一个了。

list

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

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="myType">
        <xs:list itemType="xs:integer"/>
    </xs:simpleType>
    <xs:element name="hello" type="myType"></xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="schema1.xsd"  >5 2 3 99</hello>

上面的定义的list在下面实现上,可以定义的多个数字。意思就是只要是数字定义多少没关系。

union

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

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:attribute name="allFrameSize">
        <xs:simpleType>
            <xs:union memberTypes="roadbikeSize mountainbikeSize"/>
        </xs:simpleType>
    </xs:attribute>

    <xs:simpleType name="roadbikeSize">
        <xs:restriction base="xs:positiveInteger">
            <xs:enumeration value="44"/>
            <xs:enumeration value="48"/>
            <xs:enumeration value="50"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="mountainbikeSize">
        <xs:restriction base="xs:string">
            <xs:enumeration value="small"/>
            <xs:enumeration value="medium"/>
            <xs:enumeration value="large"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="hello">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="welcome" type="xs:string"/>
            </xs:sequence>
            <xs:attribute ref="allFranmeSize" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

定义个属性allFrameSize 是个简单类型simpType,里面是个两个集合

<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="schema1.xsd" allFranmeSize="small" >
    <welcome>asd</welcome>
</hello>

complexType与simpleType区别


simpleType类型的元素中不能包含元素或者属性。

当需要声明一个元素的子元素和/或属性时,complexType;

当需要基于内置的基本数据类型定义一个新的数据类型时,用simpleType

最后修改:2020 年 01 月 07 日
如果觉得我的文章对你有用,请随意赞赏