与DTD(http://code727.iteye.com/blog/1882877)相比较,Schema是另一种灵活但复杂的XML约束方式。它主要增加了更多的数据类型和结构模式来改进DTD,并使用XML作为约束格式描述。
1.定义Schema约束规则
<?xml version="1.0" encoding="UTF-8"?> <templet> <bean name="user" class="com.daniele.appdemo.test.domain.User"> <property name="id" type="java:long"/> <property name="name" type="java:string" /> <property name="age" type="java:integer"/> <property name="mail" type="java:string" /> <property name="company" ref="company" /> </bean> <bean name="company" class="com.daniele.appdemo.test.domain.Company"></bean> <description date="2013-06-07">This is a XML Schema example.</description> <author name="daniele" /> <end/> </templet>文档约束规则:
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.appdemo.daniele.com/schema/templet" targetNamespace="http://www.appdemo.daniele.com/schema/templet"> <!-- 定义XML根元素temple,其内有0个或多个bean元素 --> <xsd:element name="templet"> <!-- complexType元素表示templet是一个复合类型, 属性为false,表示它只能包含元素。 否则,为true(默认)表示同时还可包含文本。 --> <xsd:complexType mixed="false"> <!-- 定义子元素组必须按照其sequence内部定义时的顺序出现一次(默认为有且只出现一次) 次数可通过minOccurs和maxOccurs这两个属性来控制。 --> <xsd:sequence> <!-- 通过ref属性值,引用其他元素作为当前元素的子元素。 如果在schema中定义了目标名称空间,则ref值为"名称空间前缀:子元素名", 否则直接关联子元素名(对应另一个element元素的name属性值)即可。 --> <xsd:element ref="tns:bean" minOccurs="0" maxOccurs="unbounded"/> <xsd:element ref="tns:description" minOccurs="0" maxOccurs="1"/> <xsd:element ref="tns:author" minOccurs="0" maxOccurs="1"/> <xsd:element ref="tns:end" minOccurs="0" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="bean"> <xsd:complexType> <!-- 按顺序出现0次(minOccurs="0")或多次(maxOccurs="unbounded")其内部引用的元素 --> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:element ref="tns:property" /> </xsd:sequence> <!-- 定义当前元素具备的属性。 1)name:属性名 2)type:属性值类型 3)use:可选性。 required:必须,optional:可选(默认),prohibited:禁用 当为可选时,可以不用设置该属性。 --> <xsd:attribute name="class" type="xsd:string" use="required" /> <xsd:attribute name="name" type="xsd:string" /> </xsd:complexType> </xsd:element> <xsd:element name="property"> <xsd:complexType mixed="false"> <xsd:attribute name="name" type="xsd:string" use="required" /> <!-- 如果某个属性值具有备选值,则利用<xsd:simpleType> 和<xsd:restriction>这两个元素来替代type属性 --> <xsd:attribute name="type" default="java:string"> <!-- 表示当前属性的值为简单类型 --> <xsd:simpleType> <!--表示当前属性备选值限定的类型 --> <xsd:restriction base="xsd:string"> <!-- 备选值定义 --> <xsd:enumeration value="java:boolean" /> <xsd:enumeration value="java:byte " /> <xsd:enumeration value="java:char" /> <xsd:enumeration value="java:double" /> <xsd:enumeration value="java:float" /> <xsd:enumeration value="java:long" /> <xsd:enumeration value="java:integer" /> <xsd:enumeration value="java:short" /> <xsd:enumeration value="java:string" /> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="ref" type="xsd:string" /> </xsd:complexType> </xsd:element> <!-- description元素只包含文本和属性date --> <xsd:element name="description"> <xsd:complexType> <xsd:simpleContent> <!-- 文本类型为任意字符 --> <xsd:extension base="xsd:string"> <!-- 定义description元素的可选的date属性,其值必须符合日期时间类型 如果不在extension中定义任何attribute,则表示当前元素只能包含文本内容 --> <xsd:attribute name="date" type="xsd:date"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> <!-- 定义只有一个必选name属性的author元素,不能有子元素和文本内容 --> <xsd:element name="author"> <xsd:complexType mixed="true"> <xsd:attribute name="name" use="required"/> </xsd:complexType> </xsd:element> <!-- 定义为空的end元素,即不能任何有子元素、文本内容和属性 --> <xsd:element name="end"> <xsd:complexType /> </xsd:element> </xsd:schema>
<?xml version="1.0" encoding="UTF-8"?> <!-- 无命名空间引入 --> <!-- <templet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schame_test.xsd"> --> <!-- 命名空间引入 --> <templet xmlns="http://www.appdemo.daniele.com/schema/templet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.appdemo.daniele.com/schema/templet schame_test.xsd"> <bean name="user" class="com.daniele.appdemo.test.domain.User"> <property name="id" type="java:long"/> <property name="name" type="java:string" /> <property name="age" type="java:integer"/> <property name="mail" type="java:string" /> <property name="company" ref="company" /> </bean> <bean name="company" class="com.daniele.appdemo.test.domain.Company"></bean> <description date="2013-06-07">This is a XML Schema example.</description> <author name="daniele" /> <end/> </templet>
1万+



被折叠的 条评论
为什么被折叠?



