模式(schema)本身就是一份格式良好的 XML 文件,它用強大的 XML 模式定義語言(XSDL,也稱 W3C Schema)來建模並驗證其他 XML 資料。而 schema 中的每個粒子——元素、型別、屬性及其他構造——依定義位置不同,會具有全域(exposed)或局部(hidden)的作用域。作用域的設計,極大程度決定了模式能否被改進、重用以及與其他技術協作。
無論你是剛起步,還是想讓既有方案發揮更大價值,理解作用域都是關鍵。本文先說明如何為各類粒子定義全域或局部作用域、作用域如何影響行為,接著介紹四種基本設計範式,最後給出選擇上的實務建議。
常用縮略詞:W3C(萬維網聯盟)、XML(可擴展標記語言)、XSDL(XML 模式定義語言)。
模式的最高層容器元素是 schema。schema 元素的直接子元素即為全域定義,可作為根節點,也可被模式的其他部分引用——定義一次,處處可用。
清單 1:
<xs:schema>
<xs:element name='postalCode' type='xs:string'/>
</xs:schema>
可成功驗證的資料實例:
<postalCode>14534</postalCode>
在這個實例中,postalCode 是根元素。只有模式最高層定義的元素才能充當根元素。清單 1 只定義了一個元素,所以很單純。
清單 2 在根層定義兩個元素:
<xs:schema>
<xs:element name='postalCode' type='xs:string'/>
<xs:element name='zipCode' type='xs:string'/>
</xs:schema>
這時 postalCode 與 zipCode 都可以充當根元素。
把元素定義為局部的,可以防止它被暴露給模式的其他部分——它的上下文局限於所在位置,無法被別處引用。
清單 3 中,zipCode 不是全域定義,而是作為 address 元素的子元素,定義在該元素的 complexType 內。
<xs:schema>
<xs:element name='address'>
<xs:complexType>
<xs:sequence>
<xs:element name='street' type='xs:string'/>
<xs:element name='city' type='xs:string'/>
<xs:element name='state' type='xs:string'/>
<xs:element name='zipCode' type='xs:string'/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
由於定義位於 address 的宣告之內,zipCode 的作用域只在 address 中。有效實例必須長這樣:
<address>
<!-- street、city、state 為求簡明而省略 -->
<zipCode>14534</zipCode>
</address>
這裡 address 是根元素;zipCode 不能充當根元素,因為它不是在根層全域定義的。局部定義的元素只能出現在定義它們的上下文中。
除了充當根元素,任何全域元素也能在需要的地方被引用。清單 5 中,全域 zipCode 被用於 address 的定義裡:
<xs:schema>
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name='street' type='xs:string'/>
<xs:element name='city' type='xs:string'/>
<xs:element ref='zipCode'/> <!-- 引用全域元素 -->
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- 全域定義、被上方引用的元素 -->
<xs:element name='zipCode' type='xs:string'/>
</xs:schema>
可見全域暴露的元素宣告支援模組化與重用:既能在本模式其他部分引用,也能被導入本模式的父模式引用。
屬性同理。清單 6 中,全域 state 被引用在 address 的局部上下文中:
<xs:schema>
<xs:element name='address'>
<xs:complexType>
<!-- 元素部分為可讀性而省略 -->
<xs:attribute ref="state"/>
</xs:complexType>
</xs:element>
<xs:attribute name="state" type="xs:string"/>
</xs:schema>
元素與屬性能全域或局部定義,型別也一樣。前面幾個例子用的是局部型別;要改成全域,把它移到根 schema 節點下並取個獨特名稱:
<xs:schema>
<xs:element name='address' type="address.type"/>
<xs:complexType name="address.type">
<xs:sequence>
<xs:element name='street' type='xs:string'/>
<xs:element name='city' type='xs:string'/>
<xs:element name='state' type='xs:string'/>
<xs:element name='zipCode' type='xs:string'/>
</xs:sequence>
</xs:complexType>
</xs:schema>
現在這個型別是全域的,名稱為 address.type。要把它關聯到元素,透過 type 屬性引用名稱即可。全域型別可以用 xs:extension 擴充,也可以用 xs:restriction 限制。
該把粒子定義成局部還是全域,並不總是容易決定,最佳選擇會隨使用情境、名稱空間需求與模式演進而異。一般可歸為四類。
名字來自那套一個套一個的木娃娃。此範式所有子元素都局部定義,每個元素及其型別都封裝在父元素之中。
清單 8(家電說明文件的簡化表示):
<xs:schema>
<xs:element name="HelpDoc">
<xs:complexType>
<xs:sequence>
<xs:element name="Section">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Body" type="xs:string"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
對應實例:
<HelpDoc>
<Section name="Operation_instructions">
<Title>Operating your appliance.</Title>
<Body>First, open the packaging and check to see...</Body>
</Section>
</HelpDoc>
每個子元素、屬性與型別都是局部定義,唯一的全域元素是根元素 HelpDoc。語法緊湊,有些人覺得較易讀。由於組件不對外暴露,這類模式被視為高度去耦(元素不全域依賴其他元素)且內聚(相關元素收在同一個自足的父元素裡)。適用情境是與其他系統互動少、組件不需重用——結構自足、名稱空間隱藏、不受外界影響。
快速筆記:當模式指定了 targetNamespace,所有全域粒子都必須以限定名稱(prefix:name)引用,這樣的名稱空間稱為「暴露的」。例如 <xs:element ref="xyz:Section"/> 是正確寫法,而 <xs:element ref="Section"/> 無效。
把清單 8 中所有局部元素都改成全域定義,就是這個範式。
清單 10:
<xs:schema>
<xs:element name="Body" type="xs:string"/>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Section">
<xs:complexType>
<xs:sequence>
<xs:element ref="Title"/>
<xs:element ref="Body"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="HelpDocs">
<xs:complexType>
<xs:sequence>
<xs:element ref="Section"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
這個範式暴露所有元素,因此可在模式其他部分引用重用,也讓它們對其他模式透明。主要優點是元素高度可重用;代價則是所有名稱空間都被暴露,元素之間的耦合升高——清單 10 中 Section 與 Title、Body 全域耦合,後者一改,Section 就跟著受影響。
不全域定義所有元素,而是先全域定義所有型別。
清單 11:
<xs:schema>
<xs:complexType name="section.type">
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Body" type="xs:string"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
<xs:complexType name="helpdocs.type">
<xs:sequence>
<xs:element name="Section" type="section.type"/>
</xs:sequence>
</xs:complexType>
<xs:element name="HelpDocs" type="helpdocs.type"/>
</xs:schema>
用全域型別定義提高重用能力;因為所有子元素都是局部的,額外的好處是能隱藏名稱空間。透過 elementFormDefault 屬性當作切換開關,你可以同時做到「結構定義暴露以便重用」與「名稱空間選擇性隱藏」,兩全其美。
元素宣告與型別宣告全部全域,把全局化推到極致。
清單 12:
<xs:schema>
<xs:attribute name="name" type="xs:string"/>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Body" type="xs:string"/>
<xs:element name="Section" type="section.type"/>
<xs:element name="HelpDocs" type="helpdocs.type"/>
<xs:complexType name="section.type">
<xs:sequence>
<xs:element ref="Title"/>
<xs:element ref="Body"/>
</xs:sequence>
<xs:attribute ref="name"/>
</xs:complexType>
<xs:complexType name="helpdocs.type">
<xs:sequence>
<xs:element ref="Section"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
這在內部與模式之間都最大化了重用,代價是強制名稱空間暴露。結構完全公開讓模式高度耦合卻也高度敏捷——元素相互依賴,大規模變更很快就能套用開來。
設計模式時,你其實一直在三件事之間拉鋸:暴露可重用組件、隱藏名稱空間、降低耦合(多個全域元素/型別間的相互依賴)。四種範式在重用潛能、耦合程度與暴露等級上的相對位置如下圖。
圖 1:暴露程度與各範式的耦合

高重用潛能可以縮短未來開發時間、讓大規模變更變得容易;但它也容易讓元素與型別不必要地糾纏在一起。當模式高度耦合時,元素互相牽制,未來的修改與擴充都變得棘手。更麻煩的是,一旦其他系統依賴你的介面保持一致,改進空間就被堵死了。所以「暴露什麼、暴露多少」必須審慎——這個決定一旦做出,後悔時往往已經難以回頭。
在同一個模式裡混用多種範式,往往是最務實的選擇:希望保持私有隱藏的結構用 Russian doll,需要對外暴露的元素則用 Salami slice 或 Garden of Eden。
清單 13:
<xs:schema>
<!-- Garden of Eden 風格組件 -->
<xs:element name="Title" type="xs:string"/>
<xs:element name="Body" type="xs:string"/>
<xs:element name="Section" type="section.type"/>
<xs:element name="HelpDocs" type="helpdocs.type"/>
<xs:complexType name="section.type">
<xs:sequence>
<xs:element ref="Title"/>
<xs:element ref="Body"/>
</xs:sequence>
<xs:attribute name="name"/>
</xs:complexType>
<xs:complexType name="helpdocs.type">
<xs:sequence>
<xs:element ref="Section"/>
<!-- Russian doll 風格組件 -->
<xs:element name="Credits">
<xs:complexType>
<xs:sequence>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Year" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
這個場景裡,HelpDocs、Section、Title、Body 需要暴露供其他模式使用;Credits 則刻意隱藏,避免與其他模式的定義耦合。因為全域化容易、去全域化困難,若重用的需求還不明確,先把元素設為全域是合理的保守選擇——未來要增加暴露很簡單,反之則否。
動手任何模式專案之前,你都得做出符合自身目標的設計選擇。理解作用域的使用,能讓模式與內容的開發流程更順暢,最終提升你管理整個模式生命週期、並讓模式高效與其他系統互動的能力。