Table of Contents
实体引用
XML 中的注释
在 XML 中,空格会被保留
XML 命名规则
最佳命名习惯
Home Backend Development XML/RSS Tutorial XML entity references, annotations, naming rules

XML entity references, annotations, naming rules

Feb 15, 2017 pm 03:37 PM


实体引用

在 XML 中,一些字符拥有特殊的意义。

如果你把字符 "<" 放在 XML 元素中,会发生错误,这是因为解析器会把它当作新元素的开始。

这样会产生 XML 错误:

<message>if salary < 1000 then</message></p>
<p style="margin-top:12px; margin-bottom:0px; border:0px; line-height:18px">为了避免这个错误,请用<span style="margin:0px; padding:0px; border:0px; font-style:normal"><strong>实体引用</strong></span>来代替 "<" 字符:</p><pre style="margin-top:10px; margin-bottom:0px; padding:10px; border:1px dotted rgb(119,136,85); font-family:&#39;Courier New&#39;,Courier,monospace; width:620px; background-color:rgb(245,245,245)"><message>if salary &lt; 1000 then</message></p>
<p style="margin-top:12px; margin-bottom:0px; border:0px; line-height:18px">在 XML 中,有 5 个预定义的实体引用:</p>
<table style="margin:10px 0px 0px; padding:0px; border:1px solid rgb(170,170,170); border-collapse:collapse; width:709px"><tbody style="margin:0px; padding:0px; border:0px">
<tr style="margin:0px; padding:0px; border:0px" class="firstRow">
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">&lt;</td>
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)"><</td><td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">小于</td></tr><tr style="margin:0px; padding:0px; border:0px"><td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">&gt;</td><td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">></td>
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">大于</td>
</tr>
<tr style="margin:0px; padding:0px; border:0px">
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">&amp;</td>
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">&</td>
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">和号</td>
</tr>
<tr style="margin:0px; padding:0px; border:0px">
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">&apos;</td>
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">'</td>
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">单引号</td>
</tr>
<tr style="margin:0px; padding:0px; border:0px">
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">&quot;</td>
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">"</td>
<td style="margin:0px; padding:5px 15px 5px 5px; border:1px solid rgb(170,170,170); vertical-align:text-top; background-color:rgb(239,239,239)">引号</td>
</tr>
</tbody></table>
<p style="margin-top:12px; margin-bottom:0px; border:0px; line-height:18px"><span style="margin:0px; padding:0px; border:0px; font-weight:bold">注释:</span>在 XML 中,只有字符 "<" 和 "&" 确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。</p><p style="margin:0px 0px 0px 15px; padding:20px 0px; border-width:1px 0px; border-top-style:solid; border-bottom-style:solid; border-top-color:rgb(170,170,170); border-bottom-color:rgb(170,170,170); width:710px; font-family:Verdana,Arial,宋体; font-size:12px; line-height:normal; background-color:rgb(249,249,249)"></p><h2 id="XML-中的注释">XML 中的注释</h2><p></p><p style="margin-top:12px; margin-bottom:0px; border:0px; line-height:18px">在 XML 中编写注释的语法与 HTML 的语法很相似:</p><pre style="margin-top:10px; margin-bottom:0px; padding:10px; border:1px dotted rgb(119,136,85); font-family:&#39;Courier New&#39;,Courier,monospace; width:620px; background-color:rgb(245,245,245)"><!-- This is a comment --></p>
<p style="margin:0px 0px 0px 15px; padding:20px 0px; border-width:1px 0px; border-top-style:solid; border-bottom-style:solid; border-top-color:rgb(170,170,170); border-bottom-color:rgb(170,170,170); width:710px; font-family:Verdana,Arial,宋体; font-size:12px; line-height:normal; background-color:rgb(249,249,249)"></p>
<h2 id="在-XML-中-空格会被保留">在 XML 中,空格会被保留</h2>
<p></p>
<p style="margin-top:12px; margin-bottom:0px; border:0px; line-height:18px">HTML 会把多个连续的空格字符裁减(合并)为一个:</p>
<pre style="margin-top:10px; margin-bottom:0px; padding:10px; border:1px dotted rgb(119,136,85); font-family:'Courier New',Courier,monospace; width:620px; background-color:rgb(245,245,245)">HTML:	Hello           my name is David.
输出:	Hello my name is David.
Copy after login

在 XML 中,文档中的空格不会被删节。

XML 命名规则

XML 元素必须遵循以下命名规则:

  • 名称可以含字母、数字以及其他的字符

  • 名称不能以数字或者标点符号开始

  • 名称不能以字符 “xml”(或者 XML、Xml)开始

  • 名称不能包含空格

可使用任何名称,没有保留的字词。

最佳命名习惯

使名称具有描述性。使用下划线的名称也很不错。

名称应当比较简短,比如:,而不是:

避免 "-" 字符。如果您按照这样的方式进行命名:"first-name",一些软件会认为你需要提取第一个单词。

避免 "." 字符。如果您按照这样的方式进行命名:"first.name",一些软件会认为 "name" 是对象 "first" 的属性。

避免 ":" 字符。冒号会被转换为命名空间来使用(稍后介绍)。

XML 文档经常有一个对应的数据库,其中的字段会对应 XML 文档中的元素。有一个实用的经验,即使用数据库的名称规则来命名 XML 文档中的元素。

非英语的字母比如 éòá 也是合法的 XML 元素名,不过需要留意当软件开发商不支持这些字符时可能出现的问题。

以上就是XML  实体引用、注释、命名规则 的内容,更多相关内容请关注PHP中文网(www.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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

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

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

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

Conveniently use PyCharm shortcut keys to implement multi-line comments Conveniently use PyCharm shortcut keys to implement multi-line comments Jan 27, 2024 am 08:02 AM

PyCharm multi-line comment shortcut keys: Make code comments more convenient and require specific code examples. In daily programming work, code comments are a very important part. It not only improves the readability and maintainability of the code, but also helps other developers understand the intent and design ideas of the code. However, manually adding code comments is often a time-consuming and tedious task. In order to make our code comments more efficient, PyCharm provides shortcut keys for multi-line comments. In PyCharm, we can use Ctrl+/

How to Optimize the Maintainability of Java Code: Experience and Advice How to Optimize the Maintainability of Java Code: Experience and Advice Nov 22, 2023 pm 05:18 PM

How to Optimize the Maintainability of Java Code: Experience and Advice In the software development process, writing code with good maintainability is crucial. Maintainability means that code can be easily understood, modified, and extended without causing unexpected problems or additional effort. For Java developers, how to optimize the maintainability of code is an important issue. This article will share some experiences and suggestions to help Java developers improve the maintainability of their code. Following standardized naming rules can make the code more readable.

What are the rules for using underscores and hyphens in PHP function names? What are the rules for using underscores and hyphens in PHP function names? Apr 20, 2024 am 09:33 AM

PHP function naming rules: Underscore: used to separate words and improve readability; suitable for private methods or variables. Hyphens: should not be used in function names. Best practices: Avoid hyphens; use underscores to separate words; precede private or protected method or variable names with an underscore.

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

How to add notes to saved passwords on iPhone How to add notes to saved passwords on iPhone Feb 28, 2024 pm 07:41 PM

iCloud Keychain makes it easier to manage your passwords without relying on memorizing or guessing website or usernames. You can do this by adding notes to existing passwords for apps and websites in iCloud Keychain. In this post, we will explain how to add notes to the passwords you save in iCloud Keychain on iPhone. Requirements There are some requirements you need to meet to use this new feature in iCloud Keychain. iPhone running iOS 15.4 or later Passwords stored in iCloud Keychain A valid Apple ID A valid internet connection How to add notes to saved passwords It goes without saying that you should store some passwords in iCloud Keychain

Using Python to implement data verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding

See all articles