XML(3)——schema文件的三种编写方式

简介:
一、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">
<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="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:book>
</book:books>


二、Salami Slice腊肠切片
优点:可以进行最大化的重用
缺点:根元素不清晰。book、id、title、content所有的element都可以作为根元素
SalamiSlice.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="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="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.xy.com" 
xmlns:tns="http://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="http://www.example.org/04"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://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="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.xy.com"
xmlns:xy="http://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="http://www.xy.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.xy.com">
   
 <person sex="女" id="p1">
  <name>test</name>
<age>10</age>
<email>123456@qq.com</email>
 </person>
 <person sex="男" id="p2">
  <name>test2</name>
<age>20</age>
<email>123456@qq.com</email>
 </person>
</persons>
目录
相关文章
|
8天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
9 0
|
10天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
29天前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
11 0
|
30天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
9 0
|
1月前
|
Kubernetes Cloud Native Java
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
37 1
|
30天前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
10 0
|
2月前
|
XML JavaScript 数据格式
打开 XML 文件报错 There is no Unicode byte order mark
打开 XML 文件报错 There is no Unicode byte order mark
41 0
|
2月前
|
XML 存储 数据格式
什么是 XML 文件的 Schema
什么是 XML 文件的 Schema
25 0
|
25天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
14 1
|
29天前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0