一个简单的 XML 文档:
请看这个名为 "note.xml" 的 XML 文档:
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML Schema
下面这个例子是一个名为 "note.xsd" 的 XML Schema 文件,它定义了上面那个 XML 文档的元素:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
提示:XML Schema是用来定义元素和类型的
<schema> 元素是每一个 XML Schema 的根元素:
<schema> 元素可包含属性。
下面的片断:
xmlns:xs="http://www.w3.org/2001/XMLSchema"显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:
这个片断:
targetNamespace="http://www.w3school.com.cn" 显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。
这个片断:
xmlns="http://www.w3school.com.cn" 指出默认的命名空间是 "http://www.w3school.com.cn"。
这个片断:
elementFormDefault="qualified" 指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。
xs表示QName中的prefix
schema表示QName中的localPart,说明这是一个schema类型的文档
http://www.w3.org/2001/XMLSchema表示QName中的namespaceURI