SoFunction
Updated on 2025-04-07

A simple example of XML Schema

We can see that the syntax of DTD is quite complex, and it does not comply with the standards of XML files and forms its own system. In other words, the DTD document itself is not a good form of XML document. The above introduction to DTD is just an introduction, aiming to help everyone understand DTD files and create simple DTD files when necessary, because many XML applications are now built on DTD.



One other replacement for DTD is the W3C-defined Schema. In a literal sense, Schema can be translated into patterns, outlines, plans, planning, etc. Its basic meaning is to create a pattern for XML documents.

The obvious benefit of Schema over DTD is that the XML Schema document itself is an XML document, rather than using its own syntax like DTD. This makes it convenient for users and developers, as the same tools can be used to handle 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 has not been improved, but the adoption of Schema has become a trend in the development of XML.


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

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

<Book>
<Name>Tianya Mingyue Knife
<Author>Gu Long

If you use DTD to define the XML document structure, it can be as follows:


<!ELEMENT Book (name, author)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Author (#PCDATA)>

So how to define it in the form of Schema? See the following code:

<element name='book' type='book type'/>
<complexType name='Book Type'>
<element name='name' type='string'/>
<element name='author' type='string'/>
</complexType>

It can be noted that in Schema, the definition of the entire document nature and content is also achieved through the definition of elements and the definition of element relationships. It should also be noted that in Schema, an element is determined by its name and content model. The name is the name of the element. Everyone can understand this, and the content model actually represents the type of the element. Just like in C++, we can define any variable, but we must define the type of the variable. The type of the variable may have many forms. It can be a simple variable (such as the type specified in C++, bool, int, double, char, etc.), or a very complex type (such as a struct or class). The same is true in Schema. Types (types) can be divided into two forms, one is a very simple type, called simple, and the other is a complex type, called complex. Simple types cannot contain elements and attributes (note that in Schema and DTD, there are statements about element attributes, and the avenues are the same). Complex types can not only contain attributes, but also nest other elements therein, or can be associated with attributes in other elements.