XML easy learning Manual (4) XML syntax

Source: Internet
Author: User
Based on the previous three chapters, we have understood what XML is, its implementation principles, and related terms. Next, we will start to learn the XML syntax and write our own XML documents. I. XML syntax rules XML documents and the original HTML

Based on the previous three chapters, we have understood what XML is, its implementation principles, and related terms. Next, we will start to learn the XML syntax and write our own XML documents.

I. XML syntax rules

The XML document is similar to the original HTML code and uses an identifier to identify the content. The creation of XML documents must follow the following important rules:

Rule 1: XML declaration statements are required

We mentioned this in the previous chapter. Declaration is the first sentence of the XML document. its structure is as follows:

The purpose of the declaration is to tell the browser or other processing programs that this document is an XML document. The version in the declaration statement represents the version of the XML specification followed by the document; standalone performance document is accompanied by a DTD file, if any, the parameter is no; encoding represents the language encoding used by the document, the default is the UTF-8.

Rule 2: whether a DTD file exists

If the document is a "valid XML document", the document must have a corresponding DTD file and strictly abide by the specifications set by the DTD file. The declaration statement of the DTD file follows the XML declaration statement in the following pattern:

Where:

"! DOCTYPE "indicates that you want to define a DOCTYPE;

"Type-of-doc" is the name of the document type, which is defined by you and is generally the same as the DTD file name;

"SYSTEM/PUBLIC" only uses one of the two parameters. SYSTEM refers to the URL of the private DTD file of the document application, while PUBLIC refers to the URL where the document calls a public dtd file.

"Dtd-name" is the URL and name of the DTD file. The suffix of all DTD files is ". dtd ".

We should use the following example to write it as follows:

Rule 3: pay attention to your case sensitivity

In XML documents, the case sensitivity is different.

And

Is different. Note that when writing elements, the front and back identifiers must be the same in case. Example: ajie, Written as ajieYes.

You 'd better develop a habit, either in upper case, or in lower case, or in upper case, the first letter. This reduces document errors caused by case-insensitive.

Rule 4: quote the property value

In HTML code, attribute values can be enclosed by quotation marks or not. For example, word and word can be accurately described by the browser.

However, in XML, it is stipulated that all attribute values must be enclosed by quotation marks (single quotation marks or double quotation marks). Otherwise, it will be regarded as an error.

Rule 5: All identifiers must have end identifiers.

In HTML, tags may not be displayed in pairs? Lt; br>. In XML, all identifiers must be presented in pairs. if there is a start identifier, there must be an end identifier. Otherwise, it is regarded as an error.

Rule 6: all empty identifiers must be closed.

An empty ID is an identifier with no content between the identifiers. For example
. In XML, all identifiers must have end Identifiers. for such empty identifiers, the XML processing method is to add/at the end of the original identifiers. For example:

Reference content is as follows:

Should be written
;
Should be written ;
Should be written

II. element syntax

An element consists of a pair of identifiers and the content. Like this: ajie. The element name is the same as the identifier name. The identifier can be further described by attributes.

In XML, there are no reserved words, so you can use any words as the element name. However, the following specifications must also be observed:

1. the name can contain letters, numbers, and other letters;

2. the name cannot start with a number or underscore;

3. the name cannot start with the letter xml (or XML or Xml ...).

4. the name cannot contain spaces.

5. the name cannot contain ":" (colon)

In order to make elements easier to read and control, we also have some suggestions:

1. do not apply "." to the name ".". In many programming languages, "." is an attribute of an object, such as font. color. For the same reason, "-" is also better not to be used. it must be used instead;

2. the name should be as short as possible.

3. the name should be case-insensitive to the same scale.

4. the name can contain non-English characters, such as Chinese characters. But some software may not support it. (IE5 currently supports Chinese elements .)

In addition, I would like to add some clarification on attributes. In HTML, attributes can be used to define the display pattern of elements. for example, word will display word in red. In XML, attributes only describe the identifiers and are irrelevant to the display of element content. For example, the word is not displayed in red. (So some netizens will ask: how can I display the text in red in XML? This requires the application of CSS or XSL, which will be described below .)

III. annotation syntax

Annotations are added to the XML document to facilitate browsing and understanding. the additional information added to the XML document will not be described by the program or displayed by the browser.

The syntax of the annotation is as follows:

As you can see, it is the same as the annotation syntax in HTML and is very easy. Developing a good habit of commenting will make your documents easier to protect, share, and look more professional.

IV. CDATA syntax

CDATA stands for character data, which is translated as character data. When writing an XML document, we sometimes need to display letters, numbers, and other symbols, such as "<". in XML, these characters have special meanings, what should we do? This requires the CDATA syntax. The syntax format is as follows:

这里放置需要显示的字符

For example:

ajie</AUTHOR>

The content displayed on the page will be "ajie"

V. Namespaces syntax

Translate Namespaces into namespace. What is the role of namespace? When we apply another or multiple DTD files in an XML document, it will present the conflict: Because the identifiers in XML are self-created, in different DTD files, the identifiers may have the same name but different meanings, which may cause Data confusion.

For example, in a document

Wood table
Medium Show the table while in another document
Namelist
Medium

Knowing this title, we have introduced the namespaces concept. Namespaces identifies the same names by adding a URL to the ID.

Namespaces must also be declared at the beginning of the XML document. the declared syntax is as follows:

Yourname is the namespaces name defined by you, and URL is the URL of the namespace.

Suppose the "table" above

Performance table. If I need to process these two documents at the same time, a name conflict will occur.

Then apply the defined namespace in the following identifiers:

Wood table

"The document comes from the http://www.zhuozi.com and we can declare it

In this way

6. entity syntax

Entity is translated as "entity ". Its function is similar to the "macro" in the word. it can also be understood as a dashboard in DW. you can define an entity in advance and then call it multiple times in a document, or call the same entity in multiple documents.

Leave the zone. Note: setting a URL does not mean that the ID is actually to be read from that URL. it is only used as a different mark.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.