schema详解
来源:互联网 发布:单片机gpio是什么意思 编辑:程序博客网 时间:2024/04/30 09:15
一、介绍
XML Schema是W3C制定的基于XML格式的XML文档结构描述标准。作为一种文档描述语言,通常我们将其简写为XSD(XML Schema Define)。XSD作为DTD(文档类型定义)的替代者,已经广泛地应用到各种商业应用。使用XSD,我们不仅可以描述XML文档的结构以便颁布业务标准,而且可以使用支持XSD的通用化XML解析器对XML文档进行解析并自动地检查其是否满足给定的业务标准。应用XSD校验XML文档的结构后,我们不仅验证了XML文档的有效性(Well-Formed Document),还验证了XML文档的合法性,甚至验证了XML文档各域的值合法性(数据类型与编码值),而且这些验证工作不必我们编写任何代码,只需使用支持XSD的通用化XML文档解析器即可完成。这就给应用软件带来了巨大的灵活性,以前需要借助数据库或配置文件才能完成的参数化管理,现在只需按照新的业务需求发布新的XML Schema即可。
二、简单案例
1. 一个简单的schema文件以及与其对应的有效的XML文档:
schema文件:
<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xxz.org/user"xmlns:tns="http://www.xxz.org/user" elementFormDefault="qualified"><element name="user"><complexType><sequence><element name="id" type="int"/><element name="username" type="string"/><element name="birthday" type="date"/></sequence></complexType></element></schema>
<?xml version="1.0" encoding="UTF-8"?><user xmlns="http://www.xxz.org/user" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxz.org/user"><id>1</id><username>zhangsan</username><birthday>1990-12-12</birthday></user>
三、详细分析
1. schema文件的结构:
2. 在xml文件中如何引入schema文件:
第一种方式:
在eclipse中如何增加xml的category:
第二种方式:
四、属性的分析
1. 我们再写一个books的schema如下:
<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.npf.org/books"xmlns:tns="http://www.npf.org/books" elementFormDefault="qualified"><element name="books"><complexType><sequence minOccurs="1" maxOccurs="unbounded"><element name="book"><complexType><sequence ><element name="title" type="string"/><element name="content" type="string"/><choice><element name="author" type="string"/><element name="authors"><complexType><sequence minOccurs="1" maxOccurs="unbounded"><element name="author" type="string"/></sequence></complexType></element></choice></sequence></complexType></element></sequence></complexType></element></schema>
说明如下:
(1). minOccurs="1" maxOccurs="unbounded", 说明这个sequence最少出现1次,最多出现的次数无限制。
(2). 一本书的author可能有多个,那么我们就使用了choice标签,它表示使用里面的其中一个。
(3). 我们使用的sequence是有顺序的。必须严格按照定义的顺序出现在xml文件中。
(4). 如果我们不想使用有顺序的,那么使用all标签来代替sequence标签。但是使用all标签的话,就限定了每个子元素都只能出现一次。
2. 我们写一个books的xml如下:
2. 我们写一个books的xml如下:
<?xml version="1.0" encoding="UTF-8"?><books xmlns="http://www.npf.org/books" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="books.xsd"><book><title>Java in action</title><content>very good</content><author>npf</author></book><book><title>Spring in action</title><content>very good</content><authors><author>xxz</author><author>npf</author></authors></book></books>
3. 我们写一个带有属性的schema如下:
4. 我们写一个带有属性的xml如下:
<?xml version="1.0" encoding="UTF-8"?><books xmlns="http://www.npf.org/books" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="books.xsd"><book id="1"><title>Java in action</title><content>very good</content><author>npf</author></book><book id="2"><title>Spring in action</title><content>very good</content><authors><author>xxz</author><author>npf</author></authors></book></books>
0 0
- 详解Schema
- schema详解
- XML详解----Schema
- XML详解----Schema
- XML详解----Schema
- LDAP定义schema详解
- XML详解----Schema
- Schema技术详解
- Schema中elementFormDefault详解
- [转]XML详解--Schema
- openldap之schema详解
- xml schema详解
- Mondrian Schema详解
- xml--schema详解
- Schema例子详解
- XML详解----Schema
- XML详解----Schema
- Mondrian Schema详解
- OkHttp使用详解
- Eclipse中.setting目录下文件介绍
- IBM x3850 x5U盘启动或光驱启动不起作用
- 用对话框作为主窗口;IDD_FORMVIEW资源文件;
- linux kernel 网络协议栈
- schema详解
- 用回溯法求解打靶问题
- 2946: [Poi2000]公共串 (hash+二分)
- WebView简单使用(一)
- 函数【4-1-1】
- 工作中遇到的问题--validator与datepicker不兼容
- java web 作用域 笔记
- Centos7安装配置JDK8
- ECMAScript6(ES6)标准之解构赋值语法及应用