XSD - 語法
XML XSD 儲存在一個單獨的檔案中,然後這個檔案可以連結到一個 XML 文件中以使用它。
語法
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>
<Schema> Element
Schema 是 XSD 的根元素,它總是必需的。
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
以上片段指定 Schema 中使用的元素和資料型別在 http://www.w3.org/2001/XMLSchema 名稱空間中定義,並且這些元素/資料型別應使用 xs 作為字首。它總是必需的。
targetNamespace = "https://tutorialspoint.tw"
以上片段指定此 Schema 中使用的元素在 https://tutorialspoint.tw 名稱空間中定義。它是可選的。
xmlns = "https://tutorialspoint.tw"
以上片段指定預設名稱空間為 https://tutorialspoint.tw。
elementFormDefault = "qualified"
以上片段表示在此 Schema 中宣告的任何元素在任何 XML 文件中使用之前都必須具備名稱空間限定。它是可選的。
引用 Schema
看一看以下引用 Schema −
<?xml version = "1.0"?>
<class xmlns = "https://tutorialspoint.tw"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "https://tutorialspoint.tw student.xsd">
<student rollno = "393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno = "493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno = "593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
xmlns = "https://tutorialspoint.tw"
以上片段指定預設名稱空間宣告。此名稱空間由 Schema 驗證程式檢查使用,以確保所有元素都是此名稱空間的一部分。它是可選的。
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "https://tutorialspoint.tw student.xsd">
定義 XMLSchema-instance xsi 後,使用 schemaLocation 屬性。此屬性有兩個值:名稱空間和 XML Schema 的位置,使用空格分隔。它是可選的。
廣告