XML Learning 2:xml Basic Syntax (i)

Source: Internet
Author: User
Tags cdata xml example xml parser

Import

an XML file can be divided into the following sections: document declarations, elements, attributes, comments, CDATA areas, special characters, processing instructions

(processing instruction) and so on.

Let's look at a simple XML description:

<?xml version= "1.0" encoding= "UTF-8"?>  <!--above is document declaration--  <?xml-stylesheet type= "text/css" href = "Xml2.css"?>  <!--above is processing instructions-  < root elements >      <!--comments--      <![ Cdata[cdata area, can be any character]]>      < element attribute = "attribute value" >          < element > element content </element >          < empty element/>      </Element >   </root element >  

an XML syntax rule

The syntax rules for XML are simple and logical. These rules are easy to learn and easy to use.

(1) All XML elements must have a close tag

In HTML, you will often see elements without closing tags:

<p>this is a paragraph<p>this is another paragraph

In XML, it is illegal to omit the close tag. All elements must have a close tag:

<p>this is a paragraph</p><p>this is another paragraph</p>  

Note: You may have noticed that the XML declaration did not close the label. This is not a mistake. Declarations are not part of the XML itself. It is not an XML element

You do not need to close the label.

(2) The XML tag is case sensitive

XML elements are defined using XML tags.

The XML tag is case sensitive. In XML, label <Letter> is different from tag <letter>.

You must use the same casing to write open tags and close labels:

Note: Opening labels and closing labels are often referred to as start and end tags. Whatever terminology you prefer, the concepts are the same.
(3) XML must be nested correctly

In HTML, you often see elements that are not nested correctly:

<b><i>this text is bold and italic</b></i>

In XML, all elements must be nested correctly with each other:

<b><i>this text is bold and italic</i></b>

In the example above, the proper nesting means that because the <i> element is open within the <b> element, it must be closed within the <b> element.
(4) The XML document must have a root element

An XML document must have an element that is the parent element of all other elements. This element is called the root element.

<root>  <child>    <subchild>.....</subchild>  </child></root>

(5) The attribute value of XML must be quoted

Like HTML, XML can also have attributes (name/value pairs).

In XML, the attribute value of XML should be quoted. Please study the following two XML documents. The first one is wrong and the second one is correct:

<note date=08/08/2008>     <to>George</to>     <from>John</from></note> < Note date= "08/08/2008" >     <to>George</to>     

The error in the first document is that the Date property in the note element is not quoted.

(6) Entity references

In XML, some characters have a special meaning. To avoid confusing character data with some of the special symbols needed in the label, XML also

For the entity reference

If you put the character "<" in an XML element, an error occurs because the parser takes it as the beginning of the new element.

This produces an XML error:

<message>if Salary < Then</message>

To avoid this error, use an entity reference instead of the "<" character:

<message>if Salary &lt; Then</message>

In XML, there are 5 predefined entity references:


Note: In XML, only the characters "<" and "&" are really illegal. The greater than sign is legal, but it is a good practice to replace it with an entity reference.

(7) annotations in XML

XML annotations, like HTML, are <!--annotation-->, nor can they be nested, and cannot be placed in the middle of the element name.

The syntax for writing comments in XML is similar to the syntax of HTML:

(8) in XML, spaces are preserved

HTML cuts (merges) multiple consecutive whitespace characters into one:

HTML: Hello My name is David.

Output: Hello My name is David.

In XML, spaces in a document are not truncated.

(9) XML document declaration

Most XML files begin with an XML declaration, which provides basic information about the document.

<?xml version= "1.0" encoding= "encoding Mode"  standalone= "Yes|no"?>  

The XML declaration is placed on the first line of the XML document.

The XML declaration consists of the following parts:

Version represents the versions of the XML.

Encoding represents the character encoding method used by XML files, and is encoded in utf-8,gb2312,iso-8859-1.

Standalone indicates whether the document definition is used independently. It has two values:standalone= "yes" and standalone= "no" (default).

Two XML elements

An XML document contains XML elements.

(1) What is an XML element

The XML element refers to the part from (and including) the start tag until (and including) the end tag. Elements can contain other elements, text, or a mixture of the two

Extract Elements can also have properties.

XML element basic syntax:< elements > element content </elements >

Precautions:

1) Each XML document must have only one root element.

2) The root element is an element that completely includes all other elements in the document.

3) The start tag of the root element is placed before the start tag of all other elements.

4) The end tag of the root element is placed after the end tag of all other elements.

5) The XML element refers to the label that appears in the XML file, a label is divided into the start tag and the end tag, and a label has the following writing forms, such as:

Contains the label body:<a>123</a>

Without tag body: <a></a>, abbreviated to:<a/>

6) for all whitespace and line breaks that appear in the XML tag, the XML parser is treated as a label content . For example: <a>123</a> and <a>

123 </a> meaning is completely different.

Take a look at an example:

<bookstore>   <book category= "Children" >       <title>harry potter</title>        < Author>j K. rowling</author>        <year>2005</year>        <price>29.99</price>    </book>   <book category= "WEB" >       <title>learning xml</title>        <author >erik T. ray</author>        <year>2003</year>        <price>39.95</price>    

In the example above, both,<bookstore> and <book> have element content because they contain other elements. <author> only text content, because it

Contains only text. In the example above, only the <book> element owns the attribute (category= "children").

(2) XML naming conventions

XML elements must follow the following naming conventions:

1) The name can contain letters, numbers, and other characters.

2) The name cannot begin with a number or punctuation mark.

3) The name cannot begin with the character "XML" (or XML, XML).

4) The name cannot contain spaces.

You can use any name without a reserved word.

(3) Best naming habits

Some practices for best naming habits:

1) make the name descriptive. It's also good to use an underscore name.

2) The name should be relatively brief, for example: <book_title>, rather than:<the_title_of_the_book>.

3) Avoid the "-" character. If you name it in this way: "First-name", some software will think you need to extract the first word.

4) Avoid "." Character. If you name it this way: "First.name", some software will assume that "name" is the property of the object "first".

5) Avoid the ":" character. Colons are converted to namespaces to be used.

XML documents often have a corresponding database, where the fields correspond to the elements in the XML document. There is a practical experience, that is, the use of database

Name rules for naming elements in an XML document.

Non-English letters such as Éòá are also valid XML element names, but you need to be aware of problems that may arise when software developers do not support these characters.

(4) XML elements are extensible

XML elements are extensible to carry more information.

Take a look at the following XML example:

<note>     <to>George</to>     <from>John</from>     
Let's imagine that we have created an application that extracts the <to>, <from>, and <body> elements and produces the following output

Out

MESSAGETo:GeorgeFrom:JohnDon ' t forget the meeting!
Imagine, then, that the XML document author added some additional information to the document:

<note>     <date>2008-08-08</date>     <to>George</to>     <from>john</ from>     

Then will this application break or crash? No. The application can still find the <to>, <from>, and <body> elements in the XML document

and produce the same output. One of the advantages of XML is that it can often be extended without interrupting the application.


XML Learning 2:xml Basic Syntax (i)

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.