The structure and syntax of XML introductory fine solution

Source: Internet
Author: User
Tags empty format definition html tags first row xml parser xsl xsl file

Now let's use Notepad to create our XML file. First look at an XML file:

Example 1

〈?xml version= "1.0" encoding= "gb2312"?
Resources
Books
Name 〉xml Introductory Refinement 〈/name
Author John 〈/author
Price currency unit = "RMB" 〉20.00〈/price
〈/Books
Books
Name 〉xml Grammar 〈/name
!--The book is about to be published--〉
Author Dick 〈/author
Price currency unit = "RMB" 〉18.00〈/price
〈/Books
〈/Resources

This is a typical XML file, edited and saved as a file with an. xml suffix. We can divide this file into a file preface (Prolog) and a two large section of the file body. The first line in this file is the preamble to the file. The line is something that an XML file must declare, and it must also be in the first row of an XML file, which basically tells the XML parser how to work. Wherein, version is the standard version number that is used to indicate this XML file, and must have; encoding indicates the type of character used in this XML file and can be omitted, and when you omit this declaration, the following character code must be a Unicode character code (not recommended). Because we use the GB2312 character code in this example, encoding this statement cannot be omitted. There are also some statement statements in the preamble of the document, which we give in the following sections.

The rest of the file is part of the file body, and the content information for the XML file is stored here. As we can see, the body of the file is composed of the starting reference and the ending 〈/reference control tag, this is called the "root element" of the XML file; The book is the "child element" under the root element, and under the book there are the child elements of the name, author and price. The monetary unit is an attribute in the price element, and the renminbi is a property value.

!--The book is about to be published--〉 this sentence is the same as HTML, is the annotation, in the XML file, the annotation part is placed between "!--" and "--〉" the part of the tag.

As you can see, the XML file is fairly straightforward. Like HTML, an XML file is made up of a series of tags, but the markup in the XML file is our custom tag, with a clear meaning that we can explain the meaning of the content in the tag.

With an initial impression of the XML file, let's talk about the syntax of the XML file in detail. Before we talk about grammar, we have to understand an important concept, the XML parser (XML Parse).

1.XML Parser

The main function of the parser is to check the XML file for structural errors, peel the tags in the XML file, and read the correct content to the next application. XML is a markup language for structured file information, and there is a detailed rule in XML specification for how to tag a file, and the parser is the software written in accordance with these rules (written in Java). Like HTML, in a browser, you must have an HTML parser so that browsers can "read" various HTML markup pages and display them in front of us. If a browser's HTML parser does not read the tag, it will return the error message to us.

Since the HTML tags are actually quite confusing, there are a lot of nonstandard tags (some of the Web page with IE can be normal display, but not with Netscape Navigator), so from the outset, the designer of XML strictly defined the syntax and structure of XML, The XML file We write must follow these rules, or the XML parser will show you an error message without mercy.

There are two kinds of XML files, one is the well-formed XML file, the other is the validating XML file.

If an XML file satisfies some of the relevant rules in the XML specification and does not use a DTD (a file format definition-after detailed), it can be called a well-formed. And if an XML file is well-formed and correctly using the syntax in DTD,DTD is correct, then this file is validating. For two kinds of XML files, there are two kinds of XML parsers, one is the well-formed parser, the other is the validating parser. The validating parser is included in IE 5, and the validating parser can also be used to parse well-formed XML files.

Check to see if it satisfies the well-formed condition. We can open the first XML file that we just edited with IE 5 version of the browser.

You may want to ask why the display in the browser is the same as my source file? Yes, because for XML files, we pelochely Bibroni 龉 匦, and it's displayed in the form of CSS or XSL. Here, we do not define its CSS or XSL file for this XML file, so it appears in its original form. In fact, for electronic data interchange, you just need an XML file, and if you want to show it in some form, we have to edit the CSS or XSL file (this problem will be discussed later).

2.well-formed XML file

We know that XML must be well-formed in order to be parsed correctly by the parser and displayed in the browser. So what is a well-formed XML file? There are several guidelines that we must meet when creating XML files.

First, the first line of the XML file must be to declare that the file is an XML file and the XML specification version it uses. No other elements or annotations can be in front of the file.

Second, there is only one root element in the XML file. In our first example, the reference 〉...〈/reference is the root element of this XML file.

Third, the tags in the XML file must be properly closed, that is, in an XML file, the control tag must have an end tag corresponding to it. For example: the "name" tag must have a corresponding 〈/name of the end tag, unlike HTML, some tag end tag is optional. If you encounter tags from a single cell in an XML file, this is similar to the 〈img src=.....〉 in HTML when there is no end tag, XML calls it the "empty element", which must be written in this way: empty element name/〉, if the element contains attributes when the Write law is: "EMPTY element name Property name = "Property value"/〉.

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.