2012-07-24 36 views
1

我有幾個xml文件,但我只出現一個xsd,並對其進行驗證。首先讓我描述這個問題。我編輯VS2010中的文件,並且在編輯xml文件時,VS運行時解析器顯示沒有錯誤。 DOM文檔:: schemaValidate():元素「元素」:後來,當我運行它加載XML文件並驗證它針對XML模式的PHP命令行腳本,它與excption說對xml架構的Xml驗證在php中失敗,但在.NET(VS2010)中起作用

警告沒有結束比賽爲keyref'elementRef'的鍵序列['回答']找到。

但是,我真的不能在xsd文件中看到一個錯誤。

XML文件:在XSD文件

<domain xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Answer" namespace="Custom\Surveys" xsi:noNamespaceSchemaLocation="../../xsd/domain.xsd"> 
    <entities> 
     <entity name="Answer" main="true" schema="answers"/> 
    </entities> 
    <bindings>  
    </bindings> 
    <key> 
     <element entity="answers" property="question_option_id"/> 
     <element entity="answers" property="survey_voter_id"/> 
    </key> 
</domain> 

XML模式:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="domain"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="entities"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="entity" minOccurs="1" maxOccurs="unbounded"> 
           <xs:complexType> 
            <xs:attribute name="name" type="xs:string" use="required" id="entityName" /> 
            <xs:attribute name="main" type="xs:boolean" use="optional" default="false" /> 
            <xs:attribute name="schema" type="xs:string" use="required" id="schemaName" />         
           </xs:complexType>        
          </xs:element> 
          <xs:element name="multientity" minOccurs="0" maxOccurs="1"> 
           <xs:complexType> 
            <xs:attribute name="name" type="xs:string" use="required" /> 
            <xs:attribute name="schema" type="xs:string" use="required" /> 
           </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType>     
       </xs:element> 
       <xs:element name="bindings" minOccurs="0" maxOccurs="1"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="bind" minOccurs="0" maxOccurs="unbounded"> 
           <xs:complexType> 
            <xs:sequence> 
             <xs:element name="referenced"> 
              <xs:complexType> 
               <xs:attribute name="entity" type="xs:string" use="required" /> 
               <xs:attribute name="property" type="xs:string" use="required" /> 
              </xs:complexType> 
              <xs:keyref name="referencedRef" refer="entityId"> 
               <xs:selector xpath="." /> 
               <xs:field xpath="@entity" /> 
              </xs:keyref> 
             </xs:element> 
            </xs:sequence> 
            <xs:attribute name="entity" type="xs:string" use="required" /> 
            <xs:attribute name="property" type="xs:string" use="required" /> 
           </xs:complexType> 
           <xs:keyref name="bindRef" refer="entityId"> 
            <xs:selector xpath="." /> 
            <xs:field xpath="@entity" /> 
           </xs:keyref> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType> 
       </xs:element> 
       <xs:element name="key"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="element" minOccurs="1" maxOccurs="unbounded"> 
           <xs:complexType> 
            <xs:attribute name="entity" type="xs:string" use="required" /> 
            <xs:attribute name="property" type="xs:string" use="required" /> 
           </xs:complexType> 
           <xs:keyref name="elementRef" refer="entityId"> 
            <xs:selector xpath="." /> 
            <xs:field xpath="@entity" /> 
           </xs:keyref> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType>     
       </xs:element> 
      </xs:sequence> 
      <xs:attribute name="name" type="xs:string" use="required" />    
      <xs:attribute name="namespace" type="xs:string" use="required" /> 
     </xs:complexType> 
     <xs:key name="entityId"> 
      <xs:selector xpath="./entities/entity|./entities/multientity" /> 
      <xs:field xpath="@schema" /> 
     </xs:key> 
    </xs:element> 
</xs:schema> 
+0

您是否添加了適當的標題到您的XML文件? '<?xml version =「1.0」encoding =「utf-8」?>' – 2012-07-24 14:10:08

+0

好的,但是我做到了。 – Santhos 2012-07-24 14:17:35

回答

0

我已發現一種解決方案,我不得不在端以限定所有在根元素的鍵和keyrefs 。

這裏去新的xsd文件:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="domain"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="entities"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="entity" minOccurs="1" maxOccurs="unbounded"> 
           <xs:complexType> 
            <xs:attribute name="name" type="xs:string" use="required" id="entityName" /> 
            <xs:attribute name="main" type="xs:boolean" use="optional" default="false" /> 
            <xs:attribute name="schema" type="xs:string" use="required" id="schemaName" />         
           </xs:complexType>        
          </xs:element> 
          <xs:element name="multientity" minOccurs="0" maxOccurs="1"> 
           <xs:complexType> 
            <xs:attribute name="name" type="xs:string" use="required" /> 
            <xs:attribute name="schema" type="xs:string" use="required" /> 
           </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType>     
       </xs:element> 
       <xs:element name="bindings" minOccurs="0" maxOccurs="1"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="bind" minOccurs="0" maxOccurs="unbounded"> 
           <xs:complexType> 
            <xs:sequence> 
             <xs:element name="referenced"> 
              <xs:complexType> 
               <xs:attribute name="entity" type="xs:string" use="required" /> 
               <xs:attribute name="property" type="xs:string" use="required" /> 
              </xs:complexType>           
             </xs:element> 
            </xs:sequence> 
            <xs:attribute name="entity" type="xs:string" use="required" /> 
            <xs:attribute name="property" type="xs:string" use="required" /> 
           </xs:complexType>        
          </xs:element> 
         </xs:sequence> 
        </xs:complexType> 
       </xs:element> 
       <xs:element name="key"> 
        <xs:complexType> 
         <xs:sequence> 
          <xs:element name="element" minOccurs="1" maxOccurs="unbounded"> 
           <xs:complexType> 
            <xs:attribute name="entity" type="xs:string" use="required" /> 
            <xs:attribute name="property" type="xs:string" use="required" /> 
           </xs:complexType> 
          </xs:element> 
         </xs:sequence> 
        </xs:complexType>     
       </xs:element> 
      </xs:sequence> 
      <xs:attribute name="name" type="xs:string" use="required" />    
      <xs:attribute name="namespace" type="xs:string" use="required" /> 
     </xs:complexType> 
     <xs:key name="entityId"> 
      <xs:selector xpath="./entities/entity|./entities/multientity" /> 
      <xs:field xpath="@schema" /> 
     </xs:key> 
     <xs:keyref name="elementRef" refer="entityId"> 
      <xs:selector xpath="./key/element" /> 
      <xs:field xpath="@entity" /> 
     </xs:keyref> 
     <xs:keyref name="bindRef" refer="entityId"> 
      <xs:selector xpath="./bindings/bind" /> 
      <xs:field xpath="@entity" /> 
     </xs:keyref> 
     <xs:keyref name="referencedRef" refer="entityId"> 
      <xs:selector xpath="./bindings/bind/referenced" /> 
      <xs:field xpath="@entity" /> 
     </xs:keyref> 
    </xs:element> 
</xs:schema> 
0

我建議總是把一個keyref,並在同一水平及其相應的鍵(在相同的元素聲明),如果你所能。這在理論上不是必需的,但是當你不這樣做時的規則非常複雜,有時甚至是違反直覺的。 (我不告訴你規則是什麼的原因是我知道閱讀這個區域的規範會傷害我的大腦,並且我不可能在沒有閱讀規範的情況下記住規則。)

+0

我知道我已經嘗試閱讀規則。我確實如你所說(即我自己的回答)。 – Santhos 2012-07-25 15:50:05