Both XML schema and DTD are used for document verification, but there are some differences between them. The former is more accurate and flexible:
DTD
DTD does not comply with XML syntax
The DTD cannot be expanded.
DTD does not support namespace applications
The DTD does not provide powerful data type support and can only represent simple data types.
Schema
Schema is fully based on XML syntax
Can be processed using tools for processing XML documents
Greatly expanded data types
Supports prototype, that is, element inheritance.
Support attribute groups
Open, multiple schemas can be applied to an XML document
1. xml validity Verification |
One of the main objectives of XML technology is to facilitate information exchange. Obviously, this goal can be achieved only when the format or structure of the XML document is unanimously recognized by all parties involved in the interaction. XML Schema (outline) and DTD are the models used to describe the information structure. They can be used to verify the validity of XML documents. |
Schema and DTD specify the elements and attributes that can be used in the document, as well as possible combinations of these objects and text content. As a standard for document verification, schema and DTD should not only be understood, but also be easy to understand for applications. There are two ways to verify the validity of a document: one is to verify the syntax structure, that is, validity of the object itself and its structure, such as the element type, element nesting format, attribute type, attribute value data type, and whether the attribute value is optional. On the other hand, it is the semantic verification, only by truly understanding the meaning of an XML document can information interaction be realized. Otherwise, only information with correct structure but fuzzy meaning is useless to the recipient, sometimes there may be misunderstandings, which play the opposite role. |
Perform the following steps to verify the XML document: |
1. Define an XML schema or DTD: schema or DTD defines structural standards for all documents of the same type. For example, documents related to book information can share a standard. |
2. Apply schema or DTD: the schema is generally specified by the author of the document, so that the recipient can quickly verify the document according to the instructions. However, in the C/S (Client/Server) environment, the server cannot determine whether XML has been verified on the client. Therefore, to ensure the verification process and avoid possible repeated verification, the server performs schema or DTD application operations on the document. |
3. Run the parser for verification: the parser scans the structure while loading the document, terminates the loading process if any error occurs, and responds appropriately. |
2. Differences between Schema and DTD |
Although both XML schema and DTD are used for document verification, there are some differences between them. The former is more accurate and flexible: |
● XML schema is an open model with high scalability and functionality. DTD is a closed model with poor scalability; |
● XML Schema supports a wide range of data types to fully meet the needs of network applications, especially e-commerce. DTD does not support element data types, and attribute types are limited; |
● XML Schema supports the namespace mechanism, while DTD does not; |
● XML schema can be used to verify the entire XML document or document in different regions, but DTD lacks the flexibility; |
● The syntax of XML Schema fully complies with XML specifications and can be used in combination with DOM technology. It has powerful functions, while the DTD syntax is self-contained and difficult to learn. |
Therefore, schema is likely to replace DTD as a new Standard for verifying the legitimacy of XML documents. |
3. Basic Elements of Schema |
Schema has eight elements: <schema>, <elementtype>, <element>, <group>, <attributetype>, <attribute>, <datatype>, and <description>. These elements define the syntax and structure allowed in XML. Note: These elements must be defined before schema is used. |
The schema file is very similar to other XML documents. The root element is <schema>, indicating the document type: |
<Schema name = "myschema" |
Xmlns = "urn: Schemas-Microsoft-com: XML-Data" |
Xmlns: dt = "urn: Schemas-Microsoft-com: datatypes"> |
The attribute name specifies the schema name and xmlns specifies the namespace. The first xmlns specifies the default namespace for the full text, and the second defines the namespace of the data types available in the document. |
To reference schema in XML, specify the URL address of the schema file by using the namespace in the root element of the XML file. prefix "X-schema" before the URL: |
<Myelement xmlns = "X-schema: http://mysite.com/myschema.xml"> |
If you use xmlns to reference schema in an element of an XML document, it is to verify the local legitimacy of the document. |
<Elementtype> elements are used to declare new elements used in XML documents: |
Name = "element tag name" |
Content = "empty" | "textonly" | "eltonly" | "mixed" |
Model = "open" | "closed" |
Order = "one" | "seq" | "sequence" |
DT: TYPE = "XML data type"> |
The content attribute specifies whether the declared element is null, whether it contains text or child elements, or both. Order indicates the arrangement order of the child elements; model specifies whether an element can contain elements or attributes not defined in the schema. DT: Type specifies the Data Type of the element. |
The <element> element defines the content of the elements declared by <elementtype>, indicating which child elements can be used in the specified Element type: |
Type = "declared element type" |
The Type attribute is necessary and must be consistent with the name attribute in <elementtype>. The minoccurs and maxoccurs attributes specify the minimum and maximum number of occurrences of an element. The default value is 1. If minoccurs is 0, the description element is optional. If maxoccurs is *, the number of times that the description element appears is not limited. |
<Group> elements also appear in <elementtype> to express the concept of element grouping. Objects in a group can be elements or child groups. The Order attribute specifies the order of elements in the group: |
<Elementtype name = "..."> |
<Group minoccurs = "0" | "1" |
Order = "one" | "seq" | "sequence"> |
5. <attributetype> element |
The <attributetype> element defines the attribute types used in the schema. The function range varies depending on the location where the image appears. If it is defined in the <elementtype> element, the defined attribute type can only be applied to the element type. If it is defined outside, this attribute can be used for all element types in the document. |
DT: TYPE = "XML data type" |
DT: Values = "enumerated list of values" |
Default = "Default Value"> |
Name is the name of the property type. DT: type is the same as the usage in the <elementtype> element. Required specifies whether this property is necessary for the element that references it. DT: values only applies when DT: this parameter is valid only when type is "enumeration". In this case, it lists all possible values of the attribute. Default specifies the default value of the attribute type and must be a valid value. For example, if the attribute type is Enumeration type, the default value must be one of the values listed in DT: values. |
The <attribute> element defines the attributes declared by <attributetype>: |
Type = "declared attribute type" |
Default = "Default Value"/> |
The Type attribute uniquely specifies the referenced property type. The value must be exactly the same as the name attribute in <attributetype>. Required indicates whether the attribute is necessary for the element that references it; default specifies the default value of the property type. If the default and required attributes corresponding to <attributetype> and <attribute> of the same attribute are defined, the priority level of <attribute> is higher, and the value of this attribute prevails. |
The other two elements <datatype> and <description> define the data type of the schema element and provide instructions for the element. The usage is simple and will not be described in detail in this article. |