使用W3C XML Schema(5)

来源:互联网 发布:客户达软件怎么样 编辑:程序博客网 时间:2024/05/16 14:48

内容类型

在文章的第一部分,我们检查了默认的内容类型行为,在面向数据的文档后建模,发现复杂类型的元素只是元素和属性,并且简单类型的元素是没有属性的字符数据。

W3C XML Schema 定义语言也支持定义空白内容元素,以及具有属性的简单内容(它们只包含字符串数据)。

空白内容元素使用xs:complexType结构并有意的忽略定义子元素。下列结构定义一个空白book元素并接受一个isbn属性。

<xs:element name="book"> 
<xs:complexType> 
<xs:attribute name="isbn" type="isbnType"/> 
</xs:complexType> 
</xs:element> 

简单的内容元素,例如,具有属性的字符串数据元素,可以使用xs:simpleContent从简单类型派生。上面定义的book元素因此被扩展成可以接受一个文本数据:

<xs:element name="book"> 
<xs:complexType> 
<xs:simpleContent> 
<xs:extension base="xs:string"> 
<xs:attribute name="isbn" type="isbnType"/> 
</xs:extension> 
</xs:simpleContent> 
</xs:complexType> 
</xs:element> 

注意属性定义的位置,显示扩展点是通过扩展属性完成的。这个定义将会接受下面的XML元素:

<book isbn="0836217462"> 
Funny book by Charles M. Schulz. 
Its title (Being a Dog Is a Full-Time Job) says it all ! 
</book>

W3C XML Schema在 xs:complexType 元素中通过混合属性支持混合的内容。考虑下:

<xs:element name="book"> 
<xs:complexType mixed="true"> 
<xs:all> 
<xs:element name="title" type="xs:string"/> 
<xs:element name="author" type="xs:string"/> 
</xs:all> 
<xs:attribute name="isbn" type="xs:string"/> 
</xs:complexType> 
</xs:element> 

将验证这样的XML文件,如下:

<book isbn="0836217462"> 
Funny book by 
<author>Charles M. Schulz</author>
Its title (
<title>Being a Dog Is a Full-Time Job</title>) says it all ! 
</book> 

不像DTD那样,W3C XML Schema 混合内容不修改子元素的约束,简单内容模型可以使用相同的方式表示。虽然这是较XML 1.0 DTD重要的改进,但注意字符数据值以及其相对子元素的位置,其不能应用约束。

 
原创粉丝点击