XSD <any> element

element <any> ສາມາດສະແຫຼງຄວາມສາມາດຂອງພວກເຮົາໃນການຂະຫຍາຍ XML document ຜ່ານ element ທີ່ບໍ່ໄດ້ກຳນົດໃນ schema!

element <any>

element <any> ສາມາດສະແຫຼງຄວາມສາມາດຂອງພວກເຮົາໃນການຂະຫຍາຍ XML document ຜ່ານ element ທີ່ບໍ່ໄດ້ກຳນົດໃນ schema!

ບົດຢ່າງນີ້ເປັນສິ່ງທີ່ຂຶ້ນຈາກ XML schema ທີ່ຊື່ "family.xsd". ມັນສະແດງວ່າມີການສະແດງ "person" element. ຜ່ານການໃຊ້ element <any>, ພວກເຮົາສາມາດຂະຫຍາຍ ຂອບເຂດ "person" ຜ່ານ element ທີ່ບໍ່ມີການກຳນົດໃນ schema: <any> element

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
      <xs:any minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

ຕອນນີ້ພວກເຮົາຕ້ອງການໃຊ້ປະກອບສ່ວນ "children" ເພື່ອຂະຫຍາຍ "person" ປະກອບສ່ວນ. ໃນກໍລະນີນີ້ພວກເຮົາສາມາດເຮັດແນວນັ້ນໄດ້ຖ້າຜູ້ຂຽນ schema ກ່ອນໜ້ານີ້ບໍ່ໄດ້ຖະແຫຼງ "children" ປະກອບສ່ວນ.

ບັນທຶກ schema ນີ້ເຫັນຢູ່ນັ້ນ,ຊື່ວ່າ "children.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.codew3c.com"
xmlns="http://www.codew3c.com"
elementFormDefault="qualified">
<xs:element name="children">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="childname" type="xs:string"
      maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

ພາຍໃນເອກະສານ XML ນີ້ (ຊື່ວ່າ "Myfamily.xml"),ໃຊ້ປະກອບສ່ວນຈາກ schema ສອງບ່ອນແຕກຕ່າງກັນ,"family.xsd" ແລະ "children.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.codew3c.com children.xsd">
<person>
<firstname>David</firstname>
<lastname>Smith</lastname>
<children>
  <childname>mike</childname>
</children>
</person>
<person>
<firstname>Tony</firstname>
<lastname>Smith</lastname>
</person>
</persons>

The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element by adding optional elements after the "lastname" element.

<any> and <anyAttribute> can both be used to create extensible documents! They make the document capable of including additional elements that are not declared in the main XML schema.