Overview
To date, you have learned the basic rules of XML documents in this tutorial; that's of course, you need to define elements that will be used to represent data. You will understand two ways to do this chapter.
One method is to use document type definition, or refer to DTD. DTD definitions can appear in the XML document, the order of these elements, how can they nescence and other details of the XML document structure. DTD is part of the initial XML specification and is very similar to SGML DTD. Another method is to use XML Schema. Mode can define all document structures you can use in DTD, which can also define data types and more complex rules than DTD. The W3C has developed an XML Schema specification several years after the initial XML specification.
Document type definition
DTD allows you to specify the basic structure of the XML document. The following few pages will study the DTD fragment. The first is the DTD of the basic structure of the address document example in this chapter.
What is XML?
:
This DTD defines all elements used in the sample document. It defines three elements:
element contains aAlthough DTD is quite simple, it clearly shows what kind of elementary combination is rules.
Also, please note that the DTD grammar is different from a normal XML syntax. (Instead, the XML Schema document itself is XML, which leads to some interesting results). Although the grammar of the DTD is different, you can still put a normal note into the DTD.
Symbol in DTD
Several symbols in the DTD are used to indicate how many elements may occur (or whether it appears) in the XML document. Here are some examples and its meaning:
element must include a
Before proceeding, there is a quick description of the type of XML document for flexibility. Consider the previous sample name and address document type; Obviously I have written this document for US postal address habits. If you want to define the DTD or mode of the rule of the other address type, you must add more complicated things to it. There is a
Document type definition
The sample DTD is dealt with Canada's address, but adding
Key points: To define the structure of an XML document, you should consider DTD or mode in advance as designed in the application of database modes or data structures. The more future needs you have in advance, the easier it is, the easier it is, the lower the cost. Define properties
This introductory tutorial has not studied how DTD work well in detail, but here is to introduce a more basic concept: define attributes. You can define the properties of the elements that will appear in the XML document. With DTD, you can also:
Definition What attributes are the default defined attribute defined attributes list all valid values for a given property
Suppose you want to change the DTD to make State become the properties of the
City (#pcdata)> , before we define the
#Implied can be).
To define multiple properties of the elements, write Attlist as follows:
City (#pcdata)>
This example defines both state and postal-code as the attribute of the
Finally, the DTD allows you to define the attribute default value and sufficient all valid values for properties:
City (#pcdata)>
The above example shows that it only supports address from Arizona (AZ), California (CA), Nevada (NV), Oregon (OR), Utah (UT) and Washington (WA), and default It is California. This way, you can perform a very limited data verification. Although this is a useful function, it is only a small subset of work that you can do with XML mode (see XML mode).
XML mode
With XML mode, you will have more capabilities to define what kind of XML document is valid. They have several advantages compared to DTD:
XML mode uses XML syntax. In other words, the XML mode is an XML document. This means that you can work like any other document. For example, you can write an XSLT style sheet that converts the XML schema into a web form with automatically generated JavaScript code, where JavaScript code can verify the data you entered. XML mode supports data types. Although DTD does support data types, it is obvious that these data types are developed from the perspective of publishing. The XML mode supports all raw data types in the DTD (such as types such as identification and identity reference). They also support integers, floating point, date, time, string, URL, and other data types for data processing and verification. XML mode is scalable. In addition to the data types defined in the XML schema specification, you can also create your own data type and can send new data types based on other data types. The XML model has a stronger expression ability. For example, you can define any
The following is an XML schema that matches the original name and address DTD. It adds two constraints:
xml
Version = "1.0" eNCoding = "UTF-8"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: restriction>
xsd: SimpleType>
xsd: element>
xsd: restriction>
xsd: SimpleType>
xsd: element>
xsd: schema>
Elements in definition mode
Some of the XML mode in the sample XML mode is defined in
XML element. The first two defined elements (
andName = "address"> xsd: sequence> xsd: complexType> xsd: element> As with the DTD version, XML mode example definitions
Most elements contain texts; definitions them are simple. As long as you declare a new element, then specify its data type to XSD: String: Name = "Title" type = "xsd: string" /> Define element content in the mode The sample mode defines the content of the two elements. The content of the Name = "state"> xsd: restriction> xsd: SimpleType> xsd: element> xsd: restriction> xsd: SimpleType> xsd: element> For the The introduction of the summary here is only the surface of the XML mode capabilities; this topic is discussed with all books. For this introductory article, we are enough to explain that XML mode is a very powerful and flexible way to describe what kind of XML documentation is effective.