-- 作者:teiki
-- 发布时间:4/30/2004 12:15:00 AM
-- [原创]深入学习Schema(1):如何定义属性节点
在XML Schema中定义属性节点要用到元素<xsd:attribute> 比如以下这个购书文档 order.xml ---------- <?xml version="1.0" encoding="GB2312" ?> <order:books xmlns:order="urn:orderList" name="购书清单"> <book isbn="ISBN7-5058-3486-7"> <title>税法</title> <author>CPA考试税法教材编写组</author> <published>经济科学出版社</published> <price>31.00</price> <publishDate>2003/04/18</publishDate> </book> <book isbn="ISBN7-5005-6450-3"> <title>会计</title> <author>CPA考试会计教材编写组</author> <published>中国财政经济出版社</published> <price>34.00</price> <publishDate>2003/04/16</publishDate> </book> ……省略内容…… </order:books> 我们可以写一个这样的文档来定义其中的属性节点。 order.xsd ----------------- <?xml version="1.0" encoding="GB2312" ?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="books"> <xsd:complexType> <xsd:sequence> <xsd:element name="book" type="book_type" minOccurs="0" maxOccurs="unbounded" /> </xsd:sequence> <xsd:attribute name="name" type="xsd:string" use="optional" /> </xsd:complexType> </xsd:element> <xsd:complexType name="book_type"> <xsd:sequence> <xsd:element name="title" type="xsd:string" /> <xsd:element name="author" type="xsd:string" /> <xsd:element name="published" type="xsd:string" /> <xsd:element name="price" type="xsd:positiveInteger" /> <xsd:element name="publishDate" type="xsd:date" /> </xsd:sequence> <xsd:attribute name="isbn" type="xsd:string" use="required" /> </xsd:complexType> </xsd:schema> 有关Schema的基本定义方法可以参考本版面的另一篇入门文章-《跟我学Schema》 这里,我们着重看一下<xsd:attribute>元素的"use"属性。 use属性用于指示属性是必需的还是可选的,通常可能是以下几种值: required: 这代表该属性是必须的 optional:这代表该属性是可选的 deflaut: 代表缺省时用value预设值做默认值。比如:<xsd:attribute name="category" type="xsd:string" use="default" value="other" /> fixed: 代表固定值。比如:<xsd:attribute name="category" type="xsd:string" use="default" value="selected" /> use值为deflaut或者fixed时,必须要有相应的value值。
|