XML easy learning Manual (iv): XML syntax

Source: Internet
Author: User


Chapter 4 XML syntax

Outline:

I. XML syntax rules
Ii. Element syntax
3. annotation syntax
Iv. CDATA syntax
V. namespaces syntax
6. entity syntax
VII. DTD syntax

Based on the previous three chapters, we have learned about 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 document and the original htmlCodeSimilarly, content is identified by an identifier. To create an XML document, follow these important rules:
Rule 1: an XML declaration statement is required.
We mentioned this in the previous chapter. Declaration is the first sentence of the XML document. Its format is as follows:

The purpose of the statement is to tell the browser or other processingProgram: This document is an XML document. The version in the declaration statement indicates the version of the XML specification that the document complies with; standalone indicates whether the document has a DTD file, and if so, the parameter is no; encoding indicates the language encoding used by the document, which is the UTF-8 by default.
Rule 2: whether a DTD file exists
If the document is a "valid XML document" (see the previous chapter), 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 format:

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 usually 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 used by the document, 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. For example, Ajie is incorrect.
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 correctly interpreted by the browser.
However, in XML, it is stipulated that all attribute values must be enclosed in quotation marks (single 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 appear in pairs, such
. In XML, all identifiers must appear 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 also be disabled.
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:
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. Attributes can be used for further description.
There are no reserved words in XML, so you can use any words as element names as you like. However, the following rules 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 ...).
4. The name cannot contain spaces.
5. The name cannot contain ":" (colon)
To make the elements easier to read, understand, and operate, we also have some suggestions:
1. Do not use "." in the name ".". In many programming languages, "." is an attribute of an object, for example, Font. color. For the same reason, "-" is not recommended. It must be used instead;
2. The name should be as short as possible.
3. The name should be case-insensitive.
4. The name can contain non-English characters, such as Chinese characters. However, some software may not. (Ie5 currently supports Chinese elements .)
In addition, add a description of attributes. In HTML, attributes can be used to define the display format of elements. For example, word will display word in red. In XML, attributes only describe the identifier and are irrelevant to the display of the 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 use of CSS or XSL, which will be detailed below .)
3. annotation syntax

Annotations are added to the XML document for ease of reading and understanding. They are not interpreted 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 maintain, 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]>
The content displayed on the page will be "Ajie"
V. namespaces syntax

Translate namespaces into namespace. What is the role of namespace? When we use another person's or multiple DTD files in an XML document, the conflict arises: Because the identifiers in XML are created by ourselves, in different DTD files, the identifiers may have the same name but different meanings, which may cause data confusion.
For example, in a document <Table> wood table </table>, <Table> indicates a table,
In another document <Table> namelist </table>, <Table> indicates a table. If I need to process these two documents at the same time, a name conflict will occur.
To solve this problem, we introduced the namespaces concept. Namespaces identifies the same names by adding a URL.
Namespaces must also be declared at the beginning of the XML document. The declared syntax is as follows:
<Document xmlns: yourname = 'url'>
Yourname is the namespaces name defined by you, and URL is the URL of the namespace.
Assuming that the above "Table <Table>" document comes from the http://www.zhuozi.com, we can declare
<Document xmlns: zhuozi = 'HTTP: // www.zhuozi.com '>
Then use the defined namespace in the following identifiers:
<Zhuozi: Table> wood table </table>
In this way, the two <Table> are distinguished. Note: setting a URL does not mean that the ID is to be read from that URL. It is just a different sign.
6. entity syntax

Entity is translated as "entity ". Its function is similar to the "macro" in the word. It can also be understood as the touchpad in DW. You can define an entity in advance and call it multiple times in a document, or call the same entity in multiple documents.
Entity can contain characters, text, etc. The benefit of using entity is: 1. It can reduce errors. You only need to enter multiple identical parts in the document once. 2. It improves maintenance efficiency. For example, if you have 40 documents that contain the copyright entity, you only need to modify the originally defined entity statement without modifying all the files if you need to modify the copyright.
XML defines two types of entity. One is the common entity mentioned here, which is used in the XML document; the other is the parameter entity, which is used in the DTD file.
The definition Syntax of entity is:
<! Doctype filename [
<! Entity entity-name "entity-content"
]
>
For example, I want to define a piece of copyright information:
<! Doctype copyright [
<! Entity copyright "Copyright 2001, Ajie. All rights reserved"
]
>
If the content of my copyright information shares an XML file with others, you can also use an external call method. The syntax is as follows:
<! Doctype copyright [
<! Entity copyright system "http://www.sample.com/copyright.xml">
]
>
The reference syntax of the defined entity in the document is: & entity-name;
For example, the copyright information defined above is written as: & copyright;
The complete example is as follows. You can copy and save it as a copyright. xml viewing instance:
<? XML version = "1.0" encoding = "gb2312"?>
<! Doctype copyright [
<! Entity copyright "Copyright 2001, Ajie. All rights reserved">
]>
<Myfile>
<Title> XML </title>
<Author> Ajie </author>
<Email> ajie@aolhoo.com </Email>
<Date> 20010115 </date>
& Copyright;
</Myfile>
V. namespaces syntax

Translate namespaces into namespace. What is the role of namespace? When we use another person's or multiple DTD files in an XML document, the conflict arises: Because the identifiers in XML are created by ourselves, in different DTD files, the identifiers may have the same name but different meanings, which may cause data confusion.
For example, in a document <Table> wood table </table>, <Table> indicates a table,
In another document <Table> namelist </table>, <Table> indicates a table. If I need to process these two documents at the same time, a name conflict will occur.
To solve this problem, we introduced the namespaces concept. Namespaces identifies the same names by adding a URL.
Namespaces must also be declared at the beginning of the XML document. The declared syntax is as follows:
<Document xmlns: yourname = 'url'>
Yourname is the namespaces name defined by you, and URL is the URL of the namespace.
Assuming that the above "Table <Table>" document comes from the http://www.zhuozi.com, we can declare
<Document xmlns: zhuozi = 'HTTP: // www.zhuozi.com '>
Then use the defined namespace in the following identifiers:
<Zhuozi: Table> wood table </table>
In this way, the two <Table> are distinguished. Note: setting a URL does not mean that the ID is to be read from that URL. It is just a different sign.
6. entity syntax

Entity is translated as "entity ". Its function is similar to the "macro" in the word. It can also be understood as the touchpad in DW. You can define an entity in advance and call it multiple times in a document, or call the same entity in multiple documents.
Entity can contain characters, text, etc. The benefit of using entity is: 1. It can reduce errors. You only need to enter multiple identical parts in the document once. 2. It improves maintenance efficiency. For example, if you have 40 documents that contain the copyright entity, you only need to modify the originally defined entity statement without modifying all the files if you need to modify the copyright.
XML defines two types of entity. One is the common entity mentioned here, which is used in the XML document; the other is the parameter entity, which is used in the DTD file.
The definition Syntax of entity is:
<! Doctype filename [
<! Entity entity-name "entity-content"
]
>
For example, I want to define a piece of copyright information:
<! Doctype copyright [
<! Entity copyright "Copyright 2001, Ajie. All rights reserved"
]
>
If the content of my copyright information shares an XML file with others, you can also use an external call method. The syntax is as follows:
<! Doctype copyright [
<! Entity copyright system "http://www.sample.com/copyright.xml">
]
>
The reference syntax of the defined entity in the document is: & entity-name;
For example, the copyright information defined above is written as: & copyright;
The complete example is as follows. You can copy and save it as a copyright. xml viewing instance:
<? XML version = "1.0" encoding = "gb2312"?>
<! Doctype copyright [
<! Entity copyright "Copyright 2001, Ajie. All rights reserved">
]>
<Myfile>
<Title> XML </title>
<Author> Ajie </author>
<Email> ajie@aolhoo.com </Email>
<Date> 20010115 </date>
& Copyright;
</Myfile>
(Source: enet College)

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.