閱讀911 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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 20:19:27

  上一篇:go Java中線程封閉之ThreadLocal
  下一篇:go AsyncQueryHandler的使用