XML介紹
DTDs
Introduced as part of the XML 1.0 specification, DTDs are the oldest constraint model around in the XML world. They're simply to use, but this simplicity comes at a price: DTDs are inflexible, and offer you little for data type validation as well.
XML Schema (XSD)
XML Schema is the W3C's anointed successor to DTDs. XML Schemas are literally orders of magnitude more flexible than DTDs, and offer an almost dizzying array of support for various data types. However, just as DTDs were simple and limited, XML Schemas are flexible, complex, and (some would argue) bloated. It takes a lot of work to write a good schema, even for 50- or 100-line XML documents. For this reason, there's been a lot of dissatisfaction with XML Schema, even though they are widely being used.
[prefix]:[element name]
元素:
root元素必須包含所有文檔中的元素,只能有一個root元素。元素名只能以下劃線或字母開頭,不能有空格,區(qū)分大小寫。開元素必須有對應(yīng)閉元素(也有類似html的簡寫,如<img src="/images/xml.gif" />)。文檔由DTD或schema來限制它是否合格。
屬性:
什么時候用屬性?基本原則:多個值的數(shù)據(jù)用元素,單值的數(shù)據(jù)用元素。如果數(shù)據(jù)有很多值或者比較長,數(shù)據(jù)最可能屬于元素。他主要被當(dāng)作文本,容<rss:author>Doug Hally</rss:author> <journal:author>Neil Gaiman</journal:author>易搜索,好用。比如一本書的章節(jié)描述。然而如果數(shù)據(jù)主要作為單值處理的話,最好作為屬性。如果搞不清楚,可以安全的使用元素。
命名空間Namespaces:
xml的命名空間是一種用一個特定的URI來關(guān)聯(lián)XML文檔里的一個或多個元素的方法。意味著元素是由名字和命名空間一起來識別的。許多復(fù)雜的XML文件里,同一個名字會有多個用途。比如,一個RSS feed有一個作者,這個作者同時是每個日記的。雖然這些數(shù)據(jù)都用author元素來表示,但他們不應(yīng)該被當(dāng)作同一個類型的數(shù)據(jù)。命名空間很好的解決了這個問題,命名空間說明書要求一個前綴和唯一的URI聯(lián)合起來區(qū)分不同命名空間里的元素。如http://www.neilgaiman.com/entries作為URI,聯(lián)合前綴journal用來表示日志相關(guān)的元素。
<rdf:RDF xmlns:rss="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:journal="http://www.neilgaiman.com/entries">,然后就可使用了:
<rss:author>Doug Hally</rss:author> <journal:author>Neil Gaiman</journal:author>實際上在使用命名空間前綴的時候再定義也可以的:
<rss:author xmlns:rss="http://www.w3.org/1999/02/22-rdf-syntax-ns#">Doug Hally</rss:author>
如果名字沒有命名空間,不代表在默認命名空間中,而是xml處理器以在任何命名空間以外方式解釋他。要聲明默認命名空間的話就不用后面冒號部分,如<Insured xmlns="使用的一些術(shù)語:
-
The name of a namespace (such as http://www.ibm.com/software) is the namespace URI.
-
The element or attribute name can include a prefix and a colon (as in prod:Quantity). A name in that form is called a qualified name, or QName, and the identifier that follows the colon is called a local name. If a prefix is not in use, neither is the colon, and the QName and local name are identical.
-
An XML identifier (such as a local name) that has no colon is sometimes called an NCName. (The NC comes from the phrase no colon.)
Entity references:
用來處理轉(zhuǎn)義字符,語法是& [entity name] ;XML解析器碰到這種entity reference,就會用對應(yīng)的值替換掉他。如<(<),>(>),&(&),"("),'(')。注意entity reference是用戶可定義的。比如多處用到版權(quán)提示,自己定義后以后更改就方便了:<ora:copyright>&OReillyCopyright;</ora:copyright>。除了用來表示數(shù)據(jù)中的復(fù)雜或特殊字符外,entity reference還會有更多用途。
不解析的數(shù)據(jù):
當(dāng)傳輸大量數(shù)據(jù)給應(yīng)用且不用xml解析的時候,CDATA就有用了。當(dāng)大量的字符需要用entity reference轉(zhuǎn)義的時候,或空格必須保留的時候,使用CDATA。<![CDATA[….]]>
posted on 2011-03-24 09:05 yuxh 閱讀(317) 評論(0) 編輯 收藏 所屬分類: XML 、web service 、SOA