Home > Article > Backend Development > A brief introduction to XML Schema
XML Schema is the same as DTD (DTD syntax overview). It also constrains a type of XML document and determines its structure, elements, attributes, and type of data. And the constraints on the elements, entities, attributes of elements, and relationships between elements and entities used in XML documents. XML Schema was first proposed by Microsoft and has been accepted by W3C as a standard. Different from DTD, XML Schema files use XML syntax. Its design purposes are many similar to DTD, but it surpasses DTD in terms of functionality and extensibility. Let’s talk about some of the differences between them:
1. XML Schema is an XML document, unlike DTD which has its own unique syntax. For developers, you don't need to know both syntaxes to write a well-formed XML document. For developing XML parsers, since XML Schema is also XML syntax, it is more convenient to implement and support. At the same time, XML Schema inherits the extensible advantages of XML.
2. Define the data type. In a DTD file, data can only be declared as string type or sub-elements of string type, such as PCDATA, CDATA, ID, etc. In XML Schema, you can define the same rich data types as Programming languages, such as integers, Floating point types, Boolean types, date types, etc. The benefits of this are obvious. When you write a program to use an integer data, if it is defined with DTD, you must convert it from character type to integer type, and XML Schema can be directly defined as Integer type.
3. XML Schema is an open model. For example, the following XML document:
<item> <name>TG/DTLatte</name> <quantity>1</quantity> <price>2.00</price> </item>
The following is its DTD and Schema description:
DTD:
<!ELEMENT name (#PCDATA)> <!ELEMENT quantity (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ELEMENT item (name,quantity,price)>
schema
<ElementType name="name"/> <ElementType name="quantity" dt:type="int"/>> <ElementType name="price" dt:type="fixed.14.4"/> <ElementType name="item" model="open"> <element type="name"/> <element type="quantity"/> <element type="price"/> </ElementType>
When the above XML Adding a 50fd4fbe00dbd22c492052e666d4196f10:21 PDT8d4a8a2a17471cc2a44f0c023147b0ca element to the document becomes:
<item xmlns:myItm="urn:myItems"> <name>TG/DT Latte</name> <quantity>1</quantity> <price>2.00</price> <myItem:time>10:21 PDT</myItem:time> </item>
The above DTD will cause a validation error, but the Schema will not.
Four.Integration of namespace. An XML document can only be described by one DTD document, but can be described by multiple XML Schema documents. The latter fully supports namespaces.
The above is the detailed content of A brief introduction to XML Schema. For more information, please follow other related articles on the PHP Chinese website!