学院首页>网页制作>XML>XML Schema学习笔记

XML Schema学习笔记

作者: 来源: 添加时间:2006-5-21 20:22:56
9、对元素的定义可以采用通过指定其type属性为已定义的属性的方式,也可一采用匿名定义类型的方式,如:
采用类型定义:
<xsd:element name=”comment” type=”xsd:string”>
采用匿名定义:
<xsd:element name=”quantity”>
<xsd:simpleType>
<xsd:restriction base=”xsd:positiveInteger”>
<xsd:maxExclusive value=”100” />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>

10、union(联合)类型表示在XML实例文档中的元素实例符合union类型定义的成员类型中的一种就可以了(合法),这一点和C++中的联合类型有类似的概念,如:
<xsd:simpleType name="addrUnion">
<xsd:union memberTypes="xsd:string integer"/>
</xsd:simpleType>

11、复杂类型一般可以分为三类:第一类是包含字符内容和属性但不包含子元素;第二类是包含属性和子元素但不包含字符数据(字符数据包含在子元素中);第三类是即包含属性和字符内容又包含子元素的;那么如何来定义这三类类型呢?针对第一类可以通过simpleContent来实现,第二类可以通过complexContent来做到,第三类只需要将complexType的属性mixed设为true就可以了。具体的例子如下:

第一种类型(从一个简单类型扩展而来,增加了属性):
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="currency" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>

第二种类型(有一个element和两个attribute构成):
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:complexContent>
<xsd:element name=”Country” ?type=”xsd:string” />
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value"?type="xsd:decimal"/>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
注意:在这里由于默认情况下缺省是complexContent,所以在这里简略的写法是:
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:element name=”Country”type=”xsd:string” />
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value"?type="xsd:decimal"/>
</xsd:complexContent>
</xsd:element>

第三种类型:
<xsd:element name="letterBody">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="salutation">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="quantity"type="xsd:positiveInteger"/>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="shipDate"type="xsd:date" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
第三种类型的实例可能如下:
<letterBody>
<salutation>Dear Mr.<name>Robert Smith</name>.</salutation>
Your order of <quantity>1</quantity> <productName>Baby
Monitor</productName> shipped from our warehouse on
<shipDate>1999-05-21</shipDate>
</letterBody>

12、根据11的描述那么要定义一个空内容的元素,也就是说定义一个只包含属性的元素,只要在complexContent中不包含任何子元素,就可以了,如:
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value"type="xsd:decimal"/>
</xsd:complexType>
</xsd:element>

13、anyType是所有Schema类型的基类型,和Java中的Object类似。因此,以下定义:
<xsd:element name="anything" type="xsd:anyType"/>
可以写成:
<xsd:element name="anything"/>

14、Schema中用annotation、document、appInfo三个元素来进行注释,其中appI和document都是作为annotation的子元素来处理的。并且annotation一般是作为schema的顶层子元素、element的构造、类型定义的顶层子元素的。
如:
<xsd:element name="internationalPrice">
<xsd:annotation>
<xsd:documentation xml:lang="en">
element declared with anonymous type
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:annotation>
<xsd:documentation xml:lang="en">
empty anonymous type with 2 attributes
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value"type="xsd:decimal"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

15、choice仅允许在实例文档中使用其中一个子元素;在all中的所有元素都可以出现一次或一次都不出现,并且其中元素实例是没有顺序约束的,而且all必须放在任何内容模型的最顶层,为了说明这个问题,下面先列出一个合法的,然后列出一个不合法的以供对照说明:
<xsd:complexType name="PurchaseOrderType">
<xsd:all>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items"type="Items"/>
</xsd:all>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
下面是一个不合法的:
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:all>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element name="items"type="Items"/>
</xsd:all>
<xsd:sequence>
<xsd:element ref="comment" />
</xsd:sequence>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
第 2 页,共 3 页 [1] [2] [3]
站内搜索