一、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>
①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>