2013-10-23 72 views
0

我有以下XSD已經有一個獨特的項目,但我試圖添加另一個沒有成功。有xsd問題:唯一

我想在整個文檔結構中使WikiArticle - Filename唯一,但儘管存在幾種不同的變體,我無法使其失敗。

任何想法?

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="AllMeta"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element maxOccurs="unbounded" name="WikiMeta"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="Subject" type="xs:string" minOccurs="1" maxOccurs="1" /> 
       <xs:element name="PageTitle" type="xs:string" minOccurs="1" maxOccurs="1" /> 
       <xs:element name="PageMetaDescription" type="xs:string" minOccurs="1" maxOccurs="1" /> 
       <xs:element name="Articles"> 
       <xs:complexType> 
        <xs:sequence> 
        <xs:element name="WikiArticle" minOccurs="1"> 
         <xs:complexType> 
         <xs:sequence> 
          <xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1" /> 
         </xs:sequence> 
         </xs:complexType> 
        </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
       </xs:element> 
       <xs:element name="Groups"> 
       <xs:complexType> 
        <xs:sequence> 
        <xs:element maxOccurs="unbounded" name="WikiGroup"> 
         <xs:complexType> 
         <xs:sequence> 
          <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1" /> 
         </xs:sequence> 
         </xs:complexType> 
        </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:unique name="uniqueSubject"> 
     <xs:selector xpath="WikiMeta"/> 
     <xs:field xpath="Subject"/> 
    </xs:unique> 
    </xs:element> 
</xs:schema> 

回答

0

鑑於我見過的其他模式,我冒昧地修改的WikiArticle到無限的基數(它不會在你的情況下,重要)。

修改後的XSD如下。

<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="AllMeta"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element maxOccurs="unbounded" name="WikiMeta"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="Subject" type="xs:string" minOccurs="1" maxOccurs="1"/> 
          <xs:element name="PageTitle" type="xs:string" minOccurs="1" maxOccurs="1"/> 
          <xs:element name="PageMetaDescription" type="xs:string" minOccurs="1" maxOccurs="1"/> 
          <xs:element name="Articles"> 
           <xs:complexType> 
            <xs:sequence> 
             <xs:element name="WikiArticle" minOccurs="1" maxOccurs="unbounded"> 
              <xs:complexType> 
               <xs:sequence> 
                <xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1"/> 
               </xs:sequence> 
              </xs:complexType> 
             </xs:element> 
            </xs:sequence> 
           </xs:complexType> 
          </xs:element> 
          <xs:element name="Groups"> 
           <xs:complexType> 
            <xs:sequence> 
             <xs:element maxOccurs="unbounded" name="WikiGroup"> 
              <xs:complexType> 
               <xs:sequence> 
                <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/> 
               </xs:sequence> 
              </xs:complexType> 
             </xs:element> 
            </xs:sequence> 
           </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
     <xs:unique name="uniqueSubject"> 
      <xs:selector xpath="WikiMeta"/> 
      <xs:field xpath="Subject"/> 
     </xs:unique> 
     <xs:unique name="fileName"> 
      <xs:selector xpath="WikiMeta/Articles/WikiArticle"/> 
      <xs:field xpath="FileName"/> 
     </xs:unique> 
    </xs:element> 
</xs:schema> 

是什麼樣子(你現在可能要添加其他約束,如唯一性WikiGroup名)

enter image description here

有效樣本XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<AllMeta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <WikiMeta> 
     <Subject>Subject1</Subject> 
     <PageTitle>PageTitle1</PageTitle> 
     <PageMetaDescription>PageMetaDescription1</PageMetaDescription> 
     <Articles> 
      <WikiArticle> 
       <FileName>FileName1</FileName> 
      </WikiArticle> 
      <WikiArticle> 
       <FileName>FileName2</FileName> 
      </WikiArticle> 
     </Articles> 
     <Groups> 
      <WikiGroup> 
       <Name>Name1</Name> 
      </WikiGroup> 
      <WikiGroup> 
       <Name>Name1</Name> 
      </WikiGroup> 
     </Groups> 
    </WikiMeta> 
    <WikiMeta> 
     <Subject>Subject2</Subject> 
     <PageTitle>PageTitle1</PageTitle> 
     <PageMetaDescription>PageMetaDescription1</PageMetaDescription> 
     <Articles> 
      <WikiArticle> 
       <FileName>FileName3</FileName> 
      </WikiArticle> 
      <WikiArticle> 
       <FileName>FileName4</FileName> 
      </WikiArticle> 
     </Articles> 
     <Groups> 
      <WikiGroup> 
       <Name>Name1</Name> 
      </WikiGroup> 
      <WikiGroup> 
       <Name>Name1</Name> 
      </WikiGroup> 
     </Groups> 
    </WikiMeta> 
</AllMeta> 

現在,將FileName4更改爲FileName1,您將得到如下錯誤:

Error occurred while loading [], line 35 position 6 
There is a duplicate key sequence 'FileName1' for the 'fileName' key or unique identity constraint. 
+0

感謝Petru的建議 - 這是完美的。是的,我將爲WikiGroup添加一個約束,但因爲它只需要受到約束,所以該組只能在每個WikiMeta中存在一次,但在文檔中多次存在。與其他例子有所不同,它需要更多的研究/問題提問。你能給我一個關於這可能如何工作的想法嗎? – dotnetnoob

+0

@dotnetnoob,約束僅限於包含元素。對於你的羣組問題,只需在WikiMeta元素下創建你的約束,而不是AllMeta。 –