XSD - 语法
XML XSD 储存在一个单独的文件中,然后这个文件可以链接到一个 XML 文档中以使用它。
语法
XSD 的基本语法如下 −
<?xml version = "1.0"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
targetNamespace = "https://tutorialspoint.com"
xmlns = "https://tutorialspoint.com" 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.com"
以上片段指定此 Schema 中使用的元素在 https://tutorialspoint.com 名称空间中定义。它是可选的。
xmlns = "https://tutorialspoint.com"
以上片段指定默认名称空间为 https://tutorialspoint.com。
elementFormDefault = "qualified"
以上片段表示在此 Schema 中声明的任何元素在任何 XML 文档中使用之前都必须具备名称空间限定。它是可选的。
引用 Schema
看一看以下引用 Schema −
<?xml version = "1.0"?>
<class xmlns = "https://tutorialspoint.com"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "https://tutorialspoint.com 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.com"
以上片段指定默认名称空间声明。此名称空间由 Schema 验证程序检查使用,以确保所有元素都是此名称空间的一部分。它是可选的。
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "https://tutorialspoint.com student.xsd">
定义 XMLSchema-instance xsi 后,使用 schemaLocation 属性。此属性有两个值:名称空间和 XML Schema 的位置,使用空格分隔。它是可选的。
广告