XML(3)——schema文件的三种编写方式
一、schema文件编写方式①Russian Doll(俄罗斯套娃)
②Salami Slice(香肠切片)
③Venetian Blind(百叶窗) 推荐
二、Russian Doll俄罗斯套娃
顾名思义,编写方式是一层套一层,只有一个根元素,通过且套的方式编写完成。
优点:结构清晰
缺点:元素无法重用
RussionDoll.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.xy.com"
xmlns:tns="https://www.xy.com"
elementFormDefault="qualified">
<element name="books">
<complexType>
<!-- maxOccurs表示最大出现次数,unbounded表示次数无限制。默认次数为1次 -->
<sequence maxOccurs="unbounded">
<element name="book">
<complexType>
<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">
<complexType>
<!-- all标签中的元素可以按顺序出现,但次数为1次 -->
<all>
<!-- 每个元素只能出现一次 -->
<element name="author" type="string" />
</all>
</complexType>
</element>
</choice>
</sequence>
<!-- book的属性,在schema中位置必须在sequence之后 -->
<attribute name="id" type="int" use="required" />
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
RussionDoll.xml
<?xml version="1.0" encoding="UTF-8"?>
<book:books xmlns:book="https://www.example.org/02"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="/02.xsd">
<book:book >
<book:title>Java in action</book:title>
<book:content>Java is good</book:content>
<book:author>TOM</book:author>
</book:book>
<book:book >
<book:title>SOA in action</book:title>
<book:content>SOA is difficult</book:content>
<book:authors>
<book:author>LILY</book:author>
</book:authors>
</book:book>
</book:books>
二、Salami Slice腊肠切片
优点:可以进行最大化的重用
缺点:根元素不清晰。book、id、title、content所有的element都可以作为根元素
SalamiSlice.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.xy,com"
xmlns:tns="https://www.xy.com"
elementFormDefault="qualified">
<element name="book" type="tns:bookType"></element>
<element name="id" type="int" />
<element name="title" type="string" />
<element name="content" type="string" />
<complexType name="bookType">
<sequence>
<element ref="tns:id" />
<element ref="tns:title" />
<element ref="tns:content" />
</sequence>
</complexType>
</schema>
三、百叶窗Venetian Blind
根元素清晰,元素可重用
VenetianBlind.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.xy.com"
xmlns:tns="https://www.xy.com"
elementFormDefault="qualified">
<element name="person" type="tns:personType"/>
<complexType name="personType">
<sequence>
<element name="name" type="string"/>
<element name="age" type="tns:ageType"/>
<element name="email" type="tns:emailType"/>
</sequence>
<attribute name="sex" type="tns:sexType"/>
</complexType>
<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"/>
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男"/>
<enumeration value="女"/>
</restriction>
</simpleType>
</schema>
VenetianBlind.xml
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns="https://www.example.org/04"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.xy.com" sex="男">
<name>test</name>
<age>10</age>
<email>123456@QQ.com</email>
</person>
复杂一些的dtd
VenetianBlind2.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.xy.com"
xmlns:xy="https://www.xy.com"
elementFormDefault="qualified">
<element name="persons" type="xy:personsType" />
<complexType name="personsType">
<sequence maxOccurs="unbounded">
<element name="person" type="xy:personType"></element>
</sequence>
</complexType>
<complexType name="personType">
<sequence>
<element name="name" type="string" />
<element name="age" type="xy:ageType" />
<element name="email" type="xy:emailType" />
</sequence>
<attribute name="sex" type="xy:sexType" default="男" />
<attribute name="id" type="ID" use="required" />
</complexType>
<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" />
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男" />
<enumeration value="女" />
</restriction>
</simpleType>
</schema>
VenetianBlind2.xml
<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns="https://www.xy.com"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.xy.com">
<person sex="女" >
<name>test</name>
<age>10</age>
<email>123456@qq.com</email>
</person>
<person sex="男" >
<name>test2</name>
<age>20</age>
<email>123456@qq.com</email>
</person>
</persons>
最后更新:2017-04-03 08:26:19
上一篇:
2014百度联盟峰会:预计分成70亿,技术创新引领产业变革
下一篇:
IOS中修改导航默认标题颜色、字体
HttpClient 流式读取时中文乱码的解决方法
《深入理解Elasticsearch(原书第2版)》一2.1.3 Elasticsearch如何看评分
未能正确加载包"visla Studio HTM Editor Package" 问题的解决
PreferenceActivity 用法2
[LeetCode]73.Set Matrix Zeroes
projecteuler_problem9
设置Jasperreport每页记录数
AppCan 双击返回按钮退出应用
Windows下手动完全卸载Oracle
PostgreSQL 多元线性回归 - 1 MADlib的安装