search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
实体引用
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 错误:

&lt;message&gt;if salary &lt; 1000 then&lt;/message&gt;</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>来代替 "&lt;&quot; 字符:</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)">&lt;message&gt;if salary &amp;lt; 1000 then&lt;/message&gt;</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)">&amp;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)">&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></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;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)">&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>
</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;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)">&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>
</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;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)">&amp;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 中,只有字符 "&lt;&quot; 和 &quot;&amp;&quot; 确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。</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 style="margin:0px; padding:0px; border:0px; font-family:微软雅黑; font-size:14px">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)">&lt;!-- This is a comment --&gt;</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 style="margin:0px; padding:0px; border:0px; font-family:微软雅黑; font-size:14px">在 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.

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

XML 命名规则

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

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

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

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

  • 名称不能包含空格

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

最佳命名习惯

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

名称应当比较简短,比如:<book_title>,而不是:<the_title_of_the_book>。

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

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

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

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

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

以上就是XML  实体引用、注释、命名规则 的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

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

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

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)

Commenting Out Code in PHP Commenting Out Code in PHP Jul 18, 2025 am 04:57 AM

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

Good vs. Bad PHP Comments Good vs. Bad PHP Comments Jul 18, 2025 am 04:55 AM

Comments are crucial in code because they improve the readability and maintenance of the code, especially in projects like PHP that are multi-collaborative and long-term maintenance. Reasons for writing comments include explaining “why do this” to save debugging time and be friendly to newbies and reduce communication costs. The representation of good comments includes explaining the role of functions or classes to explain complex logic intent marking to-dos or potential problems, and writing API interface documentation annotations. Typical manifestations of bad comments include repeated code content comments that are inconsistent with code and using comments to cover up bad code and retaining old information. Suggestions for writing comments include prioritizing comments "why" keeping comments synced with code Use a unified format to avoid emotional statements and consider optimizing code rather than relying on comments when the code is difficult to understand.

How to add a comment to a table or column in SQL? How to add a comment to a table or column in SQL? Sep 21, 2025 am 05:22 AM

UseCOMMENTONCOLUMNorALTERTABLEwithCOMMENTtodocumenttablesandcolumnsinSQL;syntaxvariesbyDBMS—PostgreSQLandOracleuseCOMMENTON,MySQLusesCOMMENTinCREATE/ALTERstatements,andcommentscanbeviewedviasystemtableslikeINFORMATION_SCHEMA,butSQLitelackssupport.

XML Data Binding with Castor in Java XML Data Binding with Castor in Java Aug 15, 2025 am 03:43 AM

CastorenablesXML-to-Javaobjectmappingviadefaultconventionsorexplicitmappingfiles;1)DefineJavaclasseswithgetters/setters;2)UseUnmarshallertoconvertXMLtoobjects;3)UseMarshallertoserializeobjectsbacktoXML;4)Forcomplexcases,configurefieldmappingsinmappin

Working with XML attributes vs. elements: design choices Working with XML attributes vs. elements: design choices Sep 14, 2025 am 01:21 AM

UseattributesformetadatasuchasID,status,orunit,whichdescribetheelementbutarenotcorecontent,ensuringsimplicityandcompactnesswhendataisatomic.2.Useelementsforactualdatacontent,especiallywhenitmayrequirestructure,repetition,extensibility,orfuturenesting

Working with JSON and XML Serialization in C# Working with JSON and XML Serialization in C# Jul 31, 2025 am 04:12 AM

The choice of JSON or XML depends on the application scenario: 1. The situation of using JSON includes WebAPI return data, front-end interaction, modern service communication, and lightweight configuration; 2. The situation of using XML includes legacy system compatibility, namespace support, document-based data structures, and enterprise-level application interface specifications. In C#, .NETCore uses System.Text.Json for JSON serialization by default, with better performance and supports formatted output and null value retention; XML is implemented through XmlSerializer, suitable for old projects, and can customize tag names and namespaces, but does not support circular references, and needs to be processed manually or replaced with other libraries. Rationally select and configure serialization methods to help deal with different developments

How do you add comments in HTML? How do you add comments in HTML? Sep 21, 2025 am 06:42 AM

HTML comments use syntax, and the browser ignores the contents. 1. Used to add instructions, such as; 2. You can temporarily comment code, such as; 3. Support multi-line comments, but cannot be nested, and avoid using --> in comments, otherwise it will cause the comment to end in advance, and the comments are only visible in the source code and end with a complete sentence.

A look at the SimpleXML library in PHP for easy XML manipulation A look at the SimpleXML library in PHP for easy XML manipulation Jul 27, 2025 am 01:06 AM

SimpleXMListherighttoolforstraightforwardXMLmanipulationinPHP,asitconvertsXMLintoeasy-to-navigatePHPobjects.1.ItallowsloadingXMLfromastringorfileusingsimplexml_load_string()orsimplexml_load_file().2.Elementsareaccessedlikeobjectproperties,andattribut

Related articles