An Introduction to XML Tutorial: Mastering Learning XML Grammar Rules

Source: Internet
Author: User
Tags date character set version
xml| Tutorials | Getting Started | grammar

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

Because of this, it is not difficult to create software that can read and manipulate XML.

An example of an XML document

XML uses a simple syntax that can be self-describing.

<?xml version= "1.0" encoding= "iso-8859-1"? ><note><to>tove</to><from>jani</from >

The first line in this document-the XML declaration-defines the version of the XML and the character encoding used in the document. In this example, the XML 1.0 specification is followed and the iso-8859-1 character set is used.

The next line describes the root element of the document (it's like saying: "This document is a note"):


The next 4 lines describe the 4 child elements of the root element (to, from, heading, and body):


The last line defines the end of the root element:


As we can see, this XML document contains a sticky note left to Tove by Jani. Now, you should agree with our view that XML has the perfect self-describing nature.

All elements must have a close tag

Omitting the close tag is illegal when using XML.

In HTML, some elements do not necessarily have a close tag. The following code is legal in HTML:

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

In XML, all elements must have a close tag:

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

Note: You may have noticed from the example above that the XML declaration did not close the tag. 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 tags are case sensitive

Unlike HTML, XML tags are case sensitive.

In XML, label <Letter> and label <letter> are different.

So you must open and close the label using the same case:

<Message> this is wrong. </message><message> that's right. </message>

The XML must be nested correctly

Improper nesting of tags is meaningless for XML.

In HTML, some elements can be incorrectly nested together, just like this:

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

In XML, all elements must be properly nested, like this:

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

The XML document must have a root element

All XML must contain a single label pair that can define the root element.

All other elements must be inside this root element.

All elements can have child elements. Child elements must be properly nested within their parent elements:

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

attribute values in XML must be quoted

In XML, omitting the quotation marks on either side of a property value is illegal.

Like HTML, XML can also have attributes (name/value pairs). In XML, the attribute value of an XML must be enclosed in quotation marks. Please study the following two XML documents. The first one is wrong and the second is correct:

<?xml version= "1.0" encoding= "iso-8859-1"? ><note Date=12/11/2002><to>tove</to><from >Jani</from></note>
<?xml version= "1.0" encoding= "iso-8859-1" ><note "date=" 12/11/2002 >Jani</from></note>

In the first document, date attributes are not quoted. This is true: date= "12/11/2002". This is wrong: date=12/11/2002.

In XML, spaces are preserved.

In XML, spaces are not truncated.

This is different from HTML. In HTML, a sentence like this:

Hello my              name is Tove,

will appear as follows:

Hello My name is Tove,

This is because HTML will cut multiple contiguous whitespace characters to one.

In XML, CR/LF is converted to LF

In XML, a new line (that is, a newline) is stored as LF (line Feed, newline).

Are you familiar with typewriters? Typewriters were the mechanical equipment used to make printed documents in the last century. :-)

When you type a line of text with a typewriter, you need to manually move the print slide to the left margin and manually feed the feed.

In Windows applications, new rows are usually stored as a pair of characters: carriage return (CR) and line break (LF). This character is similar to the act of setting a new line for a typewriter. In UNIX applications, new rows are usually stored as LF characters. Macintosh applications only use CR characters to store new rows.

Annotations in XML

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

<!--This is a comment-->

The XML is nothing special.

XML is nothing special. It's just plain text with a label enclosed in corner brackets.

Software that can process plain text files can also process XML. In a simple text editor, XML tags can also be displayed and not treated in a special way.

In an XML-aware (Xml-aware) application, XML tags are handled specifically. Depending on the type of application, these tags may/may not be seen, and may have some kind of functional meaning.

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