با سلام خدمت کاربران گرامی سایت پی وی لرن و علاقمندان و کاربرانی که علاقمند به یادگیری دوره XML هستند. در جلسه گذشته به معرفی و بررسی Schema پرداختیم و مختصراً با ویژگی ها و نقاط قوت آن آشنا شدیم. از این جلسه آموزش Schema را تخصصی تر آغاز می کنیم و قرار است به کاربرد XSD در Schema بپردازیم.
اسناد XML می تواند یک اشاره به یک DTD یا یک Schema XML داشته باشند.
به این سند XML ساده به نام “note.xml” نگاه کنید:
1 2 3 4 5 6 7 | <?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
مثال زیر یک فایل DTD با عنوان note.dtd است که عناصر سند XML را در بالا (note.xml) تعریف می کند:
1 2 3 4 5 | <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> |
خط اول عنصر note را تعریف می کند که دارای چهار عنصر child است: “to، from، head، body”.
خط ۲-۵ به عناصر تعریف from و heading و body به نوع “#PCDATA” می پردازد.
مثال زیر فایل XML Schema با نام note.xsd است که عناصر سند XML را در بالا (note.xml) تعریف می کند:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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> |
عنصر note یک نوع پیچیده است، زیرا حاوی عناصر دیگر است. عناصر دیگر (to, from, heading, body) انواع ساده هستند، زیرا آنها عناصر دیگر را شامل نمی شود.
در جلسات بعد در مورد مقادیر ساده و پیچیده بیشتر خواهید آموخت.
1 2 3 4 5 6 7 8 9 10 11 | <?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> |
1 2 3 4 5 6 7 8 9 10 11 | <?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> |
در این جلسه کاربرد XSD در Schema را بررسی کردیم و کار با آن را فراگرفتیم.
در جلسه بعدی به کار و بررسی schema> element> و یا عنصر schema خواهیم پرداخت.
با پی وی لرن همراه باشید.