Webservice_07_schema進階

RussianDoll.xsd

只有一個根元素,通過嵌套的方式完成編寫
優點:結構清晰,根元素只有一個
缺點:元素無法重用

 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/2"
	xmlns:tns="http://www.example.org/2" elementFormDefault="qualified">
	<element name="books">
		<complexType>
			<sequence maxOccurs="unbounded">
				<element name="book" id="bookId">
					<complexType>
						<sequence>
							<element name="title" type="string"></element>
							<choice>
								<element name="author" type="string"></element>
								<element name="authors">
									<complexType>
										<sequence maxOccurs="3">
											<element name="author" type="string"></element>
										</sequence>
									</complexType>
								</element>
							</choice>
							<element name="content" type="string"></element>
						</sequence>
						<attribute name="price" type="int" use="required"></attribute>
					</complexType>
				</element>
			</sequence>
		</complexType>
	</element>
</schema>

 

SalamiSlice.xsd

優點:能夠進行最大化重用
缺點:根元素不清晰

 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/3"
	xmlns:tns="http://www.example.org/3" elementFormDefault="qualified">
	<element name="book" type="tns:bookType"></element>
	<element name="title" type="string"></element>
	<element name="author" type="string"></element>
	<element name="price" type="int"></element>

	<complexType name="bookType">
		<sequence>
			<element ref="tns:title" />
			<element ref="tns:author" />
			<element ref="tns:price" />
		</sequence>
	</complexType>
</schema>

 

VenetianBlind.xsd

最好的選擇。

 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/VenetianBlind"
	xmlns:tns="http://www.example.org/VenetianBlind" elementFormDefault="qualified">

	<element name="coder" type="tns:coderType" ></element>

	<complexType name="coderType">
		<sequence>
			<element name="name" type="string" />
			<element name="email" type="tns:emailType" />
			<element name="sex" type="tns:sexType" />
		</sequence>
		<attribute name="coderId" type="tns:coderIdType"></attribute>
	</complexType>

	<simpleType name="emailType">
		<restriction base="string">
			<pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"></pattern>
		</restriction>
	</simpleType>

	<simpleType name="sexType">
		<restriction base="string">
			<enumeration value="男"></enumeration>
			<enumeration value="女"></enumeration>
		</restriction>
	</simpleType>

	<simpleType name="coderIdType">
		<restriction base="int">
			<minInclusive value="100"></minInclusive>
			<maxInclusive value="200"></maxInclusive>
		</restriction>
	</simpleType>
</schema>


引入schema

 

Coder.xsd

 

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Item"
	xmlns:tns="http://www.example.org/Item" elementFormDefault="qualified">

	<xsd:element name="coder" type="tns:coderType" />

	<xsd:complexType name="coderType">
		<xsd:sequence>
			<xsd:element name="name" type="xsd:string" />
			<xsd:element name="email" type="tns:emailType" />
			<xsd:element name="sex" type="tns:sexType" />
		</xsd:sequence>
		<xsd:attribute name="coderId" type="tns:coderIdType"/>
	</xsd:complexType>

	<xsd:simpleType name="emailType">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"/>
		</xsd:restriction>
	</xsd:simpleType>

	<xsd:simpleType name="sexType">
		<xsd:restriction base="xsd:string">
			<xsd:enumeration value="男"/>
			<xsd:enumeration value="女"/>
		</xsd:restriction>
	</xsd:simpleType>

	<xsd:simpleType name="coderIdType">
		<xsd:restriction base="xsd:int">
			<xsd:minInclusive value="100"/>
			<xsd:maxInclusive value="200"/>
		</xsd:restriction>
	</xsd:simpleType>
</xsd:schema>


 

Item.xsd

 

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	targetNamespace="http://www.example.org/Item" xmlns:tns="http://www.example.org/Item"
	elementFormDefault="qualified">

	<xsd:include schemaLocation="Coder.xsd"/>
	
	<xsd:element name="item" type="tns:itemType" />

	<xsd:complexType name="itemType">
		<xsd:sequence>
			<xsd:element name="name" type="xsd:string" />
			<xsd:element name="boss" type="tns:coderType" />
			<xsd:sequence minOccurs="1" maxOccurs="unbounded">
				<xsd:element name="coder" type="tns:coderType" />
			</xsd:sequence>
		</xsd:sequence>
		<xsd:attribute name="itemId" type="tns:itemIdType" />
	</xsd:complexType>

	<xsd:simpleType name="itemIdType">
		<xsd:restriction base="xsd:int">
			<xsd:minInclusive value="100" />
			<xsd:maxInclusive value="200" />
		</xsd:restriction>
	</xsd:simpleType>
</xsd:schema>


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章