[Introduction] Now let's use Notepad to create our xml file. Let’s look at an XML file first: by Use "Notepad" to create our xml file. First look at an XML file:
Example 1
〈?xml version="1.0" encoding="gb2312" ?〉 〈参考资料〉 〈书籍〉 〈名称〉XML入门精解〈/名称〉 〈作者〉张三〈/作者〉 〈价格 货币单位="人民币"〉20.00〈/价格〉 〈/书籍〉 〈书籍〉 〈名称〉XML语法〈/名称〉 〈!--此书即将出版--〉 〈作者〉李四〈/作者〉 〈价格 货币单位="人民币"〉18.00〈/价格〉 〈/书籍〉 〈/参考资料〉
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, and the content information of the XML file is stored here. We can see that the main body of the file is composed of the starting
〈!--This book will be published soon--〉This sentence is the same as HTML, it is a comment. In the XML file, the comment part is placed between the "〈!--" and "--〉" tags between parts.
As you can see, XML files are 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 to pass to the next One-step application processing. 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.
Because 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), so from the beginning, XML designers The syntax and structure of XML are strictly stipulated. 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 the Well-Formed XML file and the other is the Validating XML file.
If an XML file satisfies certain relevant rules in the XML specification and does not use DTD (document format definition - details later), it 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 of all, the first line of the 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, in the XML file, the control tag must have a corresponding closing tag. For example: the
Fourth, marks must not cross. In the previous HTML file, you can write like this: 〈B〉〈H〉XXXXXXX〈/B〉〈/H〉, 〈B〉 and 〈H〉
There are Overlapping areas, but in XML, it is strictly prohibited to write such interleaved tags, and tags must appear in a regular order.
Fifth, the attribute value must be enclosed in " ". 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 and have the same meaning, while in XML, tags like
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〉marks 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 implement 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 "## Example 2
〈![CDATA[flying xml〉〉〉〉〉,:-)
oooo〈〈〈〈〈〈
>〉
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:
should be required is different (the latter includes in addition to the character Zhang San in the
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), then we can add an XML tag to the tags Built-in attribute - xml:space. For example (note the capitalization of attribute names and values):
Poetry xml:space="preserver"〉
My motherland! motherland!
My Homeland!
/Poetry〉
### In addition, in the XML file, if the special characters in Table 1 are to be used, they must be replaced with corresponding symbols. ###### Table 1###### Special character substitution symbols### #### Let’s 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
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.
The above is the detailed content of XML basics: structure and syntax. For more information, please follow other related articles on the PHP Chinese website!