2014-03-12 77 views
1

我有一個根插入標記,一系列插入標記,每個標記都具有「名稱」屬性。xsd唯一約束不起作用

我無法獲得在線驗證器以發現存在重複的「名稱」值。

我們一直在努力......天。感謝您的發現。

XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.osames.org/osamesorm" 
    targetNamespace="http://www.osames.org/osamesorm" elementFormDefault="qualified"> 

    <xs:element name="Inserts"> 
     <xs:complexType> 
      <xs:sequence> 
      <xs:element name="Insert" maxOccurs="unbounded"> 
       <xs:complexType> 
       <xs:simpleContent> 
        <xs:extension base="xs:string"> 
        <xs:attribute name="name" type="xs:string" use="required"/> 
        </xs:extension> 
       </xs:simpleContent> 
       </xs:complexType> 
      </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
     <xs:unique name="unique-isbn"> 
      <xs:selector xpath="Inserts/Insert"/> 
      <xs:field xpath="@name"/> 
     </xs:unique> 
     </xs:element> 

</xs:schema> 

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<Inserts xmlns="http://www.osames.org/osamesorm" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.osames.org/osamesorm ./xml_schemas/verysimple.xsd"> 
<Insert name="BaseInsert">INSERT INTO {0} ({1}) values ({2});</Insert> 
<Insert name="BaseInsert">INSERT INTO {0} ({1}) values ({2});</Insert> 
</Inserts> 

回答

5

有兩個問題在您的模式:

第一個是你的選擇XPath是不正確的基礎上,位置在那裏你定義它。該<xs:unique>元素是<Inserts>元素,但你的XPath讀Inserts/Insert,這意味着該<Inserts>元素中,另一個<Inserts>元的預期,並且只在其中的<Insert>元素。

<xs:unique>約束,但是,已經內<Inserts>元素,這就是爲什麼你只需要找到<Insert>元素:

<xs:unique name="unique-isbn"> 
    <xs:selector xpath="Insert"/> 
    <xs:field xpath="@name"/> 
</xs:unique> 

的第二個問題,即XPath沒有定義的默認命名空間的概念在Xml中具有xmlns屬性。您在XPath中引用的Insert元素是而不是Insert元素來自XSD的默認命名空間,但是Insert元素沒有命名空間URI。

爲了解決這個問題,添加命名空間前綴命名空間(以替代默認的命名空間),以您的XSD文件:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.osames.org/osamesorm" targetNamespace="http://www.osames.org/osamesorm" xmlns:o="http://www.osames.org/osamesorm" elementFormDefault="qualified"> 

然後,使用該命名空間前綴在你的XPath:

<xs:unique name="unique-isbn"> 
    <xs:selector xpath="o:Insert"/> 
    <xs:field xpath="@name"/> 
</xs:unique> 

隨着這些改變,this validator輸出

有一個重複的鍵序列「BaseI nsert'爲'http://www.osames.org/osamesorm:unique-isbn'鍵或唯一標識約束。行:1列:357

+0

我有類似的問題,關鍵的解決辦法是在選擇使用命名空間前綴的! – recineshto