Home  >  Article  >  Backend Development  >  Detailed explanation of a simple XML Schema sample code

Detailed explanation of a simple XML Schema sample code

黄舟
黄舟Original
2017-03-07 16:36:211408browse

We can see that the syntax of DTD is quite complex, and it does not meet the standards of xml files and forms a system of its own. In other words, the DTD document itself is not a well-formed XML document. The above introduction to DTD is only an introduction. The purpose is to help everyone understand DTD files and create simple DTD files when necessary, because now Many XML applications are built on DTD.



Another alternative to DTD is Schema defined by W3C. Schema can literally be translated into patterns, outlines, plans, plans, etc. Its basic meaning is to develop a schema for XML documents.

The obvious advantage of Schema over DTD is that the XML Schema document itself is also an XML document, rather than using a self-contained syntax like DTD. This is convenient for users and developers because the same tools can be used to process XML Schema and other XML information, without having to use special tools specifically for Schema. Schema is simple and easy to understand, and anyone who understands XML syntax and rules can understand it immediately. The concept of Schema has been proposed for a long time, but the W3C standard has only recently come out, and the corresponding application support is not yet complete. However, the adoption of Schema has become a trend in XML development.


First of all, let’s start with the simplest example to learn the grammatical structure of Schema:

For example, a simple XML document is as follows:

<书本> 
<名称>天涯明月刀 
<作者>古龙 
  

If the XML document structure is defined in the form of DTD, it can be as follows:

<!ELEMENT 书本 (名称, 作者)> 
<!ELEMENT 名称 (#PCDATA)> 
<!ELEMENT 作者 (#PCDATA)>
那么用Schema形式如何定义呢?见下面的代码:
<element name=&#39;书本&#39; type=&#39;书本类型&#39;/> 
<complexType name=&#39;书本类型&#39;> 
<element name=&#39;名称&#39; type=&#39;string&#39;/> 
<element name=&#39;作者&#39; type=&#39;string&#39;/> 
</complexType>

One thing to note is that in Schema, the entire structure is also realized through the definition of elements and the definition of element relationships. Definition of the nature and content of the document. At the same time, it should be noted that in Schema, an element is determined by its name and content model. The name is the name of the element, which everyone can understand, and the content model actually represents the type of the element. Just like in C++, we can define a variable at will, but the type of the variable must be defined. The type of the variable may have many forms. It can be a simple variable (such as the type specified internally in C++, bool, int, double , char, etc.), or it can be a very complex type (such as a struct or class). The same is true in Schema. Type (type) can be divided into two forms. One is a very simple type, which is called For simple, there is a complex type, called complex. Simple types cannot contain elements and attributes (note that in Schema and DTD, there are elements and attributes, which are the same). Complex types can not only contain attributes, but can also have other elements nested within them, or can be associated with attributes in other elements.

The above is a detailed explanation of a simple XML Schema sample code. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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