以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  [原创]深入学习Schema(1):如何定义属性节点  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=7139)


--  作者: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值。


--  作者:hyandlsz
--  发布时间:6/3/2004 9:10:00 PM

--  
en
--  作者:init.ora
--  发布时间:9/3/2004 9:44:00 AM

--  

--  作者:jeffrey_zheng
--  发布时间:9/4/2004 3:18:00 PM

--  
好,顶上
--  作者:walter800
--  发布时间:9/4/2004 11:07:00 PM

--  
good topic
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms