自定义schema文件。xml引入该规则。

来源:互联网 发布:php extjs 编辑:程序博客网 时间:2024/06/09 23:07

首先相关定义:
Order 指示器包含一下三种:
All :规定它里面元素出现的顺序可以是任意的,但是相同的元素只能1次;
Sequence(序列):表示它里面的元素必须以它们被声明的顺序出现,还有就是它默认minOccurs最少出现1次;
Choice:规定可出现某个子元素或者可出现另外一个子元素(非此即彼),多个元素中选择1个。
attribute():属性的定义,属性必须在sequence之后进行定义,在complexType属性里面定义。
标准参考w3c XSD 复合类型指示器XSD 属性

第1步:创建02.xsd文件,定义相应的规则:

<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema"     targetNamespace="http://www.example.org/02"    xmlns:tns="http://www.example.org/02"     elementFormDefault="qualified">    <!-- xsd里的元素用element标签 -->    <element name="books">            <!--complex 英[ˈkɒmpleks]adj.复杂的; 难懂的; 复合的 -->            <!--complexType复杂的类型 -->        <complexType>            <!--    occurs英 [ə'kɜ:z] v.被发现;发生 -->            <!-- minOccurs 最少出现1次,最多可以出现无限次 -->            <sequence minOccurs="1" maxOccurs="unbounded">                <element name="book" >                    <complexType>                        <sequence minOccurs="1" maxOccurs="unbounded">                            <element name ="title" type="string"/>                            <element name="content" type="string"/>                            <choice>                                <element name="author" type="string"/>                                <element name="authors" >                                    <complexType>                                        <sequence maxOccurs="unbounded">                                            <element name="author" type="string"/>                                        </sequence>                                    </complexType>                                </element>                            </choice>                        </sequence>                        <attribute name="id" type="int" use="required"></attribute>                     </complexType>                </element>            </sequence>        </complexType>    </element></schema>

示例的xsd文件规定了书books的规则:
一个books类型,是复杂类型,它里面的元素是有序的book,可以出现无限多次。
1、 每一本book书 里有复杂类型的有序的以下元素标签:
title(标题),content(内容),author(作者)或者是authors(一本书里可能有多个作者)
authors 里可以出现多个作者
2、 每一本book书 里必须有一个名字id的属性,类型为int类型。

这里02.xsd定义了规则,02.xml引用这个xsd文件后,在eclipse里如果xml编写没有按这个规定编写,会自动报错。
第二步 创建02.xml,引入02.xsd

<?xml version="1.0" encoding="UTF-8"?><book:books xmlns:book="http://www.example.org/02"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:noNamespaceSchemaLocation="02.xsd">    <book:book id="1" >        <book:title>平凡的世界</book:title>        <book:content>讲述了少安少平和润叶</book:content>        <book:author>路遥</book:author>    </book:book>    <book:book id="2">        <book:title>邓小平理论</book:title>        <book:content>讲述了中国如何走</book:content>        <book:authors>            <book:author>邓小平</book:author>            <book:author>李四</book:author>            <book:author>张三</book:author>            <book:author>还有其他很多人</book:author>        </book:authors>    </book:book></book:books>
0 0