XML

XML 并非 HTML 的替代, 它只是纯文本, 它相对于软硬件都是独立的, 它在 1998.10 被 W3C 推荐使用.

XML 衍生

XML被用于创建了不少新的互联网语言, 如:

  • XHTML the latest version of HTML
  • WSDL for describing available web services
  • WAP and WML as markup languages for handheld devices
  • RSS languages for news feeds
  • RDF and OWL for describing resources and ontology
  • SMIL for describing multimedia for the web

XML 文本的 well-formed

XML 文档中第一行的声明内容并不属于 XML 文档本身, 它没有闭合标签.

  • XML documents must have a root element
  • XML elements must have a closing tag
  • XML tags are case sensitive
  • XML elements must be properly nested
  • XML attribute values must be quoted

有关XML的验证, 另立了一页专述: XML 验证的一个例子

实体引用

There are 5 predefined entity references in XML:

  1. &lt; < less than
  2. &gt; > greater than
  3. &amp; & ampersand
  4. &apos; ' apostrophe
  5. &quot; " quotation mark

Comments in XML. The syntax for writing comments in XML is similar to that of HTML. <!-- This is a comment -->

换行

XML 用 LF 新起一行, 和其他系统对比如下:

XML Naming Rules

XML elements must follow these naming rules:

  • Names can contain letters, numbers, and other characters
  • Names cannot start with a number or punctuation character
  • Names cannot start with the letters xml (or XML, or Xml, etc)
  • Names cannot contain spaces

Any name can be used, no words are reserved. 名字中避免使用冒号(:), 中连线(-), 和半角句号(.). Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.

属性

Attribute values must always be enclosed in quotes, but either single or double quotes can be used.

If the attribute value itself contains double quotes you can use single quotes, like in this example:

<gangster name='George "Shotgun" Ziegler'>

or you can use character entities:

<gangster name="George &quot;Shotgun&quot; Ziegler">

属性还是元素?

There are no rules about when to use attributes and when to use elements. W3CSchool 建议尽量用元素, 并且列出了用属性可能存在的问题:

  • attributes cannot contain multiple values (elements can)
  • attributes cannot contain tree structures (elements can)
  • attributes are not easily expandable (for future changes)

Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.

XML 文档的显示

可以使用 CSS, XSLT and JavaScript 等技术来实现.

用CSS的话, 需要在XML文本中添加如下语句:

<?xml-stylesheet type="text/css" href="cd_catalog.css"?> (本例链接)

但是, XSLT is the recommended style sheet language of XML. 用XSL, 需要加如下语句:

<?xml-stylesheet type="text/xsl" href="simple.xsl"?> (本例链接)

Different browsers may produce different result when transforming XML with XSLT. To reduce this problem the XSLT transformation can be done on the server. 根据页面上附的例子, 是服务端把XML转化为XHTML, 再发送给客户端.

名字空间

When using prefixes in XML, a so-called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="http://www.w3schools.com/furniture">

  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
</root>

Namespaces can be declared in the elements where they are used or in the XML root element:

<root
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">

虽然 xmlns 的值是个 URI, 但其实没什么具体含义, 只是用来作唯一性的区分而已. 注意它的闭合格式: <x:y>*</x:y>. 中文文档有一篇参考: 这里.

疑问?(TODO) 对于自动生成的 web 工程, 其中的web.xml, 也有 namespace 的内容, 但基本上可以认为是没用的, 真是这样么? 去问问看.

DOM

The elements, their text, and their attributes are all known as nodes. DOM is a language-independent technology, and is implemented by Java, JS and etc.. 详细内容需要参考独立页面.

相关技术

在W3CSchool上的有一个列表, 列出了如下(包含但不限于)技术:

XHTML, XML DOM, CSS, XSL (XSLT, XSL-FO, XPath), XQuery, DTD, XSD, XLink, XPointer, XForms, SOAP, WSDL, RDF , RSS, WAP, SMIL , SVG.

关于数据的结构化记录,还有一种方式:JSON

其他

  1. TODO. 有关 XML JavaScript 部分, 暂时先跳一下, 待看了 JavaScript 部分之后再来看.
  2. TODO. 关于编码还是那个旧问题, XML 文档中声明了一个编码, 保存 XML 文件的时候再选了一种编码, 各有何用途