天天看點

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">

<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

targetnamespace="http://www.xy.com" 

xmlns:tns="http://www.xy.com" 

<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>[email protected]</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" />

<pattern value="(\w+\.*)*\w+@\w+\.[a-za-z]{2,6}" />

<minlength value="6" />

<maxlength value="255" />

<mininclusive value="1" />

<maxexclusive value="150" />

<enumeration value="男" />

<enumeration value="女" />

venetianblind2.xml

<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>

 </person>

 <person sex="男" id="p2">

  <name>test2</name>

<age>20</age>

</persons>