XML easy learning Manual (4) 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:
<? XML version = "1.0" standalone = "yes/no" encoding = "UTF-8"?>
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:
<! Doctype type-of-Doc system/Public "DTD-name">
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:
<? XML version = "1.0" standalone = "no" encode = "UTF-8"?>
<! Doctype filelist System "filelist. DTD">

Rule 3: Pay attention to your case sensitivity
In XML documents, the Case sensitivity is different. <P> and <p> are different identities. Note that when writing elements, the front and back identifiers must be the same in case. For example, <author> Ajie </author> 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, <font color = Red> word </font> and <font color = "red"> word </font> 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? Lt; br>. 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. Such as <br> and . 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:
<Br> it should be written as <br/>;
<Meta name = "keywords" content = "XML, SGML, HTML"> enter <meta name = "keywords" content = "XML, SGML, HTML"/>;

Chapter 4 XML syntax

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, <font color = "red"> word </font> displays word in red. In XML, attributes only describe the identifier and are irrelevant to the display of the element content. For example, <font color = "red"> word </font> does not display word 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:

<! -- Here is the comment -->

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:

<! [CDATA [place the characters to be displayed here]>

For example:

<! [CDATA [<author sex = "female"> Ajie </author>]>

The content displayed on the page will be "<author sex =" female "> Ajie </author>"

Chapter 4 XML syntax

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 during the call? 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>
Right;
</Myfile>

Chapter 4 XML syntax

VII. DTD syntax

DTD is a mandatory file for "valid XML documents". We use a DTD file to define the rules and relationships between elements and identifiers in documents. How to Create a DTD file? Let's learn together:

1. Set Elements

An element is a basic component of an XML document. You need to define an element in the DTD and use it in the XML document. Element Definition Syntax: <! Element description (# pcdata, definition) *>

Note:

"<! Element "is an element declaration, indicating that you want to define an element;

The description following the declaration is the name of the element;

"(# Pcdata, definition) *>" is the usage rule of this element. Rules define the content that an element can contain and their relationships. The following table lists the element rules:

2. Element rule table:

Symbol

Description

Example

# Pcdata

Contains character or text data

<Myfile (# pcdata)>
Element myfile contains a text data

# Pcdata, element-name

Contains text and other child elements

<Myfile (# pcdtata, title)>
The myfile element must contain text and title child elements.

,

Sort by commas (,)

<Myfile (title, author, email)>
The myfile element must contain three sub-elements: titile, author, and email.

|

Use "|" to indicate or

<Myfile (Title | author | email)>
The myfile element must contain the title, author, or email sub-element.

Name

Only once

<Myfile (title)>
The myfile element must contain the title sub-element and can only be used once.

Name?

Once used or not used

<Myfile (title, author ?, Email?)>
The myfile element must contain the title sub-element and can only be used once. It can contain or does not contain the author and email sub-elements. However, it can only be used once.

Name +

Use at least once or multiple times

<Myfile (Title +, author ?, Email)>
The myfile element must contain the title sub-element and be used at least once. You can follow the author sub-element or do not follow it. The last element must contain the email sub-element and can only be used once.

Name *

Use once, multiple times, or not at all

<Myfile (Title *)>
The myfile element can contain one or more or no title sub-elements.

()

Set groups and can be nested

<Myfile (# pcdata | title) *>
The element myfile contains one or more text or title sub-elements.

<Myfile (Title *, author ?, Email) * | comment)>
The myfile element must contain some content, content, or comments, or multiple groups containing one, multiple, or no title sub-elements, followed by one or no author sub-elements, next is a required email subelement.

In addition, we can also define attributes for elements, because we do not recommend the use of attributes, so we will not detail them here.

Finally, let's summarize some of the content learned in the first four chapters and write a simple example containing DTD, XML, and script for readers to understand:
1. Save the following file as myfile. DTD
<! Element myfile (title, author)>
<! Element title (# pcdata)>
<! Element author (# pcdata)>

2. Create the XML document myfile. xml:
<? XML version = "1.0" encoding = "gb2312"?>
<! Doctype myfile System "myfile. DTD">
<Myfile>
<Title> XML easy learning manual </title>
<Author> Ajie </author>
</Myfile>

3.establish the HTML document myfile.html
<HTML>
<Head>
<Script language = "JavaScript" for = "window" event = "onLoad">
VaR xmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. async = "false ";
Xmldoc. Load ("myfile. xml ");
Nodes = xmldoc.doc umentelement. childnodes;
Title. innertext = nodes. Item (0). text;
Author. innertext = nodes. Item (1). text;
</SCRIPT>
<Title> call XML data in HTML </title>
</Head>
<Body bgcolor = "# ffffff">
<B> title: </B>
<Span id = "title"> </span> <br>
<B> author: </B>
<Span id = "author"> </span> <br>
</Body>
</Html>

4.use ie5.0open myfile.html with the browser to see the effect.

well, by the end of chapter 4 today, we have some basic knowledge about XML. In chapter 5, I will explain a successful example of XML application to show you the powerful functions of XML. Let's take a look at the next chapter: XML instance analysis.

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.