search
HomeBackend DevelopmentXML/RSS TutorialXML file structure and basic syntax

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元素的格式基本相同,其格式如下:

内容标记名称>

 

所有的数据内容都必须在某个标记的开始和结束标记内,而每个标记又必须包含在另一个标记的开始与结束标记内,形成嵌套式的分布,只有最外层的标记不必被其他的标记所包含。最外层的是根元素(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

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
RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool