For now, let's use "Notepad" to create our XML file. Let’s look at an XML file first:
Example 1
〈?xml version="1.0" encoding="gb2312" ?〉
〈References〉
<books>
〈Name〉Introduction to XML 〈/Name〉
〈Author〉Zhang San〈/Author〉
〈Price currency unit="RMB"〉20.00〈/price〉
</books>
<books>
<name>XML syntax </name>
〈!--This book will be published soon--〉
〈Author〉Li Si〈/Author〉
〈Price currency unit="RMB"〉18.00〈/price〉
</books>
〈/Reference Materials〉
This is a typical XML file. After editing, it is saved as a file with the .xml suffix. We can divide this file into two major parts: the file preamble (Prolog) and the file body. The first line in this file is the file preamble. This line is something that an XML file must declare, and it must also be located on the first line of the XML file. It mainly tells the XML parser how to work. Among them, version is the standard version number used by this XML file, which is required; encoding specifies the character type used in this XML file, which can be omitted. When you omit this statement, the following character code must be Unicode Character code (it is recommended not to omit it). Because we are using GB2312 character code in this example, the encoding statement cannot be omitted. There are also some declaration statements in the preamble of the file, which we will introduce later.
The rest of the file belongs to the file body, where the content information of the XML file is stored. We can see that the main body of the file is composed of the starting <reference> and the ending </reference> control tags. This is called the "root element" of the XML file; <book> is the "root element" directly under the root element. "Sub-element"; under "Book" there are sub-elements such as "Name", "Author" and "Price". The currency unit is an "attribute" in the <price> element, and "RMB" is the "attribute value".
〈!--This book will be published soon--〉 is a comment just like HTML. In an XML file, the comment part is placed between the "〈!--" and "--〉" tags.
As you can see, the XML file is quite simple. Like HTML, XML files are also composed of a series of tags. However, the tags in XML files are our own custom tags and have clear meanings. We can explain the meaning of the content in the tags.
After having a preliminary impression of XML files, let's talk about the syntax of XML files in detail. Before talking about grammar, we must understand an important concept, which is XML Parse.
1. XML parser
The main function of the parser is to check whether there are structural errors in the XML file, strip the tags in the XML file, and read out the correct content for processing by the next application program. XML is a markup language used to structure file information. The XML specification has detailed rules on how to mark the structure of files. The parser is software written according to these rules (mostly written in Java). Just like HTML, in the browser, there must be an HTML parser so that the browser can "read" various web pages composed of HTML tags and display them in front of us. If there are tags that the browser's HTML parser cannot read, an error message will be returned to us.
Since the current HTML tags are actually quite confusing and there are a lot of non-standard tags (some web pages can be displayed normally with IE, but not with Netscape Navigator), the designers of XML have strictly stipulated the syntax of XML from the very beginning. and structure, the XML files we write must comply with these regulations, otherwise the XML parser will show you error messages mercilessly.
There are two types of XML files, one is Well-Formed XML file and the other is Validating XML file.
If an XML file satisfies certain relevant rules in the XML specification and does not use a DTD (document format definition - details later), the document can be called Well-Formed. And if an XML file is Well-Formed, the DTD is used correctly, and the syntax in the DTD is correct, then the file is Validating. Corresponding to the two XML files, there are two XML parsers, one is the Well-Formed parser and the other is the Validating parser. IE 5 includes a Validating parser, which can also be used to parse Well-Formed XML files.
Check whether it meets the conditions of Well-Formed. We can open the first XML file we just edited with a browser of IE 5 or above.
You may ask why the display in the browser is the same as my source file? That's right, because for XML files, we only know the content, and its display form is completed by CSS or XSL. Here, we have not defined its CSS or XSL file for this XML file, so it is displayed in its original form. In fact, for electronic data interchange, only an XML file is needed. If we want to display it in some form, we must edit the CSS or XSL file (this issue will be discussed later).
2. Well-Formed XML file
We know that XML must be Well-Formed in order to be correctly parsed by the parser and displayed in the browser. So what is a Well-Formed XML file? There are mainly the following guidelines, which must be met when we create XML files.
First, the first line of an XML file must declare that the file is an XML file and the XML specification version it uses. There cannot be other elements or comments in front of the file.
Second, there can be only one root element in an XML file. In our first example, 〈References〉... 〈/References〉 is the root element of this XML file.
Third, the tags in the XML file must be closed correctly, that is, the control tags in the XML file must have a corresponding closing tag. For example: the <name> tag must have a corresponding </name> closing tag. Unlike HTML, the closing tag of some tags is optional. If you encounter a self-contained unit tag in an XML file, which is similar to <img src=.....> in HTML without an end tag, XML calls it an "empty element" and you must use The writing method is: 〈empty element name/〉. If the element contains attributes, the writing method is: 〈empty element name attribute name = "attribute value"/〉.
Fourth, marks must not cross each other. In previous HTML files, you could write like this:
〈B〉〈H〉XXXXXXX〈/B〉〈/H〉,
there are overlapping areas between the 〈B〉 and 〈H〉 tags, but in XML, it is strictly Staggered writing of such marks is prohibited. Marks must appear in a regular order.
Fifth, attribute values must be enclosed in " " signs. Such as "1.0", "gb2312", "RMB" in the first example. They are all enclosed by " " and cannot be missed.
Sixth, English control tags, instructions, and attribute names must be case-sensitive. Unlike HTML, in HTML, tags like <B> and <b> have the same meaning, while in XML, tags like <name>, <NAME> or <Name> are different.
Seventh, we know that in HTML files, if we want the browser to display the things we input intact, we can put these things in 〈pre〉〈/pre〉 or 〈xmp〉〈/xmp〉 Mark the middle. This is essential for us to create HTML teaching web pages, because the source code of HTML must be displayed in the web page. In XML, to achieve such a function, CDATA tags must be used. The information in the CDATA tag is passed intact to the application by the parser, and any control tags in the segment of information are not parsed. The CDATA area is marked by: "<![CDATA[" as the start mark and ">>" as the end mark. For example: In the source code in Example 2, except for the "<![CDATA[" and ">>" symbols, the rest of the content parser will be handed over to the downstream application intact, even if there are blanks at the beginning and end of the CDATA area and newline characters, etc., will also be transferred (note that CDATA is an uppercase character).
Example 2
〈![CDATA[flying xml〉〉〉〉〉,:-)
ooo〈〈〈〈〈〈
>〉
Eighth, XML handles whitespace characters differently from HTML. The HTML standard stipulates that no matter how many blanks there are, they will be treated as one blank; while in XML, it is stipulated that the parser must faithfully hand over all blanks other than tags to downstream applications for processing. In this way, we sometimes have to abandon the indentation habit when writing HTML files, because the parser also has to process the indented spaces. Such as:
〈Author〉Zhang San〈/Author〉
and 〈author〉
Zhang San 〈/Author〉
is different for the parser (the latter includes in addition to the character Zhang San within the 〈Author〉〈/Author〉 tag, it also includes two newline marks and the characters before "Zhang San" text indentation symbols). Therefore, the parser will have different processing results after removing the mark and passing the information to the application.
If we want to clearly tell the XML program that the spaces in the tags have a clear meaning and should not be removed casually (for example, in some poems, spaces have specific meanings), we can add an XML built-in attribute - xml - to the tags. :space . For example (note the case of attribute names and values):
〈poetry xml:space="preserver"〉
Motherland! motherland!
My motherland!
〈/Poetry〉
In addition, in the XML file, if the special characters in Table 1 are to be used, they must be replaced by corresponding symbols.
Table 1
Special character replacement symbols && &
<⁢
>>
" "
' '
To make a summary here: the XML file that meets the above requirements is the Well-Formed XML file. This is the most basic requirement for writing XML files. You can see that the syntax of XML files is much stricter than that of HTML. Due to such strict regulations, it is much easier for software engineers to write XML parsers. Unlike writing parsers for HTML language, they must work hard to adapt to different web page writing methods and improve the adaptability of their own browsers. In fact, this is also a good thing for us beginners. Just do what you want, and don't have to worry about how to write various HTMLs like before.
We see that in XML files, most of the custom tags are used. But think about it, if two companies A and B in the same industry want to exchange data with each other using XML files, company A uses the <price> tag to represent the price information of their products, while company B may use <selling price> to represent the price. information. If an XML application reads the information in their respective XML files, if it only knows that the <price> tag represents price information, then company B's price information cannot be read, and an error will occur. Obviously, for entities that want to use XML files to exchange information, there must be an agreement between them - that is, which tags can be used to write XML files, which sub-elements can be included in the parent element, the order in which each element appears, and the How to define the properties, etc. This way they can have smooth communication when exchanging data with XML. This convention is called DTD (Document Type Definition, document format definition). You can think of a DTD as a template for writing XML files. For XML data exchange between the same industry, it will be much more convenient to have a fixed DTD. For example, if the XML web pages of major electronic shopping malls on the Internet all follow the same DTD, then we can easily write an application based on this DTD to automatically capture the things we are interested in online. In fact, there are already several well-defined DTDs, such as MathML, SMIL, etc. mentioned earlier.
If an XML file is Well-Formed and it is correctly created based on a DTD, then the XML file is called: Validating XML file. The corresponding parser is called: Validating Parser.