XML Easy Learning Manual (4) XML syntax

Source: Internet
Author: User
Tags date format define definition empty end version valid
xml| syntax

Fourth Chapter XML syntax

Outline:

I. XML syntax rules
Two. The syntax of the element
Three. Syntax for annotations
Four. CDATA syntax
Five. Namespaces's grammar
Six. Entity's grammar
Seven. Syntax for DTDs

With the previous three chapters, we have learned about what XML is, how it works, and what terminology is relevant. Then we begin to learn the syntax of XML and write our own XML document.

I. XML syntax rules

XML documents are similar to the original HTML code, and are identified to identify the content. The following important rules must be followed to create an XML document:
Rule 1: You must have an XML declaration statement
We have already mentioned this in the last chapter of our study. A declaration is the first sentence of an XML document, in the following format:
<?xml version= "1.0" standalone= "yes/no" encoding= "UTF-8"?>
The purpose of the declaration is to tell the browser or other handler that the document is an XML document. The version in the declaration statement represents the edition of the XML specification that the document complies with, standalone indicates whether the document is accompanied by a DTD file, and if so, the parameter is no;encoding the language encoding used for the document, and the default is UTF-8.

Rule 2: Do you have a DTD file
If the document is a "valid XML document" (see the previous chapter), then the document must have a corresponding DTD file, and strictly follow the specification set by the DTD file. The declaration statement for a DTD file immediately follows the XML declaration statement, in the following format:
<! DOCTYPE type-of-doc system/public "Dtd-name" >
which
"! DOCTYPE "means you want to define a DOCTYPE;
"Type-of-doc" is the name of the document type, defined by yourself, usually the same as the DTD file name;
The two parameters of "System/public" are only one. System refers to the Web address of the private DTD file used by the document, while public refers to the URL of the document calling a common DTD file.
"Dtd-name" is the URL and name of the DTD file. The suffix for all DTD files is ". DTD".
Let's use the example above to write this:
<?xml version= "1.0" standalone= "no" encode= "UTF-8"?>
<! DOCTYPE filelist SYSTEM "FILELIST.DTD" >

Rule 3: Pay attention to your case
In an XML document, there is a difference in capitalization. <P> and <p> are different identities. Note that when you write an element, the identity case should remain the same. For example: <author>ajie</author>, written in <Author>ajie</author> is wrong.
You'd better develop a habit, or all caps, or all lowercase, or capitalize the first letter. This can reduce the document errors that result from mismatched case mismatches.

Rule 4: Quote attribute values
In the HTML code, the attribute value can be quoted or added without. For example: <font color=red>word</font> and <font color= "Red" >word</font> can all be interpreted correctly by browsers.
In XML, however, all attribute values must be enclosed in quotes (either single or double quotes), or they will be treated as errors.

Rule 5: All identities must have corresponding end identities
In HTML, logos may not appear in pairs, more than?lt;br>. In XML, all identities must appear in pairs, with a start tag, and must have an end identity. Otherwise, it will be treated as an error.

Rule 6: All empty IDs must also be closed
An empty identity is an identity with no content between the identities. such as <br>, identification. In XML, it is necessary to specify that all identities must have an end identity, and for such an empty identifier, the method in XML is to be added at the end of the original identifier. For example:
<br> should be written as <br/>;
<meta name= "keywords" content= "XML, SGML, HTML" > should be written as <meta name= "keywords" content= "XML, SGML, HTML"/>;
should be written as


Two. The syntax of the element

An element consists of a pair of identities and the contents thereof. It's like this: Ajie. The name of the element and the name of the identity are the same. Identities can be described further with attributes.

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

1. The name may contain letters, numbers and other letters;

2. The name can not be a number or "_" (underlined) beginning;

3. Name cannot be in alphabetical XML (or XML or XML ...) Beginning

4. The name cannot contain spaces

5. Name cannot contain ":" (colon)

To make the elements easier to read and understand and manipulate, we have some suggestions:

1. Do not use "." In the name. Because in many programming languages, "." is a property of an object, such as: Font.Color. The same reason "-" is also best not to use, must be used to "_" instead;

2. Name as short as possible.

3. The name of the case as far as possible using the same standard.

4. Names can be used in non-English characters, such as in Chinese. However, some software may not be supported. (IE5 is currently supporting the Chinese element.) )

Also, add a note about the attribute. In HTML, attributes can be used to define the display format of an element, for example: <font color= "Red" >word</font> will display word as red. In XML, attributes are just descriptions of identities, regardless of the display of element content. For example, the same sentence: <font color= "Red" >word</font>, and does not display word as red. (Then, some netizens will ask: How to display text in XML in red?) This requires the use of CSS or XSL, which we'll cover in more detail below. )

Three. Syntax for annotations

Comments are for readability and understanding, additional information added to an XML document will not be interpreted by the program or displayed by the browser.

The syntax for the annotation is as follows:

<!--here is the annotation information-->

As you can see, it's the same as the annotation syntax in HTML, and it's very easy. Develop good annotation habits that will make your documents easier to maintain, share, and look more professional.

Four. CDATA syntax

CDATA The full name character data, translated as character. When we write XML documents, we sometimes need to display letters, numbers and other symbols themselves, such as "<", and in XML, these characters already have a special meaning, what do we do? This requires a CDATA syntax. The syntax format is as follows:

<! [Cdata[Place the character 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>"


Five. Namespaces's grammar

Namespaces translation into the namespace of the name. What's the role of the name space? When we use other people's or multiple DTD files in an XML document, the paradox arises: because the identities in XML are created by themselves, in different DTD files, the identity name may be the same but the meaning of the expression is different, which can cause data confusion.
For example, in a document <table>wood table</table> <table> means table,
In another document, <table>namelist</table> <table> represents the table. If I need to process both documents at the same time, a name conflict will occur.
To solve this problem, we introduced the concept of namespaces. Namespaces distinguishes these names with the same identity by positioning the identity name with a URL.
Namespaces also needs to declare at the beginning of the XML document that the syntax for the declaration is as follows:
<document xmlns:yourname= ' URL ' >
Where Yourname is the name of the namespaces that you define, the URL is the name space URL.
Assuming that the "table <table>" document above comes from http://www.zhuozi.com, we can declare
<document xmlns:zhuozi= ' http://www.zhuozi.com ' >
Then use the defined namespace in the following identity:
<zhuozi:table>wood table</table>
This separates the two <table>. Note that setting the URL does not mean that the logo is really going to the URL to read, just as a sign of a difference.

Six. Entity's grammar

Entity translated as "entity". It works like a "macro" in Word, or as a touchpad in a DW, you can define a entity in advance, call it multiple times in a document, or call the same entity in multiple documents.
Entity can contain characters, text, and so on, the advantage of using entity is: 1. It can reduce errors, and many of the same parts of the document need to be typed just once. 2. It improves maintenance efficiency. For example, you have 40 documents containing the entity of copyright, if you need to modify this copyright, do not need all the files are modified, just change the original definition of the entity statement.
XML defines two types of entity. One is the generic entity we're talking about here, used in XML documents, and the other is parameter entity, which is used in DTD files.
The definition syntax for entity is:
<! DOCTYPE FileName [
<! ENTITY entity-name "Entity-content"
]
>
For example, I want to define a section of copyright information:
<! DOCTYPE Copyright [
<! ENTITY Copyright "Copyright 2001, Ajie. All rights reserved "
]
>
If my copyright message content shares an XML file with another person, you can also use an externally invoked method, as in syntax:
<! DOCTYPE Copyright [
<! ENTITY copyright SYSTEM "Http://www.sample.com/copyright.xml" >
]
>
Defined entity the reference syntax in the document is:&entity-name;
For example, the copyright information defined above is written when invoked? Copyright;
The complete example below, you can copy down to save as Copyright.xml View 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>


Seven. Syntax for DTDs

A DTD is a necessary file for a "valid XML document" that defines the rules and interrelationships of elements and identities in a document through a DTD file. How do I create a DTD file? Let's study together:

1. Set element

Elements are an essential part of an XML document. You define an element in the DTD and use it in an XML document. The definition syntax for an element is: <! ELEMENT DESCRIPTION (#PCDATA, DEFINITION) *>

Description

"<! Element "is the declaration of elements, stating that you want to define an element;

The "DESCRIPTION" after the declaration is the name of the element;

"(#PCDATA, DEFINITION) *>" is the usage rule for that element. Rules define what elements can contain and how they relate to each other. The following table outlines the rules for the elements:

2. Element Rule table:

Symbol

Meaning

Example

#PCDATA

Contains character or text data

<myfile (#PCDATA) >
Element myfile contains a text data

#PCDATA, Element-name.

Include text and other child elements

<myfile (#PCDTATA, TITLE) >
MyFile element must contain text and title child elements

,

Separating sorting with commas

<myfile (Title,author,email) >
The myfile element must, in turn, contain titile,author,email three child elements

|

Use the "|" Indicate or

<myfile (TITLE | AUTHOR | EMAIL) >
The myfile element must contain title, or author or email child elements.

Name

Can only be used once

<myfile (TITLE) >
The myfile element must contain the title child element and can only be used once.

Name?

Use one time or do not use

<myfile (Title,author?,email?) >
The myfile element must contain the title child element and can only be used once; it may contain or not include author and email child elements, but only once if used.

name+

Use at least one or more times

<myfile (Title+,author?,email) >
The myfile element must contain the title child element and be used at least once, followed by the author child element, or not, and the email child must be included, and can only be used once.

name*

Use once, multiple times, or do not use at all

<myfile (title*) >
The myfile element can contain one, multiple, or no title child elements

( )

Set up groups, you can nest

<myfile (#PCDATA | TITLE) *>
The element myfile contains one or more text or title child elements.

<myfile ((title*, AUTHOR, EMAIL) * | COMMENT) >
MyFile elements must contain content, content, or a comment, or multiple groups that contain: one, multiple, or no title child element, followed by one or no author, followed by a required email child element.



In addition, we can also define attributes for the elements, because we do not recommend using attributes, and here we do not expand them in detail.

Finally, let's summarize some of the first four chapters and write a simple example that contains Dtd,xml and script to help readers understand:
1. Save the following file as Myfile.dtd
<! ELEMENT MyFile (title, author) >
<! ELEMENT title (#PCDATA) >
<! ELEMENT author (#PCDATA) >

2. Then create the XML document Myfile.xml:
<?xml version= "1.0" encoding= "GB2312"?>
<! DOCTYPE myfile SYSTEM "MYFILE.DTD" >
<myfile>
<title>xml Easy Learning Handbook </title>
<author>ajie</author>
</myfile>

3. Create HTML Document myfile.html
<script language= "JavaScript" for= "window" event= "onload" >
var xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
Xmldoc.async= "false";
Xmldoc.load ("Myfile.xml");
nodes = XmlDoc.documentElement.childNodes;
Title.innertext = Nodes.item (0). text;
Author.innertext = Nodes.item (1). text;
</script>
<title> call XML data in HTML </title>
<body bgcolor= "#FFFFFF" >
<b> Title: </b>
<span id= "title" ></span><br>
<b> Author: </b>
<span id= "Author" ></span><br>
</body>

4. Use IE5.0 above browser to open myfile.html can see the effect.

Well, by the end of the fourth chapter today, we have some basic knowledge of XML. In the fifth chapter, I will explain a successful example of XML practical application, and show the powerful function 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.