search
HomeBackend DevelopmentXML/RSS TutorialIntroduction to basic XML syntax

Introduction to basic XML syntax

Apr 02, 2017 am 11:46 AM

合法的XML文档可有种意思,一个是良构文档(well-format),即符合XML规则书写的文档;另一种是有效文档,是已验证符合一个DTD的文档。

1.基本语法规则

XML是区分大小写的; 

所有元素的起始和结束标注必须成对出现,且要正确嵌套; 

如果使XML说明,则它必须是XML文档的第一行: 

 

元素属性必须用引号引起来,单、双引号都可以,但必须成对出现。如: 

 

XML命名规则:

XML名以下划线或字母开始; 

XML名可包含字母、数字、句点、下划线和冒号; 

XML名不能包含空格; 

XML名不能以数字开始,但可包含数字; 

XML名区分大小写。 

保留标记字符,如果要在XML中显示

7026cfb3293664d35e1a697380f2f080 表示>字符& 表示&字符' 表示'字符" 表示"字符 

我们也可用ENTITY自定义实体: 

<!ENTITY linux "linux is a very good system">

这样我们可用&linux;来调用。 

XML文档内容中的空格是有意义的,在转换后会保留。 

空元素以结束,如

2.良构XML文档和有效XML文档
符合XML语法规则的XML文档称为良构文档,这些规则如下:

应当只有一个父标志,由父标志派生所有其它子标志,在一个文档中不能存在多个父标志。 

嵌套元素应按正确的顺序开始和结束。 

子标志应在父标志完成前关闭。 

属性值应放在双引号中。 

通过某个DTD或Schema验证的文档称为有效XML文档。

3.XML文档的组成

XML声明:

version,定义XML规范的版本号,到现在为止,只有一个版本号1.0。 

encoding,指定文档的编码系统。 

standalone,定义文档是独立的还是需要装入其他元素才能正确分析。如果XML文档没有外部实体或DTD,则可以设置为no,否则设置为yes。可用该值提高性能:如果为no,则可提高处理速度;如果设置为yes,则首先要分析文档,确定需要其他哪些文件,然后才能完全分析文档。 

根元素,每篇XML文档都需要有且只能有一个根元素。由元素是文档的第一个元素,包含其它所有元素。下例的portal就是根元素,如: 

<portal> <name>jims</name> <email></email> ...</portal>

属性,每个元素都可以设置一个或多个属性,如: 

<portal> <name id=&#39;1&#39;,sex="male">Jims</name></portal>

元素和属性都可以表示信息,什么时候使用元素,什么时候使用属性呢?属性信息表现能力有限,它只能表示字符串。所以当需灵活表示信息时应该使用元素。一般把信息主体放到元素中,属性只放一些注释或额外的信息。 

CDATA部份,它用表示,它们之间的数据作为原始字符显示,唯一不能出现的标志是]]>。 

注释,注释是很重要,不论是在编写程序和文档时,所以XML也提供了注释功能,以结尾的一对区间为注释。在以-->结束之前,不能出现“--”号,“---”更不允许。 

处理指令,处理指令以结尾。如PHP处理指令可写成,。处理指令是标记,而不是元素。因此,与注释一样,处理指令可出现在XML文档的标签外的任何位置,包括根元素之前或之后。最常见的处理指令是,xml-stylesheet样式表指令,它会告诉浏览器在显示文档时应用什么样式表。如: 

<?xml-stylesheet href="sample.css" type="text/css"?><portal> <name>...</name>...</portal>

4.XML文档树

XML文档是一种结构化的文档,可用树的形式表示出来。树是一种由节点和分支组成的简单结构,两个节点间由分支连接。上端的节点称为父节点,下端的节点称为子节点。一个节点如果没有父节点,则称为树的根节点(根),每个树必须有且只能有一个根节点。一个节点如果没有子节点,则称为树的叶节点。只有一个节点的树也是允许的。

The above is the detailed content of Introduction to basic XML syntax. For more information, please follow other related articles on the 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
RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

The XML Backbone: How RSS Feeds are StructuredThe XML Backbone: How RSS Feeds are StructuredApr 20, 2025 am 12:02 AM

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

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.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools