XSD ບົດສະຫຼຸບ
- ຫນຶ່ງກ່ອນ XSD ປ່ວຍສົງພັນ
- ຫນ້ານັ້ນ XSD ປະກອບຂໍ້ມູນ
ບົດນີ້ຈະສະແດງໃຫ້ທ່ານກວດກາວິທີການຂຽນ XML Schema.ທ່ານຈະຮຽນວ່າມີຫຼາຍວິທີທີ່ຂຽນ schema.
XML document
ພວກເຮົາຈະເບິ່ງ XML documentທີ່ມີຊື່ "shiporder.xml":
<?xml version="1.0" encoding="ISO-8859-1"?> <orderperson>George Bush</orderperson> <shipto> <name>John Adams</name> <address>Oxford Street</address> <city>London</city> <country>UK</country> </shipto> <item> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder>
XML documentນີ້ບັນຈຸມີປະກອບສາຍ "shiporder"،ທີ່ກວມມີປະສົມປະກອບ "orderid". "shiporder" elementກວມມີສາຍລູກສາຍສາມຢ່າງຕ່າງກັນ: "orderperson"、"shipto" ແລະ "item". "item" elementເກີດຂຶ້ນສອງຄັ້ງ,ມີ "title"、element "note" ທີ່ສາມາດບໍ່ມີ "note"、"quantity" ແລະ element "price".
ດັ່ງກ່າວນີ້ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",ບອກການຄົ້ນຫາ XML parser ທີ່ຈະກວດສອບເອກະສານດັ່ງກ່າວດ້ວຍ schema.ປະເພດນີ້:xsi:noNamespaceSchemaLocation="shiporder.xsd" ກໍານົດການຕັ້ງລຳດັບ schema (ບ່ອນນີ້ມັນຢູ່ໃນອາກາດດຽວກັນກັບ "shiporder.xml").
ສ້າງ XML Schema
ຕອນນີ້,ພວກເຮົາຕ້ອງກໍານົດ schema ຂອງ XML ທີ່ມີຂຶ້ນ.
ພວກເຮົາສາມາດເລີ່ມດ້ວຍການເປີດເອກະສານໃໝ່, ແລະຈະປ່ຽນຊື່ເອກະສານໃຫ້ເປັນ "shiporder.xsd". ເພື່ອສ້າງ schema, ພວກເຮົາພຽງແຕ່ຈະປະຕິບັດຕາມຂອບແມງຂອງເອກະສານ XML, ການກໍານົດບັນດາສະມາຊິກທີ່ພວກເຮົາພົບ.
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> ... ... </xs:schema>
ໃນ schema ທີ່ມີຂຶ້ນ, ພວກເຮົາໄດ້ໃຊ້ namespace ມັນກຳລັງ (xs), namespace ນີ້ກ່ຽວກັບ Schema language definition, ຄຳພິພາກທີ່ສົມບັດເປັນ http://www.w3.org/2001/XMLSchema.
ຕໍ່ມາ, ພວກເຮົາຕ້ອງກໍານົດ "shiporder" ສະມາຊິກ. ສະມາຊິກນີ້ມີບັນດາປະກອບປະກອງ, ດັ່ງນັ້ນພວກເຮົາຈະອອກມາກໍານົດວ່ານີ້ເປັນ "compound type". ບັນດາສະມາຊິກລົງມາຂອງ "shiporder" ສະມາຊິກຖືກກວມເຂົ້າໃນ "xs:sequence" ທີ່ກໍານົດຄຳພິພາກຂອງສະມາຊິກ:
<xs:element name="shiporder"> <xs:complexType> <xs:sequence> ... ... </xs:sequence> ... </xs:complexType> </xs:element>
ຕໍ່ມາ, ພວກເຮົາຕ້ອງກໍານົດ "orderperson" ສະມາຊິກຈະເປັນ "simple type" (ຍ້ອນວ່າສະມາຊິກນີ້ບໍ່ມີບາງບັນດາປະກອບປະກອງຫຼືສະມາຊິກອື່ນໆ). ການເອົາຄຳສັ່ງ "type" (xs:string) ແມ່ນຕາມຄຳສັ່ງຈາກມາງສະມາຊິກ, ມາງສະມາຊິກນີ້ກ່ຽວກັບ XML schema ທີ່ສະແດງຂໍ້ມູນຂອງ schema ທີ່ແນ່ນອນ:
<xs:element name="orderperson" type="xs:string"/>
ຕໍ່ມາ, ຂ້ອຍຕ້ອງກໍານົດສະມາຊິກສອງສະມາຊິກຈະເປັນ "compound type": "shipto" ແລະ "item". ພວກເຮົາເລີ່ມຈາກການກໍານົດ "shipto" ສະມາຊິກ:
<xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
ພາຍໃນ schema, ພວກເຮົາສາມາດໃຊ້ "maxOccurs" ແລະ "minOccurs" ທີ່ຈະກໍານົດຈຳນວນການປະກົດຂອງສະມາຊິກ. "maxOccurs" ຈະກໍານົດຈຳນວນການປະກົດທີ່ສູງສຸດຂອງສະມາຊິກ, ຂະນະທີ່ "minOccurs" ຈະກໍານົດຈຳນວນການປະກົດທີ່ຕໍ່າສຸດ. ຄຳພິພາກ "maxOccurs" ແລະ "minOccurs" ຈະມີຄຳພິພາກຊັ້ນສົມບັດເປັນ 1!
ຕອນນີ້,ພວກເຮົາສາມາດກໍານົດ "item" ສະມາຊິກກັນ. ສະມາຊິກນີ້ສາມາດປະກົດຢູ່ໃນ "shiporder" ສະມາຊິກຫຼາຍຄັ້ງ. ນີ້ແມ່ນຜ່ານດ້ວຍການກຳນົດຄຳພິພາກສະມາຊິກ "maxOccurs" ຂອງ "item" ສະມາຊິກໃຫ້ເປັນ "unbounded", ດັ່ງນັ້ນ "item" ສະມາຊິກສາມາດປະກົດຢູ່ຫຼາຍຄັ້ງຫຼາຍກວ່າທີ່ຜູ້ສ້າງຕ້ອງການ. ບໍ່ວ່າ, "note" ສະມາຊິກແມ່ນສິ່ງທີ່ພິການ. ພວກເຮົາໄດ້ກຳນົດຄຳພິພາກ "minOccurs" ຂອງສະມາຊິກນີ້ໃຫ້ເປັນ 0:
<xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element>
ຕອນນີ້ພວກເຮົາສາມາດກໍານົດລັກສະນະຂອງປະກອນ "shiporder" ໄດ້. ຍ້ອນວ່ານັ້ນເປັນລັກສະນະທີ່ຕ້ອງມີ ພວກເຮົາປະກອບ use="required".
ບັນທືກ:ການກໍານົດຂອງລັກສະນະນັ້ນຕ້ອງຖືກວາງຢູ່ສຸດທ້າຍ:
<xs:attribute name="orderid" type="xs:string" use="required"/>
ນີ້ແມ່ນທີ່ວິດີໂອຂອງເອກະສານ schema ທີ່ມີຊື່ "shiporder.xsd":
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element name="orderperson" type="xs:string"/> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema>
ການຕັດແຍກ Schema
ວິທີການອອກແບບທີ່ກ່ອນໜ້ານັ້ນງ່າຍດາຍຫຼາຍ ແຕ່ວ່າເວລາທີ່ເອກະສານຫຼາຍຫຼາຍກໍ່ບໍ່ງ່າຍອ່ານ ແລະ ບໍ່ງ່າຍຈັດການສະຖານະ.
ວິທີການອອກແບບທີ່ຈະນັດຕໍ່ມາອີງວ່າມີການກໍານົດຂອງປະກອບປະກອນ ແລະ ລັກສະນະຂອງພວກມັນກ່ອນ ແລະ ຕໍ່ມານັບໃສ່ການໃຊ້ລັກສະນະ ref ເພື່ອອ້າງຕົວພວກມັນ.
ນີ້ແມ່ນເອກະສານ schema ທີ່ອອກແບບດ້ວຍວິທີການໃໝ່:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- 简易元素的定义 --> <xs:element name="orderperson" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> <!-- ການອອກແບບບັນດາບັນດາບາງ --> <xs:attribute name="orderid" type="xs:string"/> <!-- ການອອກແບບສະບາຍການປະກອບສ່ວນ --> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="address"/> <xs:element ref="city"/> <xs:element ref="country"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item"> <xs:complexType> <xs:sequence> <xs:element ref="title"/> <xs:element ref="note" minOccurs="0"/> <xs:element ref="quantity"/> <xs:element ref="price"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element ref="orderperson"/> <xs:element ref="shipto"/> <xs:element ref="item" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="orderid" use="required"/> </xs:complexType> </xs:element> </xs:schema>
ນຳໃຊ້ປະເພດທີ່ອອກຈາກຊະນິດ (Named Types)
ວິທີທີສາມອອກແບບຈະສ້າງຊະນິດຫຼືປະເພດເພື່ອໃຫ້ພວກເຮົາມີຄວາມສາມາດນຳໃຊ້ບົດບາດຄວາມຈິງອີກ. ວິທີທີ່ຈະດຳເນີນງານຈະເປັນ: ກ່ອນເວລາຈະອອກຊະນິດສະບາຍຫຼືສະບາຍການປະກອບສ່ວນ, ແລະຫຼັງຈາກນັ້ນຈະໃຊ້ບັດຕາ type ຂອງບັນດາບົດບາດເພື່ອຍິນຍັງພວກມັນ.
ນີ້ແມ່ນເອກະສານ schema ທີ່ອອກແບບໂດຍວິທີທີສາມ ("shiporder.xsd"):
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/> </xs:simpleType> <xs:simpleType name="inttype"> <xs:restriction base="xs:positiveInteger"/> </xs:simpleType> <xs:simpleType name="dectype"> <xs:restriction base="xs:decimal"/> </xs:simpleType> <xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType> <xs:complexType name="shiptotype"> <xs:sequence> <xs:element name="name" type="stringtype"/> <xs:element name="address" type="stringtype"/> <xs:element name="city" type="stringtype"/> <xs:element name="country" type="stringtype"/> </xs:sequence> </xs:complexType> <xs:complexType name="itemtype"> <xs:sequence> <xs:element name="title" type="stringtype"/> <xs:element name="note" type="stringtype" minOccurs="0"/> <xs:element name="quantity" type="inttype"/> <xs:element name="price" type="dectype"/> </xs:sequence> </xs:complexType> <xs:complexType name="shipordertype"> <xs:sequence> <xs:element name="orderperson" type="stringtype"/> <xs:element name="shipto" type="shiptotype"/> <xs:element name="item" maxOccurs="unbounded" type="itemtype"/> </xs:sequence> <xs:attribute name="orderid" type="orderidtype" use="required"/> </xs:complexType> <xs:element name="shiporder" type="shipordertype"/> </xs:schema>
ປະກອບສັນ 'restriction' ສະແດງວ່າຊະນິດຂອງຂໍ້ມູນຍັງເປັນຊະນິດຂອງ W3C XML Schema Name Space. ດັ່ງນັ້ນກໍ່ແມ່ນບາງຢ່າງທີ່ລະບຸວ່າຄູ່ມູນຂອງປະກອບຫຼືປະກອບປະສົມຕ້ອງເປັນຄູ່ມູນຮູບແບບສະເພາະທີ່ແມ່ນຕາມຊະນິດສະເພາະນີ້:
<xs:restriction base="xs:string">
restriction ປ່ວຍຈະນຳໃຊ້ເພື່ອຕັດສິນຫົວຂໍ້. ໄດ້ຍັງພົບກັບບົດສະຫຼຸບຈາກ schema ທີ່ກ່າວກ່າວກ່າວກ່າວກ່າວກ່າວກ່າວ.
<xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType>
ວິທີຂອງການສະແດງ, ຕົວປ່ວຍຫົວຂໍ້ຫຼືການຕັດສິນທີ່ຕ້ອງແມ່ນຂໍ້ມູນ, ແລະຕ້ອງແມ່ນຫົກສິບວັດສະວັດ, ແລະວັດສະວັດຕ້ອງແມ່ນຕາມ 0-9.
- ຫນຶ່ງກ່ອນ XSD ປ່ວຍສົງພັນ
- ຫນ້ານັ້ນ XSD ປະກອບຂໍ້ມູນ