以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  schema中key、keyref怎么确定唯一性约束啊?  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=23986)


--  作者:cnsyEric
--  发布时间:11/6/2005 12:52:00 PM

--  schema中key、keyref怎么确定唯一性约束啊?
我在一个XML文档bookinfor.xml中引用了一个bookinfor.xsd的schema,schema中使用了key、keyref等来定义关键字,xml文档中的bookid有重复,在xml spy中验证为什么通过了呢?
bookinfor.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by  () -->
<!-- edited with XML Spy 2005  by Eric HONG Oct 29 2005 -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"; elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="BookInfor">
  <xs:annotation>
   <xs:documentation>The information of a book</xs:documentation>
  </xs:annotation>
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="BookItem" maxOccurs="unbounded"/>
    <xs:element ref="BookSort" maxOccurs="unbounded"/>
    <xs:element ref="BookQuantity" maxOccurs="unbounded"/>
   </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="BookItem">
  <xs:annotation>
   <xs:documentation>Basic infor of a book</xs:documentation>
  </xs:annotation>
  <xs:complexType>
   <xs:sequence>
    <xs:element name="BookId" type="xs:positiveInteger"/>
    <xs:element name="Nane" type="xs:string"/>
    <xs:element name="Sort" type="xs:positiveInteger"/>
    <xs:element name="Isbn" type="xs:string"/>
    <xs:element name="PubDate" type="xs:gYearMonth"/>
    <xs:element name="Author" type="xs:string"/>
    <xs:element name="Price">
     <xs:simpleType>
      <xs:restriction base="xs:decimal">
       <xs:fractionDigits value="2"/>
      </xs:restriction>
     </xs:simpleType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
  <xs:key name="BookItem_BookId">
   <xs:selector xpath="BookItem"/>
   <xs:field xpath="@BookId"/>
  </xs:key>
  <xs:keyref name="BookItem_Sort" refer="BookSort_SortId">
   <xs:selector xpath="BookSort"/>
   <xs:field xpath="@Sort"/>
  </xs:keyref>
  <xs:unique name="BookItem_Id">
   <xs:selector xpath="BookItem"/>
   <xs:field xpath="@BookId"/>
  </xs:unique>
</xs:element>
<xs:element name="BookSort">
  <xs:annotation>
   <xs:documentation>sort of a book</xs:documentation>
  </xs:annotation>
  <xs:complexType>
   <xs:sequence>
    <xs:element name="SortId" type="xs:positiveInteger"/>
    <xs:element name="Name" type="xs:string"/>
    <xs:element name="Code" type="xs:string"/>
    <xs:element name="Remark" type="xs:string"/>
   </xs:sequence>
  </xs:complexType>
  <xs:key name="BookSort_SortId">
   <xs:selector xpath="BookSort"/>
   <xs:field xpath="@SortId"/>
  </xs:key>
</xs:element>
<xs:element name="BookQuantity">
  <xs:annotation>
   <xs:documentation>Quantity of a  sort of book</xs:documentation>
  </xs:annotation>
  <xs:complexType>
   <xs:sequence>
    <xs:element name="PriKey" type="xs:positiveInteger"/>
    <xs:element name="BookId" type="xs:positiveInteger"/>
    <xs:element name="Quantity">
     <xs:simpleType>
      <xs:restriction base="xs:integer">
       <xs:minInclusive value="0"/>
      </xs:restriction>
     </xs:simpleType>
    </xs:element>
    <xs:element name="RecDate" type="xs:date"/>
   </xs:sequence>
  </xs:complexType>
  <xs:keyref name="BookQuantity_BookId" refer="BookItem_BookId">
   <xs:selector xpath="BookItem"/>
   <xs:field xpath="@BookId"/>
  </xs:keyref>
  <xs:key name="BookQuantity_PriKey">
   <xs:selector xpath="BookQuantity"/>
   <xs:field xpath="@PriKey"/>
  </xs:key>
</xs:element>
</xs:schema>


bookinfor.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by  () -->
<BookInfor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:noNamespaceSchemaLocation="C:\Documents and Settings\hy\桌面\spy\BookInfor.xsd">
<BookItem>
  <BookId>1</BookId>
  <Nane>Java与XML数据库整合应用</Nane>
  <Sort>1</Sort>
  <Isbn>7-900637-51-6</Isbn>
  <PubDate>2002-01</PubDate>
  <Author>沈兆阳</Author>
  <Price>49.00</Price>
</BookItem>
<BookItem>
  <BookId>2</BookId>
  <Nane>XML Schema数据库编程指南</Nane>
  <Sort>1</Sort>
  <Isbn>7-5053-7674-8</Isbn>
  <PubDate>2002-05</PubDate>
  <Author>Chelsea Valentine</Author>
  <Price>48.00</Price>
</BookItem>
<BookItem>
  <BookId>2</BookId>
  <Nane>离散数学</Nane>
  <Sort>2</Sort>
  <Isbn>7-80513-069-8/O.04</Isbn>
  <PubDate>2005-01</PubDate>
  <Author>左孝凌</Author>
  <Price>16.00</Price>
</BookItem>
<BookItem>
  <BookId>4</BookId>
  <Nane>At Middle Age</Nane>
  <Sort>3</Sort>
  <Isbn>0-8351-1609-3</Isbn>
  <PubDate>1987-12</PubDate>
  <Author>Shen Rong</Author>
  <Price>35.00</Price>
</BookItem>
<BookSort>
  <SortId>1</SortId>
  <Name>XML相关</Name>
  <Code>TN-05-829</Code>
  <Remark>计算机-语言-学习</Remark>
</BookSort>
<BookSort>
  <SortId>2</SortId>
  <Name>数学教材</Name>
  <Code>JY-01-021</Code>
  <Remark>数学教材</Remark>
</BookSort>
<BookSort>
  <SortId>2</SortId>
  <Name>英文小说</Name>
  <Code>AD-02-03</Code>
  <Remark>英文小说</Remark>
</BookSort>
<BookQuantity>
  <PriKey>1</PriKey>
  <BookId>1</BookId>
  <Quantity>3</Quantity>
  <RecDate>2005-10-28</RecDate>
</BookQuantity>
<BookQuantity>
  <PriKey>2</PriKey>
  <BookId>2</BookId>
  <Quantity>6</Quantity>
  <RecDate>2005-10-28</RecDate>
</BookQuantity>
<BookQuantity>
  <PriKey>3</PriKey>
  <BookId>3</BookId>
  <Quantity>29</Quantity>
  <RecDate>2005-10-28</RecDate>
</BookQuantity>
<BookQuantity>
  <PriKey>4</PriKey>
  <BookId>4</BookId>
  <Quantity>6</Quantity>
  <RecDate>2005-10-28</RecDate>
</BookQuantity>
</BookInfor>


--  作者:jinx
--  发布时间:11/11/2005 11:46:00 AM

--  
肯定不行,因为Key约束里面指定的是@BookID
<xs:unique name="BookItem_Id">
   <xs:selector xpath="BookItem"/>
   <xs:field xpath="@BookId"/>
</xs:unique>
而你的xml的结构却不是这样,数据是存放在BookId元素中而不是属性
<BookItem>
  <BookId>1</BookId>
  <Nane>Java与XML数据库整合应用</Nane>
  <Sort>1</Sort>
  <Isbn>7-900637-51-6</Isbn>
  <PubDate>2002-01</PubDate>
  <Author>沈兆阳</Author>
  <Price>49.00</Price>
</BookItem>
所以根本就没有对BookId的内容进行验证,那当然能够通过验证了!
所以大概应该改为:
<xs:unique name="BookItem_Id">
   <xs:selector xpath="BookItem"/>
   <xs:field xpath="BookId"/>
</xs:unique>

--  作者:cnEric
--  发布时间:11/27/2005 9:25:00 AM

--  
也不行啊?
--  作者:yiyanxiyin
--  发布时间:8/5/2008 11:14:00 AM

--  
应该将unique写在bookinfor下面
如果写在bookitem下面,表示某一个bookitem下面的bookid唯一,和其他bookitem无关,所以验证没有用
一定要注意范围
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms