XML syntax Overview

Source: Internet
Author: User

The XML document uses self-describing and simple syntax. The most basic components of an XML document include declarations, processing instructions (optional), and elements. The following is a simple XML document:

1 <? Xml version = "1.0" encoding = "GB2312" standalone = "yes"?>
2 <? Xml-stylesheet type = "text/xsl" href = "yxfqust. xsl"?>
3 <! -- The following is a student list -->
4 <student list>
5 <student>
6 <student ID> 2003081205 </student ID>
7 <Name> Tian Lin </Name>
8 <class> 03.2 </class>
9 </student>
10 <student>
11 <student ID> 2003081232 </student ID>
12 <Name> Yang Xuefeng </Name>
13 <class> 03.2 </class>
14 </student>
15 </student list>

1st rows are XML declarations, 2nd rows are processing instructions, 3rd rows are comments, and 4 to 15 are all elements of the document. (Note: XML tags are case sensitive ).
I. Document declaration: <? Xml version = "1.0" encoding = "GB2312" standalone = "yes"?>

Xml tag indicates that it is an xml document. The values of the following two attributes indicate the version number and encoding standard. If standalone is set to yes, this file does not reference other external XML files.
II. Processing Command
Format: <? Processing Command name Processing Command Information?>

<? Xml-stylesheet type = "text/xsl" href = "yxfqust. xsl"?>

3. Note:
Format: <! -- Comment content --> <! -- The following is a student list -->

Notes:
1. Annotations cannot appear before the xml Declaration;
2. Annotations cannot appear in tags;
3. The comments cannot contain two consecutive hyphens (--), for example, <! -- This is a bad document. -- do you know! -->;
4. The annotation can contain elements, but it cannot contain "--". The elements are ignored during parsing;
5. Annotations cannot be nested;
4. Elements and tags:
All XML elements must be reasonably included, and all XML documents must have a root element. Like HTML, XML elements can also have attributes. XML Element attributes are paired with names/values. The XML syntax specification requires that the XML element attribute values be enclosed in quotation marks. Please refer to the two examples below. The first is incorrect, 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">
<To> Tove </to>
<From> Jani </from>
</Note>

Format: <tag attribute name 1 = "value 1" ......> data content </Tag>
XML elements can be expanded, and they are associated. XML elements have simple naming rules:

  • Names can contain in letters, numbers, and other characters (the element name can contain the child mother, number, and other characters .)
  • Names must not start with a number or punctuation character (the element name cannot start with a number or punctuation .)
  • Names must not start with the letters xml (or XML or Xml...) (the element name cannot start with XML (or xml, Xml, xMl .)
  • Names cannot contain spaces (the element name cannot contain spaces .)

    An element is the soul of an XML document. It forms the main content of the document. XML elements are defined by tags, which are divided into non-empty tags and empty tags.
    1. Non-empty tag format:
    <Tag> element content </Tag> for example:

    <Student ID> 2003081205 </student ID>
    <Name> Tian Lin </Name>
    <Class> 03.2 </class>

    2. Empty tag format:
    <Tag Name property name = "property value", property name = "property value" ......> example: <Tian Lin student ID = "2003081205">

    At the same time, elements support reasonable nesting. In the example above, the student list and students are nested at a layer. Nesting must meet the following rules:
    1. All xml documents start from a root node. The root node contains a root element;
    2. All other elements in the document must be included in the root element;
    3. child elements are nested, and sibling elements on the same layer are mutually exclusive;
    4. child elements can also contain child elements;
    5. The element containing child elements is called a branch, and the element without child elements is called a leaf;
    Data can be stored in both sub-elements and attributes. You should try to use sub-elements instead of attributes. The reason is as follows:

  • Attributes cannot contain multiple values (child elements can) (attributes cannot contain multiple values (child elements can ))
  • Attributes are not easily expandable (for future changes) (attributes are not easy to expand)
  • Attributes cannot describe structures (child elements can) (attributes cannot describe the structure (child elements can ))
  • Attributes are more difficult to manipulate by program code (attributes are hard to be processed by program code)
  • Attribute values are not easy to test against a Document Type Definition (DTD)-which is used to define the legal elements of an XML document (it is difficult to test the attribute value through DTD)

    Previous Article: XML Introduction
     

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