DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> XML學習教程 >> XML詳解
XML Schema 入門完整指南:從最簡單結構到屬性與自訂型別
編輯:XML詳解     

從零開始認識 XML Schema

XML Schema(後綴為 .xsd)本質上也是一份完全符合 XML 語法的文件,它的根元素是 schema,並透過命名空間 xmlns:xsd="https://www.w3.org/2001/XMLSchema" 來宣告所用的詞彙。我們用 <element> 元素來描述實例文件中會出現的元素,本文将以由淺入深的方式,帶你一步步掌握 Schema 的撰寫技巧。

1. 最精簡的 Schema 文件

先看一份最樸素的實例文件 hello.xml:

<?xml version="1.0"?>
<greeting>Hello World!!</greeting>

它只有一個根元素 greeting,沒有屬性、沒有子元素,內容是一段字串。對應的 hello.xsd 可以寫成:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="https://www.w3.org/2001/XMLSchema">
  <xsd:element name="greeting" type="xsd:string"/>
</xsd:schema>

2. 含子元素的 Schema

當實例文件 customer.xml 出現巢狀結構時:

<customer>
  <name>teiki</name>
  <address>No.237, Road Waitan, Shanghai</address>
</customer>

由於 customer 含有兩個子元素,我們需要用 complexType 來定義它,並以 sequence 指定子元素出現的先後順序:

<xsd:element name="customer">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element name="address" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

3. 含子元素與孫元素的 Schema

若 address 本身又含有子元素,例如:

<address>
  <prefecture>Zhejiang</prefecture>
  <city>Hangzhou</city>
  <street>Xilu Road, No.121, 7F</street>
</address>

只要在 address 元素內再嵌一層 complexType 與 sequence 即可。此外,我們也能用 ref 屬性把重複使用的型別抽出來,讓文件更有可讀性:

<xsd:element name="customer">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element ref="address"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="address">
  ...(此處定義 address 的 complexType)...
</xsd:element>

4. 控制相同子元素的數量

假設訂購文件 order.xml 中,orderItem 的數量不得超過 10 筆,就要用到 maxOccurs 屬性:

<xsd:element name="orderItem" type="xsd:string" maxOccurs="10"/>

maxOccurs="10" 代表該元素最多可出現 10 次;若不限制則可寫成 maxOccurs="unbounded"。相對地,minOccurs 用來定義最少出現次數,例如 minOccurs="5" maxOccurs="10"。這兩個屬性的預設值都是 1。

5. 定義可選項的子元素

當 orderItem 可以用「書名」或「書號」其中一種方式填寫時,就要使用 choice 元素:

<xsd:choice>
  <xsd:element name="name" type="xsd:string"/>
  <xsd:element name="id" type="xsd:string"/>
</xsd:choice>

若還想加上選填的 quantity(例如數量為 1 時可省略),可以用 sequence 包住 choice,再將 quantity 的 minOccurs 設為 0,代表可有可無。

6. 內置與自訂簡單型別

XML Schema 內建了 44 種簡單型別,若仍無法滿足需求,就能自訂。例如要約束 ISBN 編碼格式:

<xsd:simpleType name="idType">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>
  </xsd:restriction>
</xsd:simpleType>

這裡以字串為基礎,再用 pattern(正則表達式)描述格式。若想改成列舉固定值,可用 enumeration;若想限定數值範圍,則用 minInclusive 與 maxInclusive,例如把 quantity 限制在 1 到 10 之間。

7. 定義元素屬性

屬性用 attribute 元素定義。若 orderItem 是個只帶屬性的空元素:

<xsd:element name="orderItem">
  <xsd:complexType>
    <xsd:sequence></xsd:sequence>
    <xsd:attribute name="id" type="idType" use="required"/>
  </xsd:complexType>
</xsd:element>

use 屬性有三種取值:required(必填)、optional(可選)、prohibited(禁止使用)。屬性的預設值則用 default 指定,例如 <xsd:attribute name="quantity" type="xsd:integer" default="1"/>。當屬性眾多時,可改用屬性組 attributeGroup 來重複引用,讓結構更清爽。

在 XML 中引用 XSD

若要在實例文件裡宣告它遵循某份 Schema,可使用 xsi:noNamespaceSchemaLocation;而在 XSD 端則以 targetNamespace 對應,並透過 elementFormDefault="qualified" 要求實例文件中的元素帶上命名空間前綴。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved