一个 XSD 实例
ບົດນີ້ຈະສະແດງໃຫ້ທ່ານຮູ້ວິທີການຂຽນ XML Schema. ທ່ານຈະຮຽນວ່າຈະມີຫຼາຍວິທີການຂຽນ schema.
XML document
ພວກເຮົາຈະເບິ່ງ XML document ທີ່ມີຊື່ "shiporder.xml":
<?xml version="1.0" encoding="ISO-8859-1"?> <shiporder orderid="889923"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> <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 ທີ່ກ່ຽວກັບ root element "shiporder" ທີ່ບັນຈຸ attribute "orderid". element "shiporder" ບັນຈຸສາມ element: "orderperson", "shipto" ແລະ "item". element "item" ມີສອງຄັ້ງ ທີ່ບັນຈຸ "title", element "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 ທີ່ນີ້,ພວກເຮົາໄດ້ໃຊ້ອາກາດມາດຕະຖານ (xs), ອາກາດນີ້ຈະກ່ຽວຂ້ອງກັບ Schema language definition, ອາກາດມີຄຳນວນເປັນ http://www.w3.org/2001/XMLSchema.
ຕໍ່ມາພວກເຮົາຕ້ອງຈັດ "shiporder" ສະມາຊິກ. ສະມາຊິກນີ້ມີປະສົມປະສານດຽວທີ່ກວມມີສະມາຊິກອື່ນໆ, ຖ້ານັ້ນພວກເຮົາຈະຮັບມັນຄືກັບຮູບແບບກາງ. ສະມາຊິກລູກ "shiporder" ຈະຖືກກັບກວມໂດຍ "xs:sequence" ທີ່ກໍານົດອຳນາດການປະກົດຂອງສະມາຊິກ:
<xs:element name="shiporder"> <xs:complexType> <xs:sequence> ... ... </xs:sequence> ... </xs:complexType> </xs:element>
ຕໍ່ມາພວກເຮົາຕ້ອງຈັດ "orderperson" ສະມາຊິກໃຫ້ເປັນມີຮູບແບບລະອຽດ (ຍ້ອນວ່າມັນບໍ່ມີລະບົບຫຼືສະມາຊິກອື່ນໆ). ສະໜາມ "xs:string" ຈະມີຫົວຫຼັກຈາກອາກາດຫົວຫຼັກ, ອາກາດນີ້ຈະກ່ຽວຂ້ອງກັບ XML schema ທີ່ສະແດງອາກາດຂອງ schema ທີ່ໄດ້ກໍານົດຄວາມຈໍາຕັດ XML:
<xs:element name="orderperson" type="xs:string"/>
ຕໍ່ມາພວກເຮົາຕ້ອງຈັດ "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" ສະມາຊິກຢູ່. ສະມາຊິກ "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 ປະກອບມີການສະແດງວ່າປະເພດຂໍ້ມູນຂອງຂໍ້ມູນຈາກ namespace W3C XML Schema. ດັ່ງນັ້ນ ຂໍ້ມູນທີ່ກ່ຽວກັບປະກອບຫລືອຸປະກອນຈະໄດ້ເປັນຂໍ້ມູນຮູບແບບຂອງການຕາມການຂອງຂໍ້ມູນສາຍ.
<xs:restriction base="xs:string">
restriction ປະກອບມີການໃຊ້ຫລາຍກວ່າເພື່ອການວາງຄວາມຈຳກັດໃຫ້ປະກອບ. ໄວ້ບັນທຶກກ່າວລາວກ່າວກ່ຽວກັບ schema ທີ່ມາຈາກຂໍ້ມູນທີ່ກ່າວກ່ຽວກັບຂໍ້ມູນທີ່ກ່ຽວກັບປະເພດຂໍ້ມູນ W3C XML Schema namespace.
<xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType>
这段代码指示出,元素或属性的值必须为字符串,并且必须是连续的六个字符,同时这些字符必须是 0-9 的数字。