XML Syntax rules

Source: Internet
Author: User
Tags xml example

Transfer excerpt from: http://www.w3school.com.cn/xml/xml_elements.asp

XML Grammar Rules

XML document contains XML element.

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

All XML element 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, and you do not need to close the label.

XML The label 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:

<Message> this is wrong. </message>

<message> that's right. </message>

Note: opening labels and closing labels are often referred to as start and end tags. Whatever terminology you prefer, the concepts are the same.

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.

XML The 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>

XML attribute value to 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>

<from>John</from>

</note>

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

Entity reference

In XML, some characters have a special meaning.

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:

&LT;

<&NBSP;

less than

&GT;

>&NBSP;

&amp;

&

and number

&APOS;

single quote

&quot;

quotes

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.

XML the comments in

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

<!--This is a comment-

in the XML , the 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.

XML in LF Store line breaks

In a Windows application, a newline is typically stored in a pair of characters: a carriage return (CR) and a newline character (LF). This pair of characters is similar to the action of a typewriter setting a new line. In Unix applications, new lines are stored as LF characters. The Macintosh application uses CR to store new rows.

XML Elements

What is XML element?

XML The element refers to the part from (and including) the start tag until (and includes) the end tag.

Elements can contain other elements, text, or mixtures of both. Elements can also have properties.

<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>

</book>

</bookstore>

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").

XML naming rules

XML elements must follow the following naming conventions:

    • Names can contain letters, numbers, and other characters
    • Names cannot start with numbers or punctuation marks
    • The name cannot start with the character "XML" (or XML, XML)
    • Name cannot contain spaces

You can use any name without a reserved word.

Best naming habits

Make the name descriptive. It's also good to use an underscore name.

The name should be shorter, for example: <book_title>, rather than:<the_title_of_the_book>.

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

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

Avoid the ":" character. The colon is converted to a namespace to use (described later).

XML documents often have a corresponding database, where the fields correspond to the elements in the XML document. There is a practical experience of naming the elements in an XML document using the database name rules.

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.

XML the element is extensible

XML elements are extensible to carry more information.

Take a look at the following XML example:

<note>

<to>George</to>

<from>John</from>

<body>don ' t forget the meeting!</body>

</note>

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

MESSAGE

To:george

From:john

Don ' 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>

<body>don ' t forget the meeting!</body>

</note>

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 Syntax rules

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.