+ All Categories
Home > Documents > [ 教學 ] XML Schema

[ 教學 ] XML Schema

Date post: 05-Jan-2016
Category:
Upload: helga
View: 115 times
Download: 0 times
Share this document with a friend
Description:
[ 教學 ] XML Schema. for everyone 2007-11-26 by Achi. 目錄 – (1). Schema Tutorial XSD HOME XSD Introduction XSD Why Use XSD How To XSD Simple Types XSD Elements XSD Attributes XSD Restrictions. 目錄 – (2). Complex Types XSD Elements XSD Empty XSD Elements Only XSD Text Only - PowerPoint PPT Presentation
96
[ 教教 ] XML Schema for everyone 2007-11-26 by Achi
Transcript

[ 教學 ] XML Schema

for everyone

2007-11-26 by Achi

目錄 – (1)

Schema Tutorial XSD HOME XSD Introduction XSD Why Use XSD How To XSD <schema>

Simple Types XSD Elements XSD Attributes XSD Restrictions

目錄– (2)

Complex Types XSD Elements XSD Empty XSD Elements Only XSD Text Only XSD Mixed XSD Indicators XSD <any> XSD <anyAttribute> XSD Substitution XSD Example

目錄– (3)

Data Types XSD String XSD Date XSD Numeric XSD Misc

References XSD Reference

總結

Schema TutorialXSD HOME

XML Schema 和 DTD :都是在定義 XML Document 的內容架構。

DTD ,早期的文件定義以它為主。 XML Schema ,新一代的驗證機制,可取代 DTD ,強化 DTD 的缺點。

DTD 文件的附檔名是 dtd 。 XML Schema 文件的附檔名是 xsd 。

所以, XML Schema 也可以稱為 XSD (XML Schema Definition) 。

網站: http://www.w3schools.com/schema/default.asp

Schema TutorialXSD Introduction

在開始了解 XML Schema 之前,下列的基本概念請先 ready : HTML / XHTML XML & XML Namespaces DTD

XML Schema 有多項優點,足以取代 DTD 的位置。 Refer to : http://www.w3schools.com/schema/schema_intro.asp

XML Schema is a W3C Standard 。

Schema TutorialXSD Why Use

XML Schema support data types 。 XML Schema use XML syntax 。 XML Schema secure data communication 。 XML Schema are extensible 。 Well-Formed is not enough 。

Detail content refer to : http://www.w3schools.com/schema/schema_why.asp

Schema TutorialXSD How To ( 續 ..)

以下示範如何用 XML Schema 與 DTD 來跟 XML Document 做溝通。

Schema TutorialXSD How To ( 續 ..)

XML Document (.xml)

以上是一個 Simple XML Document ,也是一般我們常會見到的格式。 接下來,示範符合 DTD 格式的 .xml & .dtd 。 以及,示範符合 XML Schema 格式的 .xml & .xsd 。

<?xml version="1.0"?><note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>

Schema TutorialXSD How To ( 續 ..)

reference to DTD (.xml) 。

<!ELEMENT note (to, from, heading, body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>

DTD File (.dtd) 。

<?xml version="1.0"?><!DOCTYPE note SYSTEM"http://www.w3schools.com/dtd/note.dtd"><note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>

Schema TutorialXSD How To

reference to XML Schema (.xml) 。

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>

XML Schema File (.xsd) 。

<?xml version="1.0"?><note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body></note>

Schema TutorialXSD <schema> ( 續 ..)

每一個 XML Schema 檔案 (.xsd) 裡,都會有一個根元素 (root element)

叫做 <schema> element 。

範例 1 : (<schema> 用法 )

範例 2 : (<schema> 通常會定義某些屬性,在下一頁會有說明 )

<?xml version="1.0"?><xs:schema>......</xs:schema>

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">......</xs:schema>

Schema TutorialXSD <schema> ( 續 ..)

我們以 P.11 頁的範例來說明: for .xsd :

xmlns:xs="http://www.w3.org/2001/XMLSchema"

1. 在 XML Schema(.xsd) 裡,能夠使用 element & data types ,是因為有定義如上 namespaces 。2. 而且以 xs 為開頭,來使用任何 element 。

targetNamespace="http://www.w3schools.com"

1. 在 XML Schema(.xsd) 裡,能夠使用 note, to, from, heading , body 等, 是因為有定義如上的 namespaces 。

xmlns="http://www.w3schools.com"

1. Default namespaces 。

elementFormDefault="qualified"

1. 在符合 XML Schema 的 XML 檔案裡的任何 element ,都必須要在 .xsd 出現。

Schema TutorialXSD <schema>

我們以 P.11 頁的範例來說明: ( 雖然 .xml 沒有使用到 <schema> ,但卻也有定義類似的設定 )

for .xml :

refer to : http://www.w3schools.com/schema/schema_schema.asp

xmlns="http://www.w3schools.com"

1. Default namespace declaration 。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd"

1. 假如有設定第一行時,就可以使用第二行的 schemaLocation 屬性。2. 第一行: for namespace to use 。3. 第二行: XMl Scheam to use 。

休息一下

Simple TypesXSD Elements ( 續 ..)

簡單的語法,如下:

XML Schema 也有其他常見的 Data Type: xs:string xs:decimal xs:integer xs:boolean xs:date xs:time

<xs:element name="xxx" type="yyy"/>

element name element data type

Simple TypesXSD Elements ( 續 ..)

Example : ( 例:一個 XML Document 會對應到 一個 XML Schema)

.xml<lastname>Refsnes</lastname><age>36</age><dateborn>1970-03-27</dateborn>

.xsd<xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date"/>

Simple TypesXSD Elements

Default Values (當 value 沒有指定時,則會使用 default value)

Fixed Value (以 Java 的角度來說,就是 final String 的類型,也就是 value 為常數,已固定 )

<xs:element name="color" type="xs:string" default="red"/>

<xs:element name="color" type="xs:string" fixed="red"/>

Simple TypesXSD Attributes ( 續 ..)

簡單的語法,如下:

XML Schema 也有其他常見的 Data Type: xs:string xs:decimal xs:integer xs:boolean xs:date xs:time

<xs:attribute name="xxx" type="yyy"/>

attribute name attribute data type

Simple TypesXSD Attributes ( 續 ..)

Example : ( 例:一個 XML Document 會對應到 一個 XML Schema)

.xml<lastname lang="EN">Smith</lastname>

.xsd<xs:attribute name="lang" type="xs:string"/>

Simple TypesXSD Attributes

Default Values (當 value 沒有指定時,則會使用 default value)

Fixed Value (以 Java 的角度來說,就是 final String 的類型,也就是 value 為常數,已固定)

Required Attributes (如果 lang 這個 attribute is required,可以使用 use 這個屬性)

<xs:attribute name="lang" type="xs:string" default="EN"/>

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

<xs:attribute name="lang" type="xs:string" use=“required"/>

Simple TypesXSD Restrictions ( 續 ..)

Restrictions 的意思是說,在 XML Schema(.xsd) 裡,針對 elements 或attributes,去定義能夠使用的值(Values),且該值(Values)能夠有一些限制條件綁住。

Restrictions 也可以稱做 facets。

接著,我們將會以一連串很簡單的 example 來說明值(Values) 的用法:

Simple TypesXSD Restrictions ( 續 ..)

Restrictions on Values

<xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 age 。2. age 的值 (values) 有設定限制的條件。3. 值 (values) 的最小值是 0 。4. 值 (values) 的最大值是 120 。

Simple TypesXSD Restrictions ( 續 ..)

Restrictions on a Set of Values

<xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="MAZDA"/> <xs:enumeration value="FORD"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 值 (values) 的限制條件是:只能是某個集合 (Set) 。2. 該集合,我們用 enumeration 來表示。

3. 我們定義一個 element 叫做 car 。4. car 的值 (values) 有設定限制的條件。5. 值 (values) 只能夠是『 MAZDA 』『 FORD 』『 BMW 』。

<xs:element name="car" type="carType"> <xs:simpleType name="carType"> <xs:restriction base="xs:string"> <xs:enumeration value="MAZDA"/> <xs:enumeration value="FORD"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 此種寫法,功能同上。2. 在 element 寫 type , 會對應到 simpleType 的 name 上。

Simple TypesXSD Restrictions ( 續 ..)

Restrictions on a Series of Values – (1)

<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 值 (values) 的限制條件是: 只能是連續 (Series) 數字或字母。2. 該連續 (Series) ,我們用 pattern 來表示。

3. 我們定義一個 element 叫做 letter 。4. letter 的值 (values) 有設定限制的條件。5. 值 (values) 只能夠是『連續的小寫英文字母 a~z 』。

<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z][A-Z]"/> </xs:restriction> </xs:simpleType></xs:element>

說明:1. 我們定義一個 element 叫做 initials 。2. initials 的值 (values) 有設定限制的條件。3. 值 (values) 只能夠是 『 3 個英文字母,且是連續的大寫 A~Z 』。

Simple TypesXSD Restrictions ( 續 ..)

Restrictions on a Series of Values – (2)

<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 initials 。2. initials 的值 (values) 有設定限制的條件。3. 值 (values) 只能夠是 『 3 個英文字母,且是連續的大寫 A~Z 或小寫 a~z 』。

<xs:element name="choice"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[xyz]"/> </xs:restriction> </xs:simpleType></xs:element>

說明:1. 我們定義一個 element 叫做 choice 。2. choice 的值 (values) 有設定限制的條件。3. 值 (values) 只能夠是 『 1 個英文字母,且只能是 x,y, 或 z 』。

Simple TypesXSD Restrictions ( 續 ..)

Restrictions on a Series of Values – (3)

<xs:element name="prodid"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 prodid 。2. prodid 的值 (values) 有設定限制的條件。3. 值 (values) 只能夠是 『 5 個數字,且每個數字只能是 0 到 9 的範圍』。

Simple TypesXSD Restrictions ( 續 ..)

Other Restrictions on a Series of Values – (1)

<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 letter 。2. letter 的值 (values) 有設定限制的條件。3. 值 (values) 只能夠是 『 0 個值或多個值以上,且值只能是小寫英文字母 a~z 』。

<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z][A-Z])+"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 letter 。2. letter 的值 (values) 有設定限制的條件。3. 值 (values) 只能夠是 『 1 個值或多個值以上, 且值是由小寫字母 a~z ,在緊接著大寫字母 A~Z , 然後一直循環』。

例如: 『 sToP 』 - 合法。 『 Stop 』 - 不合法。 『 STOP 』 - 不合法。

Simple TypesXSD Restrictions ( 續 ..)

Other Restrictions on a Series of Values – (2)

<xs:element name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 gender 。2. gender 的值 (values) 有設定限制的條件。3. 值 (values) 只能夠是 『 male 或者是 (OR) female 』。

<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9]{8}"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 password 。2. password 的值 (values) 有設定限制的條件。3. 值 (values) 只能夠是 『 8 個字元,該字元可以是大小寫英文字母或數字』。

Simple TypesXSD Restrictions ( 續 ..)

Restrictions on Whitespace Characters – (1)

<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType></xs:element>

<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="replace"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 address 。2. address 的值 (values) 有設定限制的條件。3. whiteSpace 設定 replace , 表示說『所有跟空白有關的字元,都會被替代成空白字元』。

4. 跟空白有關的字元,如: line feeds( 一整行的意思 ) tabs spaces carriage returns( 應該指 換行字元吧,我不確定。 )

說明:

1. 空白字元也可以受到控制,我們用 whiteSpace 來表示。

2. 我們定義一個 element 叫做 address 。3. address 的值 (values) 有設定限制的條件。4. whiteSpace 設定 preserve , 表示說『空白字元都不會被移除,都會被保留下來』。

Simple TypesXSD Restrictions ( 續 ..)

Restrictions on Whitespace Characters – (2)

<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="collapse"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 address 。2. address 的值 (values) 有設定限制的條件。3. whiteSpace 設定 collapse , 表示說『所有跟空白有關的字元,都會被移除』。

4. 跟空白有關的字元,如: line feeds( 一整行的意思 ) tabs spaces carriage returns( 應該指 換行字元吧,我不確定。 )

Simple TypesXSD Restrictions ( 續 ..)

Restrictions on Length

<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 對於 value 的長度,也有其限制的方式。 如『 length 』『 maxLength 』『 minLength 』。

2. 我們定義一個 element 叫做 password 。3. password 的值 (values) 有設定限制的條件。4. 條件:『長度必須為 8 個』。

<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> <xs:maxLength value="8"/> </xs:restriction> </xs:simpleType></xs:element>

說明:

1. 我們定義一個 element 叫做 password 。2. password 的值 (values) 有設定限制的條件。3. 條件:『長度最小必須是 5 個,長度最大必須是 8 個』。

Simple TypesXSD Restrictions

Restrictions for Datatypes

refer to:http://www.w3schools.com/schema/schema_facets.asp

最後,這有一份完整的資料,是從網站上擷取下來的,自行參考。

休息一下

Complex TypesXSD Elements ( 續… )

What is a Complex Element? A complex element contains other elements and/or attributes。

Complex Elements 有下列四種類型: empty elements elements that contain only other elements elements that contain only text elements that contain both other elements and text 以此類推,每個 elements 也可能會包含 attributes。

下一頁會示範這四種類型的用法。

Complex TypesXSD Elements ( 續… )

Examples :A complex element ,叫做 product ,是屬於 empty element :<product pid="12345"/>

A complex element ,叫做 employee ,是屬於 contain only other elements :<employee> <firstname>John</firstname> <lastname>Smith</lastname></employee>

A complex element ,叫做 food ,是屬於 contain only text :<food type="dessert">Ice cream</food>

A complex element ,叫做 description ,是屬於 contain both elements and text :<description> It happened on <date lang="norwegian">03.03.99</date> ....</description>

Complex TypesXSD Elements ( 續… )

How to Define a Complex Element? - (1) 假設在 .xml 裡,我們定義了如下的資料架構:

那我們想要在 .xsd 裡去定義 .xml 的資料架構時,可以有兩種方式做得到。 下一頁,將會說明哪兩種方式。

<employee> <firstname>John</firstname> <lastname>Smith</lastname></employee>

Complex TypesXSD Elements ( 續… )

How to Define a Complex Element? - (2) 第一種方式:

直接在 employee 這個 element 裡宣告。 如此做法,只能夠讓 employee 自己使用,別的 elements 無法用到。

Child elements,像是 firstname 和 lastname,是被 <sequence> 所圍繞著,且擺放位置有順序性的。

<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType></xs:element>

Complex TypesXSD Elements ( 續… )

How to Define a Complex Element? - (3) 第二種方式:

透過 element 的 type 屬性,可以對應到 complexType 的 name 屬性。 如此做法,不只能夠讓 employee 自己使用,還可以讓別的 elements 一起使用,下一頁有範例。

<xs:element name="employee" type="personinfo"/>

<xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence></xs:complexType>

Complex TypesXSD Elements ( 續… )

How to Define a Complex Element? - (4)

下一頁還有比較複雜的範例。

<xs:element name="employee" type="personinfo"/><xs:element name="student" type="personinfo"/><xs:element name="member" type="personinfo"/>

<xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence></xs:complexType>

Complex TypesXSD Elements

How to Define a Complex Element ? - (5)

refer to:http://www.w3schools.com/schema/schema_complex.asp

<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence></xs:complexType>

<xs:complexType name="fullpersoninfo"> <xs:complexContent> <xs:extension base="personinfo"> <xs:sequence> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent></xs:complexType>

Complex TypesXSD Empty

Complex Empty Elements An empty complex element cannot have contents, only attributes。

refer to:http://www.w3schools.com/schema/schema_complex_empty.asp

An empty XML element : <product prodid="1345"/>

<xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType></xs:element>

<xs:element name="product" type="prodtype"/><xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/></xs:complexType>

另外一種寫

Complex TypesXSD Elements Only ( 續… )

Complex Types Containing Elements Only An “elements-only” complex type contains an element that

contains only other elements 。

在 .xml 中,有一個 element 叫做 person ,它只能夠包含其他的 element ,例: firstname , lastname

<person> <firstname>John</firstname> <lastname>Smith</lastname></person>

那我們把 .xml 轉換成對應的 .xsd 的格式,則會如下:

<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType></xs:element>

Complex TypesXSD Elements Only

refer to:http://www.w3schools.com/schema/schema_complex_elements.asp

上一頁的 .xsd ,也可以改寫成如下的寫法,用 refer 的方式:

<xs:element name="person" type="persontype"/>

<xs:complexType name="persontype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence></xs:complexType>

Complex TypesXSD Text Only ( 續… )

Complex Text-Only Elements A complex text-only element can contain text and attributes 。

<xs:element name="somename"> <xs:complexType> <xs:simpleContent> <xs:extension base="basetype"> .... .... </xs:extension> </xs:simpleContent> </xs:complexType></xs:element>

<xs:element name="somename"> <xs:complexType> <xs:simpleContent> <xs:restriction base="basetype"> .... .... </xs:restriction> </xs:simpleContent> </xs:complexType></xs:element>

說明:

1. 在 complex Type 裡,我們使用了 simpleContent 這個 element 。2. 當使用了 simpleContent ,就必須要在其內部定義 extension 或是 restriction 。

Complex TypesXSD Text Only

在 .xml 中,我們定義了一個 shoesize element ,它只能包含 text :

<shoesize country="france">35</shoesize>

那我們把 .xml 轉換成對應的 .xsd ,則會如下:

<xs:element name="shoesize"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType></xs:element>

Example : ( 續 ..)

Complex TypesXSD Text Only

Example:

refer to:http://www.w3schools.com/schema/schema_complex_empty.asp

續上一頁,也可以改成用 refer 的寫法,如下:

<xs:element name="shoesize" type="shoetype"/>

<xs:complexType name="shoetype"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent></xs:complexType>

Complex TypesXSD Mixed ( 續… )

Complex Types with Mixed Content A mixed complex type element can contain attributes, elements, and text 。

在 .xml 中,我們定義了一個 letter element ,它能夠包含 text and other elements :

<letter> Dear Mr.<name>John Smith</name>. Your order <orderid>1032</orderid> will be shipped on <shipdate>2001-07-13</shipdate>.</letter>

那我們把 .xml 轉換成對應的 .xsd ,則會如下:

<xs:element name="letter"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveInteger"/> <xs:element name="shipdate" type="xs:date"/> </xs:sequence> </xs:complexType></xs:element>

說明:

1. 要在 letter element 中使用 child-elements 的話, mixe

d 要設定為 true 。

Complex TypesXSD Mixed ( 續… )

Example:

refer to:http://www.w3schools.com/schema/schema_complex_mixed.asp

續上一頁,也可以改成用 refer 的寫法,如下:

<xs:element name="letter" type="lettertype"/><xs:complexType name="lettertype" mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveInteger"/> <xs:element name="shipdate" type="xs:date"/> </xs:sequence></xs:complexType>

Complex TypesXSD Mixed

Complex Types with Mixed Content A mixed complex type element can contain attributes, elements, and text 。

在 .xml 中,我們定義了一個 letter element ,它能夠包含 text and other elements :

<letter> Dear Mr.<name>John Smith</name>. Your order <orderid>1032</orderid> will be shipped on <shipdate>2001-07-13</shipdate>.</letter>

那我們把 .xml 轉換成對應的 .xsd ,則會如下:

<xs:element name="letter"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveInteger"/> <xs:element name="shipdate" type="xs:date"/> </xs:sequence> </xs:complexType></xs:element>

說明:

1. 要在 letter element 中使用 child-elements 的話, mixe

d 要設定為 true 。

Complex TypesXSD Indicators ( 續… )

Indicators (譯:指示器) We can control HOW elements are to be used in documents with indicators 。

There are seven indicators: Order indicators

ALL Choice Sequence

Occurrence indicators maxOccurs minOccurs

Group indicators elementGroup name attributeGroup name

下頁開始,將列舉一些範例。

Complex TypesXSD Indicators ( 續… )

Order indicators Order indicators are used to define the order of the elements 。

Order indicators - All 使用:<xs:all> 說明:child elements 會以任何順序顯示,且只會顯示一次。

Example:

<xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType></xs:element>

Complex TypesXSD Indicators ( 續… )

Order indicators - Choice 使用:<xs:choice> 說明:如果有多個 child elements,那只能夠顯示其中的一個。

Example:

<xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType></xs:element>

Complex TypesXSD Indicators ( 續… )

Order indicators - Sequence 使用:<xs:sequence> 說明:child elements,會以一個特定的順序顯示。

Example:

<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType></xs:element>

Complex TypesXSD Indicators ( 續… )

Occurrence indicators Occurrence indicators are used to define how often an element can occur 。

Occurrence indicators - maxOccurs 使用:maxOccurs = “?” (? 代表數字) 說明:child elements 可以顯示的最高次數。

Example:

<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"/> </xs:sequence> </xs:complexType></xs:element>

說明:

1. child_name elements 可以顯示最多 10 筆。2. 那最少筆數呢? 如果沒定義的話,預設最少會顯示 1 筆。

Complex TypesXSD Indicators ( 續… )

Occurrence indicators - minOccurs 使用:minOccurs = “?” (? 代表數字) 說明:child elements 可以顯示的最少次數。

Example:

<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/> </xs:sequence> </xs:complexType></xs:element>

說明:

1. child_name elements 可以顯示最多 10筆,最少要 0筆。

Complex TypesXSD Indicators ( 續… )

如果 child elements 不想要定義 minOccurs 或 maxOccurs 可以嗎? 如果 minOccurs 不寫,是可以的,因為預設值會是 1。 但如果不想要設定最多顯示的次數,則可以改成 maxOccurs=“unbounded”。 unbounded 意思是:不受限制次數。

到現在為止,我們已經看完了 Order & Occurrence indicators,下一頁,我們就來一個範例練習。

Complex TypesXSD Indicators ( 續… )

A working example : ( 續 ..)

假設我們定義了一個 xml ,叫做 Myfamily.xml ,如下:

<?xml version="1.0" encoding="ISO-8859-1"?><persons xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation="family.xsd"> <person> <full_name>Hege Refsnes</full_name> <child_name>Cecilie</child_name> </person>

<person> <full_name>Tove Refsnes</full_name> <child_name>Hege</child_name> <child_name>Stale</child_name> <child_name>Jim</child_name> <child_name>Borge</child_name> </person> <person> <full_name>Stale Refsnes</full_name> </person></persons>

Complex TypesXSD Indicators ( 續… )

A working example :

那我們把 .xml 轉換對應到 .xsd ,則格式如下 :

<?xml version="1.0" encoding="ISO-8859-1"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="persons"> <xs:complexType> <xs:sequence> <xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" minOccurs="0" maxOccurs="5"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>

Complex TypesXSD <any> ( 續… )

The <any> Element The <any> element enables us to extend the XML document with elements not specified by the schema s 。

Group indicators - elementGroups 使用:<xs:group> 在 <xs:group> 內,必須要定義 all, choice, sequence 其中一種。

Example:

<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence></xs:group>

Complex TypesXSD Indicators ( 續… )

Example :

也可以用 refer 的寫法,如下:

<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence></xs:group>

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence></xs:complexType>

Complex TypesXSD Indicators

Group indicators - attributeGroups 使用:<xs:attributeGroup>

Example:

Example:

<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/></xs:attributeGroup>

<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/></xs:attributeGroup>

<xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> </xs:complexType></xs:element>

Complex TypesXSD <any> ( 續… )

The <any> Element The <any> element enables us to extend the XML document with elements not specified by the schema 。 大概的意思是說:可以用 <xs:any> 來代替 <xs:element> 的宣告方式。

Example: 我們用一個 .xml 跟兩個 .xsd 來示範。

<?xml version="1.0" encoding="ISO-8859-1"?><persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.com family.xsd http://www.w3schools.com children.xsd"> <person> <firstname>Hege</firstname> <lastname>Refsnes</lastname> <children> <childname>Cecilie</childname> </children> </person>

<person> <firstname>Stale</firstname> <lastname>Refsnes</lastname> </person></persons>

說明:

1. 假設這是一個 Myfamily.xml 。2. 他可以由 family.xsd 和 children.xsd 組成。

Complex TypesXSD <any> ( 續… )

Example : 我們用一個 .xml 跟兩個 .xsd 來示範。

<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:any minOccurs="0"/> </xs:sequence> </xs:complexType></xs:element>

說明:

1. 假設這是一個 family.xsd 。2. By using <any> element we can extend the content of “person” with any element 。

Complex TypesXSD <any>

Example : 我們用一個 .xml 跟兩個 .xsd 來示範。

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

<xs:element name="children"> <xs:complexType> <xs:sequence> <xs:element name="childname" type="xs:string" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>

說明:

1. 假設這是一個 children.xsd 。

Refer to : http://www.w3schools.com/schema/schema_complex_any.asp

Complex TypesXSD <anyAttribute> ( 續… )

The <anyAttribute> Element The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema。

Example: 我們用一個 .xml 跟兩個 .xsd 來示範。

<?xml version="1.0" encoding="ISO-8859-1"?><persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.com family.xsd http://www.w3schools.com attribute.xsd"> <person gender="female"> <firstname>Hege</firstname> <lastname>Refsnes</lastname> </person>

<person gender="male"> <firstname>Stale</firstname> <lastname>Refsnes</lastname> </person></persons>

說明:

1. 假設這是一個 Myfamily.xml 。2. 他可以由 family.xsd 和 children.xsd 組成。

Complex TypesXSD <anyAttribute> ( 續… )

Example : 我們用一個 .xml 跟兩個 .xsd 來示範。

<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:anyAttribute/> </xs:complexType></xs:element>

說明:

1. 假設這是一個 family.xsd 。2. By using <anyAttribute> element we can add any number of attributes to the “person” element 。

Complex TypesXSD <anyAttribute>

Example : 我們用一個 .xml 跟兩個 .xsd 來示範。

<?xml version="1.0" encoding="ISO-8859-1"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:attribute name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> </xs:attribute></xs:schema>

說明:

1. 假設這是一個 children.xsd 。

Refer to : http://www.w3schools.com/schema/schema_complex_anyattribute.asp

Complex TypesXSD Substitution

這個 Substitution 我覺得用的機率不是很高,所以請各位自行到網站上查閱。

Refer to:http://www.w3schools.com/schema/schema_complex_subst.asp

Complex TypesXSD Example

Example 的部分,請直接去網站上查看比較方便。 因為該 example 的某些 xml content 會太多,在 powerpoint 上表達會不好觀看。

Refer to:http://www.w3schools.com/schema/schema_example.asp

休息一下

Data TypesXSD String ( 續… )

String Data Type String data types are used for values that contains character strings 。 Can contain characters, line feeds, carriage returns, tabs。

Example:

.xml

<customer>John Smith</customer>

.xml

<customer> John Smith </customer>

.xsd

<xs:element name="customer" type="xs:string"/>

上面兩種 .xml ,雖然資料內容不同,但對應到下面的 .xsd ,卻是相同的格式。而且 XML processor 不會對 value 有任何的修改。

Data TypesXSD String ( 續… )

NormalizedString Data Type NormalizedString data type 是從 String data type 而來的。 比較要注意的是,用了 NormalizedString,XML processor 將會過濾 line

feeds, carriage returns, tabs,並且將之移除掉。 上一頁的則是會保留。這頁的則是會 removed。

Example:

.xml

<customer>John Smith</customer>

.xml

<customer> John Smith </customer>

.xsd

<xs:element name="customer" type="xs:normalizedString"/>

上面兩種 .xml ,雖然資料內容不同,但對應到下面的 .xsd ,卻是相同的格式。但是 XML processor 會把 tabs 用 空白 來取代。

Data TypesXSD String ( 續… )

Token Data Type Token data type 是從 String data type 而來的。 比較要注意的是,用了 Token,XML processor 將會過濾 line

feeds, carriage returns, tabs, leading, trailing spaces, multiple spaces ,並且將之移除掉。

Example:

.xml

<customer>John Smith</customer>

.xml

<customer> John Smith </customer>

.xsd

<xs:element name="customer" type=“xs:token"/>

上面兩種 .xml ,雖然資料內容不同,但對應到下面的 .xsd ,卻是相同的格式。但是 XML processor 會把 tabs 給移除掉。

Data TypesXSD String ( 續… )

Other String Data Type 前面三頁是列舉一些從 String Data Type 衍生而來的 data types。 下列還有其他 data types,也可以到網站上參考。

Data TypesXSD String

Restrictions on String Data Type Restrictions that can be used with String data types:

enumeration length maxLength minLength pattern (NMTOKENS, IDREFS, and ENTITIES cannot use this constraint ) whiteSpace

Refer to : http://www.w3schools.com/schema/schema_dtypes_string.asp

Data TypesXSD Date ( 續… )

Date Data Type Date and time data types are used for values that contain date and time。 格式:YYYY-MM-DD 說明:年-月-日

Example:

.xsd

<xs:element name="start" type="xs:date"/>

.xml

<start>2002-09-24</start>

Data TypesXSD Date ( 續… )

Date Data Type - Time Zones (時區) 可以在 Date data type 的格式最後面,加上『Z』。 例如:YYYY-MM-DDZ

Example:

或是多個 + 或 -,如下:

<start>2002-09-24Z</start>

<start>2002-09-24-06:00</start>

<start>2002-09-24+06:00</start>

Data TypesXSD Date ( 續… )

Time Data Type 格式:hh:mm:ss 說明:時:分:秒

Example:

.xsd

<xs:element name="start" type="xs:time"/>

.xml

<start>09:00:00</start>

Data TypesXSD Date ( 續… )

Time Data Type – Time Zones (時區) 可以在 Time Data Type 的格式最後面,加上『Z』。 例如:hh:mm:ssZ

Example:

或是多個 + 或 -,如下:

<start>09:30:10Z</start>

<start>09:30:10-06:00</start>

<start>09:30:10+06:00</start>

Data TypesXSD Date ( 續… )

DateTime Data Type 格式:YYYY-MM-DDThh:mm:ss 說明:年-月-日T時:分:秒 (T 表示:年-月-日之後,緊接著需要開始顯示時間)

Example:

.xsd

<xs:element name="startdate" type="xs:dateTime"/>

.xml

<startdate>2002-05-30T09:00:00</startdate>

Data TypesXSD Date ( 續… )

DateTime Data Type – Time Zones (時區) 可以在 Time Data Type 的格式最後面,加上『Z』。 例如:hh:mm:ssZ

Example:

或是多個 + 或 -,如下:

<startdate>2002-05-30T09:30:10Z</startdate>

<startdate>2002-05-30T09:30:10-06:00</startdate>

<start>09:30:10+06:00</start>

Data TypesXSD Date ( 續… )

Duration Data Type Is used to specify a time interval。 格式:PnYnMnDTnHnMnS 說明:

P:the period (required) nY:the number of years nM:the number of months nD:the number of days T:the start of a time section (required if you are going to specify hours, minutes, seconds) nH:the number of hours nM:the number of minutes nS:the number of seconds

Example:

.xsd <xs:element name="period" type="xs:duration"/>

.xml <period>P5Y</period> <period>P5Y2M10D</period> <period>P5Y2M10DT15H</period> <period>PT15H</period> <period>-P10D</period> (還可以輸入負號喔 )

Data TypesXSD Date

總覽 Date and Time Data Type 還有其他的 type 類型,如下:

Restrictions on Date Data Types Restrictions that can be used with Date data types :

Enumeration maxExclusive maxInclusive minExclusive minInclusive Pattern whiteSpace

Refer to : http://www.w3schools.com/schema/schema_dtypes_date.asp

Data TypesXSD Numeric ( 續… )

Numeric Data Type (有小數點) Decimal data types are used for numeric values。 The maximum number of decimal digits you can specify is 18 。

Example:

.xsd <xs:element name="prize" type="xs:decimal"/>

.xml <prize>999.50</prize> <prize>+999.5450</prize> <prize>-999.5230</prize> <prize>0</prize> <prize>14</prize>

Data TypesXSD Numeric ( 續… )

Integer Data Type (沒有小數點 ) Decimal data types are used for numeric values 。

Example:

.xsd <xs:element name="prize" type="xs:integer"/>

.xml <prize>999</prize> <prize>+999</prize> <prize>-999</prize> <prize>0</prize>

Data TypesXSD Numeric ( 續… )

總覽 Numeric Data Types 還有其他的 type 類型,如下:

Data TypesXSD Numeric

Restrictions on Numeric Data Types Restrictions that can be used with Numeric data types :

Enumeration fractionDigits maxExclusive maxInclusive minExclusive minInclusive Pattern totalDigits whiteSpace

Refer to : http://www.w3schools.com/schema/schema_dtypes_numeric.asp

Data TypesXSD Misc ( 續… )

Other miscellaneous data types are boolean, base64Binary, hexBinary, float, double, anyURI, QName, and NOTATION

Boolean Data Type 可以用 true 或 false 來表示。 也可以用 1 或 0 來表示。

Example:

.xsd <xs:attribute name="disabled" type="xs:boolean"/>

.xml <prize disabled="true">999</prize>

Data TypesXSD Misc ( 續… )

Binary Data Types We have two binary data types:

base64Binary (Base64-encoded binary data)。 hexBinary (hexadecimal-encoded binary data)。

Example:

.xsd <xs:element name="blobsrc" type="xs:hexBinary"/>

Data TypesXSD Misc ( 續… )

AnyURI Data Type If a URI has spaces,replace them with %20。 (我看不懂這行的意思)

Example:

.xsd <xs:attribute name="src" type="xs:anyURI"/>

.xml <pic src="http://www.w3schools.com/images/smiley.gif" />

Data TypesXSD Misc

總覽 Miscellaneous Data Type 還有其他的 type 類型,如下:

Restrictions on Miscellaneous Data Types Restrictions that can be used with other data types :

Enumeration maxExclusive maxInclusive minExclusive minInclusive Pattern whiteSpace

Refer to : http://www.w3schools.com/schema/schema_dtypes_date.asp

休息一下

ReferencesXSD Reference

XSD Elements XSD Restrictions/Facets for Datatypes

以上兩點關於 XML Schema 的相關 element 或 restrictions,都可以直接在網站上查詢得到。

Refer to:http://www.w3schools.com/schema/schema_elements_ref.asp

總結 我是根據 XML Schema 的原文網站翻譯而來的,翻的不是很好,多見諒。 我看完的感想是:

其實 XML Schema 很簡單學的,只是要去瞭解它的一些 element,attribute,restriction 等語法的規則。 因為 XML Schema 是制訂出來的標準,就像是大家都使用共同的語言來溝通。 而我其實會想要好好去研究到底 XML Schema 是什麼東西,說通了,還不都是為了 XML。 話說最近有個案子,會使用到 XMLBeans,而要了解 XMLBeans,必須要先了解什麼是 XML Schema。 這樣說的意思,好像佛家所說的:因果關係。^^ 所以現在我大約也弄懂了什麼是 XML Schema,那接著我就要繼續研究我原本就想要了解的 XMLBeans。

另外 ,在原文網站上有提到說,當對 XML Schema 有了認識之後,接著就可以繼續去認識 WSDL。 WSDL,Web Services Description Language。

END


Recommended