XSD - 概述



XML 架構定義(通常稱為 XSD)是一種描述 XML 語言的精確方法。XSD 根據相應 XML 語言的語法規則檢查 XML 文件的結構和詞彙的有效性。

XML 文件可以定義為 -

  • 良好格式 - 如果 XML 文件遵守所有一般的 XML 規則,例如標籤必須正確巢狀,開啟和關閉標籤必須平衡,空標籤必須以 '/>' 結尾,則稱為良好格式

  • 有效 - XML 文件不僅良好格式,而且還符合可用的 XSD。XSD 指定它使用的標籤、這些標籤可以包含哪些屬性,以及哪些標籤可以出現在其他標籤內(除其他屬性外)。

下圖顯示了 XSD 如何用於構造 XML 文件 -

XSD Technology

下面是一段簡單的 XSD 程式碼。請看一看。

<?xml version = "1.0"?>

<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
   targetNamespace = "https://tutorialspoint.tw" 
   xmlns = "https://tutorialspoint.tw"
   elementFormDefault = "qualified">

   <xs:element name = 'class'>
      <xs:complexType>
         <xs:sequence>
            <xs:element name = 'student' type = 'StudentType' minOccurs = '0' 
               maxOccurs = 'unbounded' />
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:complexType name = "StudentType">
      <xs:sequence>
         <xs:element name = "firstname" type = "xs:string"/>
         <xs:element name = "lastname" type = "xs:string"/>
         <xs:element name = "nickname" type = "xs:string"/>
         <xs:element name = "marks" type = "xs:positiveInteger"/>
      </xs:sequence>
      <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
   </xs:complexType>
   
</xs:schema>

特色

以下是 XSD 的一些流行特色的列表 -

  • XSD 可針對未來新增項進行擴充套件。
  • XSD 比 DTD 更豐富且更強大。
  • XSD 以 XML 編寫。
  • XSD 支援資料型別。
  • XSD 支援名稱空間。
  • XSD 是 W3C 推薦。
廣告
© . All rights reserved.