一、schema文件编写方式 ①Russian Doll(俄罗斯套娃) ②Salami Slice(香肠切片) ③Venetian Blind(百叶窗) 推荐
二、Russian Doll俄罗斯套娃 顾名思义,编写方式是一层套一层,只有一个根元素,通过且套的方式编写完成。 优点:结构清晰 缺点:元素无法重用
RussionDoll.xsd <?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xy.com" xmlns:tns="http://www.xy.com" elementFormDefault="qualified"> <element name="books"> <complexType> <!-- maxOccurs表示最大出现次数,unbounded表示次数无限制。默认次数为1次 --> <sequence maxOccurs="unbounded"> <element name="book"> <sequence minOccurs="1" maxOccurs="unbounded"> <element name="title" type="string" /> <element name="content" type="string" /> <!-- choice标签中属性任选一个 --> <choice> <element name="author" type="string" /> <element name="authors"> <!-- all标签中的元素可以按顺序出现,但次数为1次 --> <all> <!-- 每个元素只能出现一次 --> </all> </complexType> </element> </choice> </sequence> <!-- book的属性,在schema中位置必须在sequence之后 --> <attribute name="id" type="int" use="required" /> </schema> RussionDoll.xml <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>Java in action</book:title> <book:content>Java is good</book:content> <book:author>TOM</book:author> </book:book> <book:book id="2"> <book:title>SOA in action</book:title> <book:content>SOA is difficult</book:content> <book:authors> <book:author>LILY</book:author> </book:authors> </book:books> 二、Salami Slice腊肠切片 优点:可以进行最大化的重用 缺点:根元素不清晰。book、id、title、content所有的element都可以作为根元素 SalamiSlice.xsd targetNamespace="http://www.xy,com" <element name="book" type="tns:bookType"></element> <element name="id" type="int" />
<complexType name="bookType"> <sequence> <element ref="tns:id" /> <element ref="tns:title" /> <element ref="tns:content" /> 三、百叶窗Venetian Blind 根元素清晰,元素可重用 VenetianBlind.xsd <element name="person" type="tns:personType"/> <complexType name="personType"> <element name="name" type="string"/> <element name="age" type="tns:ageType"/> <element name="email" type="tns:emailType"/> <attribute name="sex" type="tns:sexType"/> <simpleType name="emailType"> <restriction base="string"> <pattern value="(w+.*)*w+@w+.[A-Za-z]{2,6}"/> <minLength value="6"/> <maxLength value="255"/> </restriction> </simpleType> <simpleType name="ageType"> <restriction base="int"> <minInclusive value="1"/> <maxExclusive value="150"/> <simpleType name="sexType"> <enumeration value="男"/> <enumeration value="女"/> VenetianBlind.xml <person xmlns="http://www.example.org/04" xsi:schemaLocation="http://www.xy.com" sex="男"> <name>test</name> <age>10</age> <email>123456@QQ.com</email> </person> 复杂一些的dtd VenetianBlind2.xsd xmlns:xy="http://www.xy.com" <element name="persons" type="xy:personsType" /> <complexType name="personsType"> <element name="person" type="xy:personType"></element> <element name="name" type="string" /> <element name="age" type="xy:ageType" /> <element name="email" type="xy:emailType" /> <attribute name="sex" type="xy:sexType" default="男" /> <attribute name="id" type="ID" use="required" />
<minLength value="6" /> <maxLength value="255" /> <minInclusive value="1" /> <maxExclusive value="150" /> <enumeration value="男" /> <enumeration value="女" /> VenetianBlind2.xml <persons xmlns="http://www.xy.com" xsi:schemaLocation="http://www.xy.com">
<person sex="女" id="p1"> <email>123456@qq.com</email> <person sex="男" id="p2"> <name>test2</name> <age>20</age> </persons> (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|