A detailed introduction to XML structure and syntax

黄舟
Release: 2017-03-24 17:09:23
Original
1303 people have browsed it

Now let's 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〈/价格〉
〈/书籍〉
〈/参考资料〉
Copy after login

This is a typical XML file. After editing, save it 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 indicates the standard version number used in this XML file, which is required; encoding indicates the character type used in this XML file, which can be omitted. When you omit this statement, the following The character code must be a 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 and the ending control tags. This is called the "root element" of the XML file; 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 a "attribute" in the element, and "RMB" is the "attribute value".

〈!--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, 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 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, it will return us an error message.

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), 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), 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.

1.XML文件的第一行必须是声明该文件是XML文件以及它所使用的XML规范版本。在文件的前面不能够有其它元素或者注释。

2.在XML文件中有且只能够有一个根元素。我们的第一个例子中,〈参考资料〉... 〈/参考资料〉就是此XML文件的根元素。

3.在XML文件中的标记必须正确地关闭,也就是说,在XML文件中,控制标记必 须有与之对应的结束标记。如:〈名称〉标记必须有对应的〈/名称〉结束标记,不像HTML,某些标记的结束标记可有可无。如果在XML文件中遇到自成一个单元的标记,就是类似于HTML 中的〈img src=.....〉的这些没有结束标记的时候,XML把它称为“空元素”,必须用这样的写法:〈空元素名/〉,如果元素中含有属性时写法则为:〈空元素名 属性名=“属性值”/〉。

4.标记之间不得交叉。在以前的HTML文件中,可以这样写:

〈B〉〈H〉XXXXXXX〈/B〉〈/H〉,〈B〉和〈H〉
Copy after login

标记之间有相互重叠的区域,而在XML中,是严格禁止这样标记交错的写法,标记必须以规则性的次序来出现。

5.属性值必须要用“ ”号括起来。如第一个例子中的“1.0”、“gb2312”、“人民币”。都是用“ ”号括起来了的,不能漏掉。

6.控制标记、指令和属性名称等英文要区分大小写。与HTML不同的是,在HTML中, 类似〈B〉和〈b〉的标记含义是一样的,而在XML中,类似〈name〉、〈NAME〉或〈Name〉这样的标记是不同的。

7.我们知道,在HTML文件中,如果我们要浏览器原封不动地将我们所输入的东西显示出来,可以将这些东西放到〈pre〉〈/pre〉或者〈xmp〉〈/xmp〉标记中间。这对于我们创建HTML教学的网页是必不可少的,因为网页中要显示HTML的源代码。而在XML中,要实现这样的功能,就必须使用CDATA标记。在CDATA标记中的信息被解析器原封不动地传给应用程序,并且不解析该段信息中的任何控制标记。CDATA区域是由:“〈![CDATA[”为开始标记,以“>〉”为结束标记。例如:例2中的源码,除了“〈![CDATA[”和“>〉”符号,其余的内容解析器将原封不动地交给下游的应用程序,即使CDATA区域中的开始和结尾的空白以及换行字符等,都同样会被转交(注意CDATA是大写的字符)。

例2:

〈![CDATA[飞翔的xml〉〉〉〉〉,:-)
oooo〈〈〈〈〈〈〈
>〉
Copy after login

8.XML处理空白字符和HTML不一样。HTML标准规定,不管有多少个空白,都当作一个空白来处理;而在XML中规定,所有标记以外的空白,解析器都要忠实地交给下游的应用程序处理。这样,我们有时必须摒弃编写HTML文件时的缩排习惯,因为缩排的空格,解析器也要处理。如:

〈作者〉张三〈/作者〉
和
〈作者〉
张三
〈/作者〉
Copy after login

上述内容对于解析器来说是不同的(后者在〈作者〉〈/作者〉标记之内除了张三这个字符以外,还包括两个换行记号以及“张三”前的文字缩排符号)。所以解析器在去掉标记后将信息传给应用程序将有不同的处理结果。

如果我们想明确地告诉XML程序,标记中的空白有明确的含义,不要随便去掉(如在一些诗中,空格有它具体的意义),则可在标记中加入一个XML内置的属性——xml:space 。如(注意属性名称和值的大小写):

〈诗歌 xml:space="preserver"〉
祖国啊! 祖国!
我的祖国!
〈/诗歌〉
Copy after login

另外,在XML文件中,如果要用到表1的特殊字符,必须用相应符号代替。

表1:

特殊字符     替代符号
 &&       &
 <       &it;
 >       >
 "       "
 &#39;      &apos;
Copy after login

小结:

符合上述规定的XML文件就是Well-Formed的XML文件。这是编写XML文件的最基本要求。可以看到XML文件的语法规定比HTML要严格多了。由于有这样的严格规定,软件工程师编写XML的解析器就容易多了,不像编写HTML语言的解析器,必须费尽心思去适应不同的网页写法,提高自己浏览器的适应能力。实际上,这对于我们初学者来说,也是一件好事。该怎样就怎样,不必像原来那样去疑惑各种HTML的写法。

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 tag to represent the price information of their products, while company B may use to represent the price. information. If an XML application reads the information in their respective XML files, if it only knows that the 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.

The above is the detailed content of A detailed introduction to XML structure and syntax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template