笔记

way

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元素。元素名只能以下划线或字母开头,不能有空格,区分大小写。开元素必须有对应闭元素(也有类似html的简写,如<img src="/images/xml.gif" />)。文档由DTD或schema来限制它是否合格。
属性:
什么时候用属性?基本原则:多个值的数据用元素,单值的数据用元素。如果数据有很多值或者比较长,数据最可能属于元素。他主要被当作文本,容<rss:author>Doug Hally</rss:author> <journal:author>Neil Gaiman</journal:author>易搜索,好用。比如一本书的章节描述。然而如果数据主要作为单值处理的话,最好作为属性。如果搞不清楚,可以安全的使用元素。
命名空间Namespaces:
xml的命名空间是一种用一个特定的URI来关联XML文档里的一个或多个元素的方法。意味着元素是由名字和命名空间一起来识别的。许多复杂的XML文件里,同一个名字会有多个用途。比如,一个RSS feed有一个作者,这个作者同时是每个日记的。虽然这些数据都用author元素来表示,但他们不应该被当作同一个类型的数据。命名空间很好的解决了这个问题,命名空间说明书要求一个前缀和唯一的URI联合起来区分不同命名空间里的元素。如http://www.neilgaiman.com/entries作为URI,联合前缀journal用来表示日志相关的元素。
<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="http://www.IBM.com/software">
使用的一些术语:
  • 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:
用来处理转义字符,语法是& [entity name] ;XML解析器碰到这种entity reference,就会用对应的值替换掉他。如&lt;(<),&gt;(>),&amp;(&),&quot;("),&apos;(')。注意entity reference是用户可定义的。比如多处用到版权提示,自己定义后以后更改就方便了:<ora:copyright>&OReillyCopyright;</ora:copyright>。除了用来表示数据中的复杂或特殊字符外,entity reference还会有更多用途。

不解析的数据
当传输大量数据给应用且不用xml解析的时候,CDATA就有用了。当大量的字符需要用entity reference转义的时候,或空格必须保留的时候,使用CDATA。<![CDATA[….]]>

posted on 2011-03-24 09:05 yuxh 阅读(294) 评论(0)  编辑  收藏 所属分类: XMLweb serviceSOA


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2011年3月>
272812345
6789101112
13141516171819
20212223242526
272829303112
3456789

统计

常用链接

留言簿

随笔分类

随笔档案

收藏夹

博客

搜索

最新评论

阅读排行榜

评论排行榜