Home  >  Article  >  Backend Development  >  XML document type declaration

XML document type declaration

PHPz
PHPzOriginal
2017-04-02 11:52:092175browse

Since XML can customize tags, the set of tags defined by each person will be different. If there is no set of standards to stipulate the definition principles of tags, the application cannot process XML documents. The solution to this problem is DTD, DTD (Document Type Definition, document type definition), which is used to define the writing rules of XML documents. Such as which elements can appear in the document, and the content and attribute requirements of the elements, etc. The application will use this DTD to check the document. The XML document that conforms to the DTD constraint rules is called a valid document and can be processed in the next step. Otherwise, an error will be reported. The application can capture the error and perform corresponding Exception handling. The verification process is optional and depends on the specific application.

Document type declaration
To use DTD for validity testing, you must use the document type definition declaration to specify the DTD. For example:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE portal SYSTEM >
<portal> 
<name>Jims</name> 
<email>Jims@163.com</email> 
<email>Jims@21cn.com</email>
</portal>

The document type declaration is located after the XML declaration and before the root element. If the dtd file is located on the local machine, the path name can be used to directly indicate the location of the dtd file. The content of portal.dtd is as follows:

<!ELEMENT portal (name,email*)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>

The above content can also be written directly into the XML document. This dtd declaration method is called an internal dtd subset, such as:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE portal [<!ELEMENT portal (name,email*)><!ELEMENT name (#PCDATA)><!ELEMENT email (#PCDATA)>]>
<portal> 
<name>Jims</name> 
<email>Jims@163.com</email> 
<email>Jims@21cn.com</email>
</portal>

If the dtd is located outside the XML document, it is called an external dtd subset. We can combine internal and external DTDs to form a DTD to verify XML documents. For example:

<!DOCTYPE portal SYSTEM "external.dtd" [<!ELEMENT portal (name,email*)><!ELEMENT name (#PCDATA)><!ELEMENT email (#PCDATA)>]>

Note that when using internal and external DTDs, the two DTDs must be compatible with each other and cannot conflict.

The above is the detailed content of XML document type declaration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn