xml Schema實例

實例一

a.xml

<?xml version="1.0" encoding="UTF-8"?>
<student>
    <name>貂蟬</name>
    <age>20</age>
</student>

a.dtd

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE student[
<!ELEMENT student (name,age)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>
<student>
    <name>貂蟬</name>
    <age>20</age>
</student>

a.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="name" type="xs:string"></xs:element>
    <xs:element name="age" type="xs:int"></xs:element>
    <!--定義一個數據類型-->
    <xs:complexType name="stuType">
        <xs:sequence>
            <xs:element ref="name"></xs:element>
            <xs:element ref="age"></xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="student" type="stuType"></xs:element>
</xs:schema>

實例二

sample.xml

<?xml version="1.0" encoding="GB2312"?>
<電影 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance>
    <男主演>小羅伯特·唐尼</男主演>
    <女主演>格溫妮絲·帕特洛</女主演>
    <片名>
        <中文>鋼鐵俠3</中文>
    </片名>
</電影>

sample.xsd

<?xml version="1.0" encoding="GB2312"?>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema">
<xsd:element name="電影" type="演員"/>
    <xsd:complexType name="演員">
        <xsd:sequence>
            <xsd:group ref="主演"/>
            <xsd:element name="片名" type="類型"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="類型">
        <xsd:choice>
            <xsd:element name="中文" type="string"/>
            <xsd:element name="英文" type="string"/>
        </xsd:choice>
    </xsd:complexType>
    <xsd:group name="主演">
        <xsd:sequence>
            <xsd:element name="男主演" type="string"/>
            <xsd:element name="女主演" type="string"/>
        </xsd:sequence>
    </xsd:group>
</xsd:schema>

實例三

shiporder.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
  <orderperson>John Smith</orderperson>
  <shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
  </shipto>
  <item>
    <title>Empire Burlesque</title>
    <note>Special Edition</note>
    <quantity>1</quantity>
    <price>10.90</price>
  </item>
  <item>
    <title>Hide your heart</title>
    <quantity>1</quantity>
    <price>9.90</price>
  </item>
</shiporder>

對應的 shiporder.xsd

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="item" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="note" type="xs:string" minOccurs="0"/>
            <xs:element name="quantity" type="xs:positiveInteger"/>
            <xs:element name="price" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>

實例四

b.xml

<?xml version="1.0" encoding="UTF-8"?>

<student studentNo="1" score="88" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="demo3.xsd">
    <name>張三</name>
    <age>20</age>
    <gender></gender>
</student>

b.xsd

<xs:element name="student">
        <xs:complexType>
             <xs:sequence>
                 <xs:element name="name" type="xs:string"></xs:element>
                 <xs:element name="age" type="xs:int"></xs:element>
                 <xs:element name="gender" type="xs:string"></xs:element>
             </xs:sequence>
             <xs:attributeGroup ref="studattr"></xs:attributeGroup>
        </xs:complexType>
    </xs:element>
    <xs:attributeGroup name="studattr">
             <xs:attribute name="studno" type="xs:int" use="required"></xs:attribute>
             <xs:attribute name="score" type="xs:int" use="required"></xs:attribute>
    </xs:attributeGroup>

實例五

stu.xml

<?xml version="1.0" encoding="UTF-8"?>
<stuBook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///E:/xmlCode/stuSchema.xsd">
	<student sid="1">
		<name>張三</name>
		<sex></sex>
		<age>20</age>
	</student>
	<student sid="2">
		<name>李四</name>
		<sex></sex>
		<age>19</age>
	</student>
	<student sid="3">
		<name>王五</name>
		<sex></sex>
		<age>27</age>
	</student>
</stuBook>

stu.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="stuBook" type="stu"/>
	<xs:complexType name="stu">
		<xs:sequence maxOccurs="unbounded">
			<xs:element name="student" type="stuType"></xs:element>
		</xs:sequence>
	</xs:complexType>
	
	<xs:complexType name="stuType">	
		<xs:sequence>
			<xs:element name="name" type="xs:string"></xs:element>
			<xs:element name="sex" type="xs:string"></xs:element>
			<xs:element name="age" type="xs:int"></xs:element>
		</xs:sequence>
		<xs:attribute name="sid" type="xs:int" use="required"></xs:attribute>
	</xs:complexType>
</xs:schema>

實例六

lib.xml

<?xml version="1.0" encoding="UTF-8"?>
<library>
	<books>
		<book id="b-1-1">JSP實例編程</book>
		<book id="b-1-2">XML詳解</book>
		<book id="b-1-3">servlet</book>
	</books>
	<records>
		<items>
			<date>2012-08-02</date>
			<person name="李四" borrowwd="b-1-1 b-1-3"/>
		</items>
		<items>
			<date>2012-08-01</date>
			<person name="張三" borrowwd="b-1-1 b-1-2"/>
		</items>
	</records>
</library> 

lib.dtd

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT records ((items+))>
<!ELEMENT person EMPTY>
<!ATTLIST person
	borrowwd IDREFS #REQUIRED
	name CDATA #REQUIRED
>
<!ELEMENT library ((books, records))>
<!ELEMENT items ((date, person))>
<!ELEMENT date (#PCDATA)>
<!ELEMENT books ((book+))>
<!ELEMENT book (#PCDATA)>
<!ATTLIST book
	id ID #REQUIRED
>

lib.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2013 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="library"/>
	<xs:complexType name="library">
		<xs:sequence maxOccurs="unbounded">
			<xs:element name="books" type="booksType"/>
			<xs:element name="records" type="recType"/>
		</xs:sequence>
	</xs:complexType>
	
	<xs:simpleType name="bookIdType">
		<xs:restriction base="xs:string">
			<xs:pattern value="b-\d{1}-\d{1}"/>
		</xs:restriction>
	</xs:simpleType>
	
	<xs:complexType name="booksType">
		<xs:sequence maxOccurs="unbounded">
			<xs:element name="book" type="xs:string"/>
		</xs:sequence>
		<xs:attribute name="id" type="bookIdType" use="required"/>
	</xs:complexType>
	
	<xs:complexType name="recType">
		<xs:sequence maxOccurs="unbounded">
			<xs:element name="items">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="date" type="xs:date"/>
						<xs:element name="person" type="person"/>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:sequence>
	</xs:complexType>
	
	<xs:complexType name="person">
		<xs:simpleContent>
			<xs:extension base="xs:string">
				<xs:attribute name="name" type="xs:string"></xs:attribute>
				<xs:attribute name="borrowed">
					<xs:simpleType>
						<xs:list itemType="bookIdType"></xs:list>
					</xs:simpleType>
				</xs:attribute>
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
</xs:schema>

發佈了81 篇原創文章 · 獲贊 18 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章