笔记20051101

模式定义元素和属性,就像DTD所做的那样。将一个类型与元素或属性相联系则可定义内容。类型可以是XML Schema规范中内置的,如整数,也可以是自己构造的,如telephoneNumber或properNoun。


一个例子:
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xsd:schema
 3   xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
 4   elementFormDefault="qualified"
 5 >
 6   <xsd:element name="Recipe">
 7     <xsd:annotation>
 8       <xsd:documentation>
 9         A Recipe from the 1782 English HouseKeeper book
10       </xsd:documentation>
11     </xsd:annotation>
12 
13     <xsd:complexType>
14       <xsd:sequence>
15         <xsd:element ref="title" />
16         <xsd:element ref="Body" />
17       </xsd:sequence>
18     </xsd:complexType>
19   </xsd:element>
20 
21   <xsd:element name="title" type="xsd:string">
22     <xsd:annotation>
23       <xsd:documentation>The title of a recipe</xsd:documentation>
24     </xsd:annotation>
25   </xsd:element>
26 
27   <xsd:element name="Body">
28     <xsd:annotation>
29       <xsd:documentation>contaner for the content</xsd:documentation>
30     </xsd:annotation>
31     <xsd:complexType mixed="ture">
32       <xsd:choice>
33         <xsd:element ref="ingredient" minOccurs="0" maxOccurs="unbounded" />
34       </xsd:choice>
35     </xsd:complexType>
36   </xsd:element>
37 
38   <xsd:element name="ingredient" type="xsd:string">
39   <xsd:annotation>
40     <xsd:documentation>A single ingredient</xsd:documentation>
41   </xsd:annotation>
42   </xsd:element>
43 </xsd:schema>
说明:该文档的主元素是schema元素。title被赋予一个xsd:string类型。Body元素将其mixed属性置为true,意味Body元素具有混合内容:文本和元素;其只有一种元素ingredient,最少0个,最多无穷。


DTD中element元素定义:
1 <!ELEMENT element
2   (
3     annotation?,
4     (complexType|simpleType)?,
5     (unique|key|keyref)*
6   )
7 >
元素的内容可以是简单的也可以是复杂的。复杂的类型包含其他元素,简单类型则不然。如果类型是预置的,那么对元素element使用属性type。如果它是自己在别处定义过的一个类型,那么可以用同样方式引用它。否则,可以为该元素立即定义一个类型,使用simpleType;如果它可能包含子元素,则使用complexType。Unique、key和keyref类似于数据库里的作用。如元素的内容必须是唯一的,即元素的值用做检索的索引关键字,或该值必须出现在另一个定义为关键字的元素中。

自定义类型示例:电话号码类型(telephoneNumber)
 1 <xsd:complexType name="telephoneNumber">
 2 <xsd:choice>
 3 <xsd:sequence>
 4 <xsd:element name="areacode">
 5 <xsd:simpleType>
 6 <xsd:restriction base="xsd:integer">
 7 <xsd:maxInclusive value="999" />
 8 </xsd:restriction>
 9 </xsd:simpleType>
10 </xsd:element>
11 <xsd:element name="exchange">
12 <xsd:simpleType>
13 <xsd:restriction base="xsd:integer">
14 <xsd:maxInclusive value="999" />
15 </xsd:restriction>
16 </xsd:simpleType>
17 </xsd:element>
18 <xsd:element name="number">
19 <xsd:simpleType>
20 <xsd:restriction>
21 <xsd:maxInclusive value="999" />
22 </xsd:restriction>
23 </xsd:simpleType>
24 </xsd:element>
25 <xsd:element name="extension">
26 <xsd:simpleType>
27 <xsd:restriction>
28 <xsd:maxInclusive value="999" />
29 </sd:restriction>
30 </xsd:simpleType>
31 </xsd:element>
32 </xsd:sequence>
33 <xsd:sequence>
34 <xsd:element name="contrycode" type="xsd:string" />
35 <xsd:element name="number" type="xsd:string" />
36 <xsd:element name="extension" type="xsd:string" />
37 </xsd:sequence>
38 </xsd:choice>
39 </xsd:complexType>

 

内容原型:
1。字母和数字,例<xsd:pattern value="hello" />,该元素的内容必须是hello。
2。a|b|c,恰好匹配a、b或c之一;a、b、c可以用任何原型替代。
3。(a),与a相同,可以使用括号分组。
4。句号(.),匹配所有除新行符(\n)或回车符(\r)以外的任何单个字符。如b.y匹配boy等。
5。a?,匹配a或什么也不匹配。原型wine?d匹配wind和wined。原型(dis)?comfort匹配comfort 或discomfort。
6。a*,匹配0个或多个a。
7。a+,匹配1个或多个a。
8。a{2,5},匹配2-5个a。也可以是a{2,},匹配两个或以上的a;a{5},匹配5个a。
9。[w-z],匹配单个字符w、x、y或z;[0-9],匹配一个一位数。
10。[^aeiouy0-9],匹配除与[aeiouy0-9]匹配以外的任何串。
11。\r,\n,\t,字符序列分别匹配回车符(&#13;)、新行符(&#10;)和跳格符(&#8;)。


当想在XML中创建从一个文档到另一个文档的超文本链接有两种方法:
1。调用XHTML名域并使用HTML链接。例如,
    <html xmlns:h="http://www.w3.org/TR/xhtml">
      <h:a href="ok.html">this works.</h:a>
    </html>
2。XML Linking Language(XLink)。例如,
    <p xmlns:xlink="http://www.w3.org/1999/xlink">
      <a xlink:type="simple" xlink:href="ok.xml">this works, too.</a>
    </p>


XLink特点:
1。与不止两个端点链接。可以使用XLink创建连接不止两点的链接。
2。多向链接。对HTML,链接有一个锚和一个目标,文档中一般不存在指向当前web页面的任何迹象。你可    以使用XLink创建双向链接。
3。可以使用外部链接集文档。
4。注解链接。

posted on 2005-11-01 21:36  Harry  阅读(453)  评论(0)    收藏  举报