Xml的schema约束

来源:互联网 发布:js radio 美化样式 编辑:程序博客网 时间:2024/06/05 05:52

schema开发过程

这里写图片描述

1. schema约束

dtd语法: <!ELEMENT 元素名称 约束>
** schema符合xml的语法,xml语句
** 一个xml中可以有多个schema,多个schema使用名称空间区分(类似于java包名)
** dtd里面有PCDATA类型,但是在schema里面可以支持更多的数据类型
* 比如 年龄 只能是整数,在schema可以直接定义一个整数类型
* schema语法更加复杂,schema目前不能替代dtd

2. schema的快速入门

这里写图片描述
* 创建一个schema文件 后缀名是 .xsd
** 根节点 <schema>
** 在schema文件里面
*属性
**xmlns=”http://www.w3.org/2001/XMLSchema”;
- 表示当前xml文件是一个约束文件
** targetNamespace=”http://www.itcast.cn/20151111”;
- 使用schema约束文件,直接通过这个地址引入约束文件
** elementFormDefault=”qualified”

步骤        (1)看xml中有多少个元素                           <element>         (2)看简单元素和复杂元素              * 如果复杂元素                          <complexType>                        <sequence>              子元素                        </sequence>                       </complexType>        (3)简单元素,写在复杂元素的                       <element name="person">                       <complexType>                        <sequence>                      <element name="name" type="string"></element>                      <element name="age" type="int"></element>                     </sequence>                    </complexType>                    </element>        (4)在被约束文件里面引入约束文件                      <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";                                    xmlns="http://www.itcast.cn/20151111";                                    xsi:schemaLocation="http://www.itcast.cn/20151111 1.xsd">         ** xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";           -- 表示xml是一个被约束文件        ** xmlns="http://www.itcast.cn/20151111";          -- 是约束文档里面 targetNamespace           ** xsi:schemaLocation="http://www.itcast.cn/20151111 1.xsd">         -- targetNamespace 空格  约束文档的地址路径
eg:     1.xsd<?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"><element name="person"><complexType><sequence><element name="name" type="String"></element><element name="age" type="int"></element></sequence></complexType></element></schema><?xml version="1.0" encoding="UTF-8"?><person xmlns:tns="http://www.w3.org/2001/XMLSchema-instance";xmlns="http://www.example.org/1";tns:schemaLocation="http://www.example.org/1 1.xsd"><name>zhangsan</name><age>20</age><a>111</a>//报错</person>

*<sequence>:表示元素的出现的顺序
<all>: 元素只能出现一次
<choice>:元素只能出现其中的一个
maxOccurs=”unbounded”: 表示元素的出现的次数//在type后面写
<any></any>:表示任意元素

*可以约束属性
* 写在复杂元素里面
*写在 </complexType>之前
--
<attribute name=”id1” type=”int” use=”required”></attribute>
- name: 属性名称
- type:属性类型 int stirng
- use:属性是否必须出现 required
这里写图片描述



*复杂的schema约束
<company xmlns = “http://www.example.org/company
xmlns:dept=”http://www.example.org/department
xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=”http://www.example.org/company
company.xsdhttp://www.example.org/department department.xsd
>

*引入多个schema文件,可以给每个起一个别名

<!– 部门名称 –>
<dept:name>100</dept:name>
* 想要引入部门的约束文件里面的name,使用部门的别名 detp:元素名称
<!– 员工名称 –>
<name>王晓晓</name>
</employee>

原创粉丝点击