小白教程
所有教程
关于
Search
172.70.100.218
172.70.100.218
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“XML 语法”的源代码
本页内容
上一节:
XML_树结构
下一节:
XML_元素
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:XML 语法}}[[Category:xml 教程|5]] = XML 语法规则 = XML 的语法规则很简单,且很有逻辑。这些规则很容易学习,也很容易使用。 == XML 文档必须有根元素 == XML 必须包含根元素,它是所有其他元素的父元素,比如以下实例中 root 就是根元素: <sample title="" desc="" lang="xml" hererun="1"> <root> <child> <subchild>.....</subchild> </child> </root> </sample> 以下实例中 note 是根元素: <sample title="" desc="" lang="xml" hererun="1"> <?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </sample> == XML 声明 == XML 声明文件的可选部分,如果存在需要放在文档的第一行,如下所示: <sample title="" desc="" lang="xml" hererun="1"> <?xml version="1.0" encoding="utf-8"?> </sample> <blockquote>以上实例包含 XML 版本( UTF-8 也是 HTML5, CSS, JavaScript, PHP, 和 SQL 的默认编码。 </blockquote> == 所有的 XML 元素都必须有一个关闭标签 == 在 HTML 中,某些元素不必有一个关闭标签: <sample title="" desc="" lang="xml" hererun="1"> <p>This is a paragraph. <br> </sample> 在 XML 中,省略关闭标签是非法的。所有元素都'''必须'''有关闭标签: <sample title="" desc="" lang="xml" hererun="1"> <p>This is a paragraph.</p> <br /> </sample> '''注释:'''从上面的实例中,您也许已经注意到 XML 声明没有关闭标签。这不是错误。声明不是 XML 文档本身的一部分,它没有关闭标签。 == XML 标签对大小写敏感 == XML 标签对大小写敏感。标签 <Letter> 与标签 <letter> 是不同的。 必须使用相同的大小写来编写打开标签和关闭标签: <sample title="" desc="" lang="xml" hererun="1"> <Message>这是错误的</message> <message>这是正确的</message> </sample> '''注释:'''打开标签和关闭标签通常被称为开始标签和结束标签。不论您喜欢哪种术语,它们的概念都是相同的。 == XML 必须正确嵌套 == 在 HTML 中,常会看到没有正确嵌套的元素: <sample title="" desc="" lang="xml" hererun="1"> <b><i>This text is bold and italic</b></i> </sample> 在 XML 中,所有元素都'''必须'''彼此正确地嵌套: <sample title="" desc="" lang="xml" hererun="1"> <b><i>This text is bold and italic</i></b> </sample> 在上面的实例中,正确嵌套的意思是:由于 <i> 元素是在 <b> 元素内打开的,那么它必须在 <b> 元素内关闭。 == XML 属性值必须加引号 == 与 HTML 类似,XML 元素也可拥有属性(名称/值的对)。 在 XML 中,XML 的属性值必须加引号。 请研究下面的两个 XML 文档。 第一个是错误的,第二个是正确的: <sample title="" desc="" lang="xml" hererun="1"> <note date=12/11/2007> <to>Tove</to> <from>Jani</from> </note> </sample> <sample title="" desc="" lang="xml" hererun="1"> <note date="12/11/2007"> <to>Tove</to> <from>Jani</from> </note> </sample> 在第一个文档中的错误是,note 元素中的 date 属性没有加引号。 == 实体引用 == 在 XML 中,一些字符拥有特殊的意义。 如果您把字符 "<" 放在 XML 元素中,会发生错误,这是因为解析器会把它当作新元素的开始。 这样会产生 XML 错误: <sample title="" desc="" lang="xml" hererun="1"> <message>if salary < 1000 then</message> </sample> 为了避免这个错误,请用'''实体引用'''来代替 "<" 字符: <sample title="" desc="" lang="xml" hererun="1"> <message>if salary < 1000 then</message> </sample> 在 XML 中,有 5 个预定义的实体引用: {| class="table table-striped table-hover" | &lt; | < | less than |- | &gt; | > | greater than |- | &amp; | & | ampersand |- | &apos; | ' | apostrophe |- | &quot; | " | quotation mark |} '''注释:'''在 XML 中,只有字符 "<" 和 "&" 确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。 == XML 中的注释 == 在 XML 中编写注释的语法与 HTML 的语法很相似。 <sample title="" desc="" lang="xml" hererun="1"> <!-- This is a comment --> </sample> == 在 XML 中,空格会被保留 == HTML 会把多个连续的空格字符裁减(合并)为一个: {| class="table table-striped table-hover" |width="50%"| HTML: |width="50%"| <sample title="" desc="" lang="xml" hererun="1"> Hello Tove </sample> |- | 输出结果: | Hello Tove |} 在 XML 中,文档中的空格不会被删减。 == XML 以 LF 存储换行 == 在 Windows 应用程序中,换行通常以一对字符来存储:回车符(CR)和换行符(LF)。 在 Unix 和 Mac OSX 中,使用 LF 来存储新行。 在旧的 Mac 系统中,使用 CR 来存储新行。 XML 以 LF 存储换行。
返回至“
XML 语法
”。
上一节:
XML_树结构
下一节:
XML_元素