XML(二)--扩展知识

这篇文章是关于XML常见知识的高度概括,更具体的细节参考每个知识点下的链接。

包含:

XML Schema or XSD (XML Schema Definition)

XML DTD

XML DOM

XML CSS

XML XSLT

XML CDATA

XML Namespaces

XML parser

XML Schema or XSD & XML DTD

http://www.w3schools.com/dtd/default.asp

http://www.w3schools.com/schema/default.asp

XML schema和DTD(Document Type Definition)产生的目的是为了验证XML是否合法。

首先,一个符合XML语法的xml文件称为"Well Formed" XML。如果一个xml文件不符合XML的语法,就是一个非法的xml文件。

其次,如果想验证一个XML中必须包含哪些element,不能包含哪些element,要有哪些层次结构,仅仅是符合语法就不能满足要求了。

有两种方式来验证:XML DTD和XML schema。其中XML schema是用于替换XML DTD 的。

XML schema也被称为我们经常见到的XML Schema Definition (XSD).


XML Namespaces

http://www.w3schools.com/xml/xml_namespaces.asp

XML Namespaces provide a method to avoid element name conflicts.

In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.

When using prefixes in XML, a so-called namespace for the prefix must be defined.

The namespace declaration has the following syntax.xmlns:prefix="URI".

(A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource.

URL就是URL的一种,另外还有一个不太常见的是Universal Resource Name (URN).)

<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr> //当父element定义了namespace之后,子element将从父那里继承。因此这里的h:可以省略掉。
    <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>

其实 xmlns:prefix="URI"中的URI的内容并不重要,并不会使用URI中的内容做任何验证。

However, often companies use the namespace as a pointer to a web page containing namespace information.


XML DOM & XML parser

http://www.w3schools.com/dom/default.asp

DOM的内容比较多。而HTTP也有自己的DOM,可以参考:

http://www.w3schools.com/htmldom/default.asp

Document Object Model (DOM) 包含三部分:CORE DOM, XML DOM, HTML DOM.

The XML DOM defines the objects and properties of all XML elements, and the methods (interface) to access them.  In other words: The XML DOM is a standard for how to get, change, add, or delete XML elements.

The DOM presents an XML document as a tree-structure.

According to the DOM, everything in an XML document is a node.

The DOM says:

  • The entire document is a document node
  • Every XML element is an element node // 像<book Title=“Seven Jobs”>30dollers</book>这样一行是一个element。
  • The text in the XML elements are text nodes //上例中的30dollers就是text node.
  • Every attribute is an attribute node //Title=“Seven Jobs"就是attribute node
  • Comments are comment nodes //指<!-- --> 括起来的部分
DOM是树状结构的,即父node包含子node,整个document就是一个node-tree。例如<book> node包含text node " 30dollers"。这里有一个常见的错误是,上例中," 30dollers"不是element node <book>的值,而是它包含的一个text node。

因此DOM定义了一个完整的node-tree。

The XML DOM contains methods (functions) to traverse XML trees, access, insert, and delete nodes.

However, before an XML document can be accessed and manipulated, it must be loaded into an XML DOM object.

An XML parser reads XML, and converts it into an XML DOM object that can be accessed with JavaScript.

Most browsers have a built-in XML parser.

一旦 XML parser将XML 转换成XML DOM后,javascript就可以使用DOM提供的添加/删除 等等node的功能来任意的操作XML文件了。

但是需要注意的是:

浏览器是一个容器,javascript被限制在这个容器里。只有浏览器暴露给javascript DOM对象时,javascript才能去操纵它。

而DOM这个对象,绝不仅仅是给javascript使用的。


XML CDATA

http://www.w3schools.com/xml/xml_cdata.asp

All text in an XML document will be parsed by the parser.  But text inside a CDATA section will be ignored by the parser.

知道javascript的同学知道,javascript是脚本语言,并且代码是嵌入在HTML中的。服务器返回给浏览器的HTML大部分是符合XML标准的,同时里面包含了javascript的代码。

浏览器接收到这样的HTML后,会自动运行其中包含的javascript的代码,达到动态的效果。

而javascript代码中可能包含很多<和&字符,这样的字符在XML中是不合法的。因此就使用XML CDATA来括住javascript代码,这样XML parser就不会将这些code解释为XML DOM的一部分了。

A CDATA section starts with "<![CDATA[" and ends with "]]>":


XML CSS

http://www.w3schools.com/xml/xml_display.asp

With CSS (Cascading Style Sheets) you can add display information to an XML document.

CSS file 和 XML文件可以是两个独立的文件,也可以将CSS代码嵌入到XML中。

例如下面这个XML文件,其中引入了一个css文件。这个css文件中标明了每个element显示时的字体,大小,颜色等等。

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CATALOG>

</CATALOG>

使用CSS format之后的XML 文件就会以要求的样子展现出来。


现在CSS在展示html时非常重要。大家可以参考我的另外一篇文章:HTML总论


XML XSLT

http://www.w3schools.com/xsl/default.asp

使用CSS并不是最常见的做法。更常见的是使用XSLT。

但是使用CSS和使用XSLT有一个本质的区别:

CSS仅仅是控制展示。也就是说,XML还是原来的XML,但是CSS改变了XML展示出来的样子。

但是XSLT是将XML转换成了其他的形式。比如我们可以用XSLT将XML转换成XHTML。

XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTMLelement.


XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.

XSLT is the recommended style sheet language of XML.

XSL stands for EXtensible Stylesheet Language. XSLT stands for XSL Transformations.

例如,如果在浏览器中打开一个HTML,而其实这个HTML是一个纯XML文件时,浏览器就会使用默认的XSLT展示XML。

用户将看到所有的XML节点都折叠起来,可以自己展开每个节点。

这就是XSLT应用的一个实际例子。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值