XML:DTD:Schema

1.从xml的文档结构最高可抽象到纵横两个方面:节点 和 属性

<topnode name="top">
<midtop name="mid"/>
</topnode>

2.DTD的约束

I.对节点:

对节点的约束,很明显要做到描述 节点的结构,类型等等

<!ELEMENT NAME (TYPE)>

在DTD中所有的这些 描述都由 上面的 TYPE 完成

看描述例子:

A.<book>my book</book> ---DTD---> <!ELEMENT book (#PCDATA)>

B.<book/> ---DTD---> <!ELEMENT book EMPTY>

C.<book>
<title>mybook</title>
<author>yife</author> ---DTD---> <!ELEMENT book (title,author)>
</book>

*#PCDATA代表字符数据

不用多解释,应该能初步体会,不过现在对上面第三种情况展开:

D.<book>
<title>mybook</title>
<chapter>NO.1</chapter>
...
unknow ,the number will be change sometimes
<chapter>NO.n</chapter>
</book>

---DTD---> <!ELEMENT book (title,chapter+)>

这里给出通配表
[DEFAULT]:MUST BE 1 TIME(1)
? :MUST BE 1 TIME OR 0(1|0)
+ :1 TIMES OR MORE(1....n)
* :0 TIMES OR MORE(0....n)

E. <chapter>NO.1</chapter>
<chapter>
<havechild/>
</chapter>

---DTD---> <!ELEMENT BOOK (#PCDATA|(havechild))>

*这里符号"|"是or的含义

II.对属性:

属性是绑定在节点上的,属性没有结构描述
<!ATTILIST ELEMENT ATTRIBUTE TYPE MODIFER>

看例子:

A.<book focus="XML"/> ---DTD---> <!ATTILIST book focus CDATA #REQUIRED>

*CDATA表示是字符的
*#REQUIRED表示是必须的 #IMPLED表示可以省的 #FIX表示不能改动的

属性除了字符的,还可以是枚举的

B.<book focus="XML"/>
<book focus="JAVA"/> ---DTD---> <!ATTILIST book focus (XML|JAVA) #IMPLED>

C.<book focus="XML">me</book>
.....
<book focus="XML">you</book>

---DTD---> <!ATTILIST book focus (XML|JAVA) "XML">

*"XML"为缺省值,这是缺省的表示方法

2.Schema的约束

I.对节点:(Schema也符合xml标准)

<element name="NAME" type="TYPE" [options...]>

here TYPEs is more than DTD ,very like java

看描述例子:

A.<book>my book</book> -Schema--> <element name="book" type ="String" />

B.<book>
<title>mybook</title>
<author>yife</author>
</book>

-Schema-->

<element name="book" type ="booktype" />
<complexType name="booktype">
<element name="title" type="string">
<element name="author" type="string">
</complexType>

C.还是上个例子还可以这样写(希望可以思考一下,两种写法有什么不同)
<element name="book">
<complexType>
<element name="title" type="string">
<element name="author" type="string">
</complexType>
</element>

一个特殊的例子:

D.<author/>

-Schema-->

<element name="author">
<complexType content="booktype" />
</element>

还记得 DTD 中的<!ELEMENT book (title,chapter+)>吗?"+" 在 Schema 的表示:

E.<element name="chapter" type="String" minOccurs="1" maxOccurs="*">

其实minOccurs="1" 不是必须的 因为 默认值是1

II.对属性:

<attribute name="NAME" type="TYPE" [options]>

看例子:

A.<book focus="XML"/>

-Schema-->

<element name="book">
<complexType>
<attribute name="focus" type="string" />
</complexType>
</element>

*attribute在这里看来是 complextype 里的一个条目.

Schema 里没有 #REUQIRED 所以 为了表示 这个含义:

B.<attribute name="focus" type="string" minOccurs="1" />

与表示节点不同,这里minOccurs 的默认值是 0.

C.<attribute name="focus" type="string" default="xml" /> 默认值的表示方法

D.schema 表示 <!ATTILIST book focus (XML|JAVA) "XML">

<attribute name="focus" >
<simpleType base="string">
<enumeration value="XML">
<enumeration value="JAVA">
</simpleType>
</attribute>

3.xml 使用:

Schema:
<book xmlns:xsi="http://www.w3.org/1999/XMLSchema/instance"
xmlns="you name space"
xsi:schemaLocation="you xsd file location "
>

DTD:
<!DOCTYPE book SYSTEM "PATH TO DTD file">

posted @ 2006-09-27 09:30  lanjue  阅读(292)  评论(0编辑  收藏  举报