课程表

Schema 教程

简单的类型

复杂的类型

数据类型

参考手册

工具箱
速查手册

XSD 如何使用

当前位置:免费教程 » XML相关 » Schema

XML 文档可对 DTD 或 XML Schema 进行引用。

一个简单的 XML 文档:

请看这个名为 "note.xml" 的 XML 文档:

  1. <?xml version="1.0"?>
  2. <note>
  3. <to>George</to>
  4. <from>John</from>
  5. <heading>Reminder</heading>
  6. <body>Don't forget the meeting!</body>
  7. </note>

DTD 文件

下面这个例子是名为 "note.dtd" 的 DTD 文件,它对上面那个 XML 文档的元素进行了定义:

  1. <!ELEMENT note (to, from, heading, body)>
  2. <!ELEMENT to (#PCDATA)>
  3. <!ELEMENT from (#PCDATA)>
  4. <!ELEMENT heading (#PCDATA)>
  5. <!ELEMENT body (#PCDATA)>

第 1 行定义 note 元素有四个子元素:"to, from, heading, body"。

第 2-5 行定义了 to, from, heading, body 元素的类型是 "#PCDATA"。

XML Schema

下面这个例子是一个名为 "note.xsd" 的 XML Schema 文件,它定义了上面那个 XML 文档的元素:

  1. <?xml version="1.0"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="//www.w3xue.com"
  4. xmlns="//www.w3xue.com"
  5. elementFormDefault="qualified">
  6.  
  7. <xs:element name="note">
  8. <xs:complexType>
  9. <xs:sequence>
  10. <xs:element name="to" type="xs:string"/>
  11. <xs:element name="from" type="xs:string"/>
  12. <xs:element name="heading" type="xs:string"/>
  13. <xs:element name="body" type="xs:string"/>
  14. </xs:sequence>
  15. </xs:complexType>
  16. </xs:element>
  17.  
  18. </xs:schema>

note 元素是一个复合类型,因为它包含其他的子元素。其他元素 (to, from, heading, body) 是简易类型,因为它们没有包含其他元素。您将在下面的章节学习更多有关复合类型和简易类型的知识。

对 DTD 的引用

此文件包含对 DTD 的引用:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE note SYSTEM "//www.w3xue.com/dtd/note.dtd">
  3. <note>
  4. <to>George</to>
  5. <from>John</from>
  6. <heading>Reminder</heading>
  7. <body>Don't forget the meeting!</body>
  8. </note>

对 XML Schema 的引用

此文件包含对 XML Schema 的引用:

  1. <?xml version="1.0"?>
  2. <note
  3. xmlns="//www.w3xue.com"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="//www.w3xue.com note.xsd">
  6.  
  7. <to>George</to>
  8. <from>John</from>
  9. <heading>Reminder</heading>
  10. <body>Don't forget the meeting!</body>
  11. </note>
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号