XSD كيف؟
يمكن أن تحتوي مستندات XML على مرجع إلى DTD أو مخطط XML.
مستند XML بسيط
انظر إلى مستند XML البسيط هذا المسمى "note.xml":
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
ملف DTD
المثال التالي هو ملف DTD يسمى "note.dtd" يحدد عناصر مستند XML أعلاه ("note.xml"):
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
يحدد السطر الأول عنصر الملاحظة ليكون له أربعة عناصر فرعية: "إلى ، من ، عنوان ، جسم".
يحدد السطر 2-5 عناصر الجسم إلى ، من ، العنوان ، لتكون من النوع "#PCDATA".
إلى مخطط XML
المثال التالي هو ملف مخطط XML يسمى "note.xsd" يحدد عناصر مستند XML أعلاه ("note.xml"):
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
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>
عنصر الملاحظة هو نوع معقد لأنه يحتوي على عناصر أخرى. العناصر الأخرى (إلى ، من ، عنوان ، جسم) هي أنواع بسيطة لأنها لا تحتوي على عناصر أخرى. سوف تتعلم المزيد عن الأنواع البسيطة والمعقدة في الفصول التالية.
إشارة إلى DTD
يحتوي مستند XML هذا على إشارة إلى DTD:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"https://www.w3schools.com/xml/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
مرجع إلى مخطط XML
يحتوي مستند XML هذا على مرجع إلى مخطط XML:
<?xml version="1.0"?>
<note
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/xml note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>