Introduction to basic XML syntax
合法的XML文档可有种意思,一个是良构文档(well-format),即符合XML规则书写的文档;另一种是有效文档,是已验证符合一个DTD的文档。
1.基本语法规则
XML是区分大小写的;
所有元素的起始和结束标注必须成对出现,且要正确嵌套;
如果使XML说明,则它必须是XML文档的第一行:
元素属性必须用引号引起来,单、双引号都可以,但必须成对出现。如:
XML命名规则:
XML名以下划线或字母开始;
XML名可包含字母、数字、句点、下划线和冒号;
XML名不能包含空格;
XML名不能以数字开始,但可包含数字;
XML名区分大小写。
保留标记字符,如果要在XML中显示<或&之类的标记,就要使用字符的实体形式,XML中有五种预先定义了的实体:
< 表示<字符> 表示>字符& 表示&字符' 表示'字符" 表示"字符
我们也可用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='1',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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and
