About Me

我的相片
台北市, Taiwan
我是方選,
方白科技(finebind tech.)共同創辦人,
臺大資管所畢,
希望能幫助更多的人!

FB: http://fb.com/function1122
LINE: http://bit.ly/1foeZft (手機開啟點擊網址自動加入)

最新15則讀者回應

最新文章

FUNction's 上課筆記

Label Cloud

Blog Archive

FeedBurner

追蹤者

顯示具有 XML Schema 標籤的文章。 顯示所有文章
顯示具有 XML Schema 標籤的文章。 顯示所有文章

XML Schema 從看不懂到慢慢懂 - part 5

FUNction 於 2008年2月13日 下午5:36 發表
前一篇: XML Schema 從看不懂到慢慢懂 - part 4

Empty Element

Empty Element是一個沒有value,但是有attribute 的原件。長的像這樣:

<product prodid="1345" />


這就是變成Schema之後的樣貌…

<xs:element name="product">

<xs:complexType>

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

</xs:complexType>

</xs:element>


若在複雜一點,這邊我們也可以用complexContent包住restriction(限制必須為數字),再包住attribute(設定屬性):

<xs:element name="product">

<xs:complexType>

<xs:complexContent>

<xs:restriction base="xs:integer">

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

</xs:restriction>

</xs:complexContent>

</xs:complexType>

</xs:element>


Element Only

介紹完似乎無用的 "Empty Element",再火速介紹Element Only。顧名思義,這個元件只能包含其他Element。像下面例子中的標籤 "preson" 就是一個Empty Elementl:

<person>

<firstname>John</firstname>

<lastname>Smith</lastname>

</person>


所以我們可以把Schema 寫成如下示,sequence 已經在之前介紹過,他代表裡面的元件是「並排」的:

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


這篇介紹的兩個方法都可以另外寫好,然後使用上一篇教的 "type" 來套用,這裡就請舉一反三啦。


下一篇:待續...今天累了
繼續閱讀全文 XML Schema 從看不懂到慢慢懂 - part 5

XML Schema 從看不懂到慢慢懂 - part 4

FUNction 下午2:49 發表
前一篇: XML Schema 從看不懂到慢慢懂 - Part 3
Complex Element

Complex Element 包含其他element或attribute。

接下來我們會從簡單到複雜定義一個complex element。 "employee" 就是一個complex element,因為他裡面包著兩個element(firstname, lastname)。

<employee>

<firstname>John</firstname>

<lastname>Smith</lastname>

</employee>


用以下的schema可以為上面的XML定義。值得注意的是我們會用<xs:complexType><xs:sequence>標籤包住裡面的物件(element)。Sequence代表employee下面的物件已經沒有階層,而是平行的。

<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 element 裡面的element 一樣時,我們可以直接幫這個complex element 給一個 "type",在從type中做定義。接下來就可以把這個type 套用到多個complex element中了。

例如employee, student, member 都包含一樣的子element(firstname, lastname),所以我們另外定義了他的type。

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


還可以一層一層的包得像洋蔥,替換的淋漓盡致,看懂以下需要不少的抽象邏輯喔。

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


下一篇:XML Schema 從看不懂到慢慢懂 - part 5
繼續閱讀全文 XML Schema 從看不懂到慢慢懂 - part 4

XML Schema 從看不懂到慢慢懂 - Part 3

FUNction 下午2:01 發表
前一篇: XML Schema 從看不懂到慢慢懂 - Part 2
Attribute

這邊用很快的速度介紹屬性(Attribute),屬性的宣告和simple element 是一樣的,所以其他廢話就不多說了。值得注意的是,在XML裡,物件的屬性都是非必要的,如果是必要屬性,則須加上use="required"。

例如以下語法,就代表lang 這個屬性是必要的:

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


Restrictions

接下來是有點無聊的限制(Restrictions),可以告訴XML 某個數值的限制,例如得分最大不能超過100,最小不能小於0。

<score>98</score>


就可以用以下Schema 限制:

<xs:element name="score">

<xs:simpleType>

<xs:restriction base="xs:integer">

<xs:minInclusive value="0"/>

<xs:maxInclusive value="100"/>

</xs:restriction>

</xs:simpleType>

</xs:element>


好…費話不多說(因為再多說對速成也許沒幫助),這邊列出原文中可以用在限制的內容:

Constraint

Description

enumeration

Defines a list of acceptable values

fractionDigits

Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero

length

Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero

maxExclusive

Specifies the upper bounds for numeric values (the value must be less than this value)

maxInclusive

Specifies the upper bounds for numeric values (the value must be less than or equal to this value)

maxLength

Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero

minExclusive

Specifies the lower bounds for numeric values (the value must be greater than this value)

minInclusive

Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)

minLength

Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero

pattern

Defines the exact sequence of characters that are acceptable

totalDigits

Specifies the exact number of digits allowed. Must be greater than zero

whiteSpace

Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

英文也很好懂…我就不逐字翻譯了。


下一篇:XML Schema 從看不懂到慢慢懂 - part 4
繼續閱讀全文 XML Schema 從看不懂到慢慢懂 - Part 3

XML Schema 從看不懂到慢慢懂 - Part 2

FUNction 下午1:33 發表
前一篇: XML Schema 從看不懂到慢慢懂 - part 1

有一篇介紹schema,包含所謂的xmlns:xs, xmlns, targetNamespace, elementFormDefault ,但因為我覺得和快速入門沒有關係,所以就移掉了,有興趣想成為嘴砲高手的起記得看該篇文章。


Simple Element

我們知道XML Schema 定義了XML 中的物件。在XML 物件中"simple element" 代表這個物件裡面只包含文字,沒有再包裹其他物件(或屬性)。這裡的「文字」也代表數值、日期、布林值等等。

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

這是一個simple element 的範例,裡面包含了name 與type。

以下列出type可以包含的內容:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time


假設XML 文件包含以下simple element:

<authname>FUNction</lastname>

<age>22</age>

<dateborn>1985-11-22</dateborn>


相信聰明的你已經可以自己寫出他的schema了:

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

<xs:element name="age" type="xs:integer"/>

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



接下來是比較進階的部分,我們可以指定預設(default)與寫死的(fixed)的值:

例如以下的標籤 "color" 我們可以給他預設值紅色(red):

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


寫死的值顧名思義就是不能被更改,且也會自動賦予該值,像這裡 "color" 就被寫死成紅色(red),不能改成其他顏色了。

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


下一篇: XML Schema 從看不懂到慢慢懂 - Part 3
繼續閱讀全文 XML Schema 從看不懂到慢慢懂 - Part 2

XML Schema 從看不懂到慢慢懂 - part 1

FUNction 下午1:08 發表
XML 的Schema 定義著這個XML 的內容。對XML 非常生疏的我,因為工作需要也要努力學習看懂他的Schema。獨樂樂不如眾樂樂,我就邊看文章,邊把文章內容用自己的話寫出來,希望能寫出一系列的教學文件,幫助其他看不懂XML 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: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,原文中命名為"note.xsd"

xsd就是XML Schema 的附檔名,從這個定義裡面,我們可以寫出以下的note.xml 文件:

<?xml version="1.0"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>


接下來的文章,都會圍繞在看懂XML 的Schema 上…如果你覺得很無聊,可以直接跳過這個單元。

下一篇:XML Schema 從看不懂到慢慢懂 - Part 2

繼續閱讀全文 XML Schema 從看不懂到慢慢懂 - part 1