Home  >  Article  >  Backend Development  >  XML file structure and basic syntax

XML file structure and basic syntax

高洛峰
高洛峰Original
2016-12-19 16:08:071484browse

XML文件结构和基本语法

分类: html java2010-03-12 17:04 903人阅读 评论(1) 收藏 举报

4.1.1               XML文件结构

一个XML文件通常包含文件头和文件体两大部分

1.         文件头

XML文件头由XML声明与DTD文件类型声明组成。其中DTD文件类型声明是可以缺少的,关于DTD声明将在后续的内容中介绍,而XML声明是必须要有的,以使文件符合XML的标准规格。

在前面的Flowers.xml文件中的第一行代码即为XML声明:

              其中:

”代表一条指令的结束;

“xml”代表此文件是XML文件;

“ version="1.0" ”代表此文件用的是XML1.0标准;

“ encoding="gb2312" ” 代表此文件所用的字符集,默认值为Unicode,如果该文件中要用到中文,就必须将此值设定为gb2312。

 

注意:XML声明必须出现在文档的第一行。

 

2.         文件体

文件体中包含的是XML文件的内容,XML元素是XML文件内容的基本单元。从语法讲,一个元素包含一个起始标记、一个结束标记以及标记之间的数据内容。

 

XML元素与HTML元素的格式基本相同,其格式如下:

<标记名称 属性名1="属性值1" 属性名1="属性值1" ……>内容

 

所有的数据内容都必须在某个标记的开始和结束标记内,而每个标记又必须包含在另一个标记的开始与结束标记内,形成嵌套式的分布,只有最外层的标记不必被其他的标记所包含。最外层的是根元素(Root),又称文件(Document)元素,所有的元素都包含在根元素内。

在前面的Flowers.xml文件中,根元素就是,根元素必须而且只能有一个,在该文件有三个子元素,这样的元素可以有多个。

4.1.2               XML的基本语法

1.         注释

XML的注释与HTML的注释相同,以“”结束。

 

2.         区分大小写

在HTML中是不区分大小写的,而XML区分大小写,包括标记,属性,指令等。

 

3.         标记

XML标记与HTML标记相同,“<”表示一个标记的开始,“>” 表示一个标记的结束。XML中只要有起始标记,就必须有结束标记,而且在使用嵌套结构时,标记之间不能交叉。

在XML中不含任何内容的标记叫做空标记,格式为:<标记名称/>

 

4.         属性

XML属性的使用与HTML属性基本相同,但需要注意的是属性值要加双引号。

 

5.         实体引用

实体引用是指分析文档时会被字符数据取代的元素,实体引用用于XML文档中的特殊字符,否则这些字符会被解释为元素的组成部分。例如,如果要显示“<”,需要使用实体引用“<”否则会被解释为一个标记的起始。

XML中有5个预定义的实体引用,如表 4.1所示。

表 4.1 XML预定义的实体引用

6. CDATA

在XML中由一个特殊的标记CDATA,在CDATA中所有文本都不会被XML处理器解释,直接显示在浏览器中,使用方法如下:

这里的内容可以直接显示。

]]>

 

7.         处理指令

处理指令使用来给处理XML文件的应用程序提供信息的,处理指令的格式如下:

              例如,XML声明就是一条处理指令:

Among them, "xml" is the processing instruction name, version="1.0" encoding="gb2312" is the processing instruction information.

4.2 XML and CSS

CSS can be used to set the display mode of XML files, that is, add the following statement at the head of the XML file, below the XML declaration:

Let’s use examples to introduce how to use CSS to display XML files. First create a css file, the code is as follows:

flowers.css: A CSS style that displays XML files

                                                                                                    using   using   using       using         using using         through through out through out through out through out through out through ‐ ‐ toward ‐ ‐‐ ‐ , and that the code is as follows:

flowers.css red}

price{display:block}

Then, use this css style in the flowers.xml file, that is, add the following statement below the XML declaration in the flowers.xml file:

The complete program code is as follows:

ex4_3_01.xml: Use CSS to display XML files

shop1

                                                                                                                                                                                                       lower>

                                                            ;/Vendor>

                                                                                                                                                                              

                                 shop3

                                                                                          

                How this example appears in the browser The effect is shown in Figure 4.2.

Figure 4.2 Using CSS to display XML files

When using CSS to display XML files, there is no selectivity. That is to say, all data under the root element will be displayed, and the structure and structure of the original file cannot be changed. The order of content. In addition, CSS does not support Chinese tags because CSS is not a style language specifically developed for XML, but the XSL introduced below will do. XSL is specially designed for XML and is more complex than CSS.

4.3 XSL

The effect of using CSS to display XML files introduced in the previous section is not very satisfactory. In fact, CSS is mainly suitable for HTML files. For data files such as XML, CSS is not suitable, especially for those When the file contents need to be rearranged and displayed according to different conditions, XSL can be used.

XSL is currently the most powerful and flexible style language. It is specially designed for applying XML. It is much more complex than CSS. However, it is currently not well supported and not yet finalized.

Use XSL to set the display mode of the XML file, that is, add the following statement to the head of the XML file, below the XML declaration:

Below we use examples to introduce how to use XSL to display XML files. First create an xsl file, the code is as follows:

flowers.xsl: xsl file used to display XML files

                                                                                                

                                                                                                                     sl:for-each select ="//Flowers/Flower">

                                                            

                                                                                            

We can watch Output: An XSL file is just like an empty HTML file, and a traditional HTML file is produced by filling in an XML file.

An XSL file must first have an XML declaration (i.e. the first line), because XSL is actually a special XML file. The root element of XSL is xsl:stylesheet, that is, an XSL document must start with the tag and end with the tag. The xmlns:xsl attribute is used to set the XSL name domain.

The root element of XSL is usually composed of one or more template elements. In this example, it only contains a single template. It starts with the tag and ends with the tag. Use The match attribute can select qualified nodes in the XML file, that is, set the template name. For the top-level template, match is set to "/", which represents the root element of the entire XML file.

Then get the required data from the XML file. The easiest way to get the data is:

If you want to get multiple elements, you need to Use the xsl:for-each element in the following format:

…..

Connect Next, we need to use this xsl file in the flowers.xml file, that is, add the following statement under the XML declaration in the flowers.xml file:

The complete program code is as follows:

ex4_4_01.xml: Using XSL to display XML files

;

                                                                        /Name>

              $4.30

                                                                                 

           $3.50

For more articles related to XML file structure and basic syntax, please pay attention PHP Chinese website!

Statement:
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
Previous article:Understand the Xml formatNext article:Understand the Xml format