2013-04-11 95 views
1

我的xs:unique不起作用。我嘗試了這個網站提供的各種解決方案。他們似乎都沒有爲我工作。我正在使用C#根據模式驗證我的xml。例如重複鍵密碼。問題與xml xs:獨特

下面是我的模式。

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mstns="http://tempuri.org/SqlManifest.xsd"> 
    <xs:element name="sqlmanifest"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="sqlparameters" maxOccurs="1" minOccurs="0"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="sqlparameter" maxOccurs="unbounded" minOccurs="0"> 
       <xs:complexType> 
        <xs:simpleContent> 
        <xs:extension base="xs:string"> 
         <xs:attribute type="xs:string" name="key" use="required"/> 
         <xs:attribute type="xs:string" name="description" use="required"/> 
        </xs:extension> 
        </xs:simpleContent> 
       </xs:complexType> 
       <xs:unique name="unique-key"> 
        <xs:selector xpath ="mstns:sqlparameter"/> 
        <xs:field xpath="@key"/> 
       </xs:unique> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="sqlcommands" maxOccurs="1" minOccurs="1"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="sqlcommand" maxOccurs="unbounded" minOccurs="1"> 
       <xs:complexType> 
        <xs:simpleContent> 
        <xs:extension base="xs:string"> 
         <xs:attribute type="xs:string" name="path" use="required"/> 
        </xs:extension> 
        </xs:simpleContent> 
       </xs:complexType> 
       <xs:unique name="unique-path"> 
        <xs:selector xpath ="mstns:sqlcommand"/> 
        <xs:field xpath="@path"/> 
       </xs:unique> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
     <xs:attribute type="xs:boolean" name="transactional"/> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

下面是我的XML

<sqlmanifest transactional="false"> 
    <sqlparameters> 
     <sqlparameter key="dbpath" description="Please enter dbpath"/> 
     <sqlparameter key="passworduserOmni" description="Please enter password for OmniUser"/> 
     <sqlparameter key="passwordadminOmni" description="Please enter password for OmniAdminUser"/> 
       <sqlparameter key="passwordadminOmni" description="Please enter password for OmniAdminUser"/> 
    </sqlparameters> 
    <sqlcommands> 
     <sqlcommand path="scripts/script1.sql"/> 
     <sqlcommand path="scripts/script2.sql" /> 
     <sqlcommand path="scripts/scripts3.sql" /> 
     <sqlcommand path="scripts/scripts4.sql" /> 
       <sqlcommand path="scripts/script1.sql"/> 
    </sqlcommands> 
</sqlmanifest> 

回答

2

您的XML不使用命名空間,所以你的XSD應該沒有目標命名空間,所以你不能在你的XPath(字段/選擇器)使用任何前綴。

限制必須放在適當的水平。所以你的unique-key必須在sqlparametersunique-pathsqlcommands下移動。

你糾正XSD如下:

<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mstns="http://tempuri.org/SqlManifest.xsd"> 
    <xs:element name="sqlmanifest"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="sqlparameters" maxOccurs="1" minOccurs="0"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="sqlparameter" maxOccurs="unbounded" minOccurs="0"> 
           <xs:complexType> 
            <xs:simpleContent> 
             <xs:extension base="xs:string"> 
              <xs:attribute type="xs:string" name="key" use="required"/> 
              <xs:attribute type="xs:string" name="description" use="required"/> 
             </xs:extension> 
            </xs:simpleContent> 
           </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType> 
        <xs:unique name="unique-key"> 
         <xs:selector xpath ="sqlparameter"/> 
         <xs:field xpath="@key"/> 
        </xs:unique> 
       </xs:element> 
       <xs:element name="sqlcommands" maxOccurs="1" minOccurs="1"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="sqlcommand" maxOccurs="unbounded" minOccurs="1"> 
           <xs:complexType> 
            <xs:simpleContent> 
             <xs:extension base="xs:string"> 
              <xs:attribute type="xs:string" name="path" use="required"/> 
             </xs:extension> 
            </xs:simpleContent> 
           </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType> 
        <xs:unique name="unique-path"> 
         <xs:selector xpath ="sqlcommand"/> 
         <xs:field xpath="@path"/> 
        </xs:unique> 
       </xs:element> 
      </xs:sequence> 
      <xs:attribute type="xs:boolean" name="transactional"/> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

如果你之前和之後可視化的約束,它會幫助你理解爲什麼範圍應該是這個樣子;另外,考慮到選擇器的xpath根源於元素,它不會按照您的期望匹配。

enter image description here