如何使用 XSD?

XML ບັນດາບັນດາສິບສີ່ອາກາດສາມາດການອ້າງປະກອບ DTD ຫຼື XML Schema.

XML ບັນດາບັນດາສິບສີ່ອາກາດທີ່ງາມງາມ:

ບັນດາບັນດາ XML ບັນຊີ "note.xml":

<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

DTD ບັນຊີ

ຄວາມທີ່ພາຍຫຼັງນີ້ແມ່ນເຫດການຂອງ "note.dtd" DTD ບັນຊີບັນດາບັນດາສິບສິບສີ່ອາກາດທີ່ພາຍໃນ XML ບັນດາບັນດາສິບສີ່ອາກາດຂື້ນກ່ອນ:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

ການອອກອາກາດ note 元ິວິທະຍາມີສິບສີ່ບັນດາສິບສິບສີ່ອາກາດລຸ່ມ: "to, from, heading, body"。

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

XML Schema

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

<?xml version="1.0"?>


    
      
	
	
	
	
      
    


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

ການອ້າງຄວາມຂອງ DTD

ບົດສະຫຼະບັນນາທິການມີການອ້າງຄວາມຂອງ DTD:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "http://www.codew3c.com/dtd/note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

ການອ້າງຄວາມຂອງ XML Schema

ບົດສະຫຼະບັນນາທິການມີການອ້າງຄວາມຂອງ XML Schema:

<?xml version="1.0"?>
<note
xmlns="http://www.codew3c.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.codew3c.com note.xsd">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>