2013-10-07 67 views
0

我需要xml來匹配XSD架構,但它不匹配。 XSD模式中可能有什麼錯誤?有些東西與xsi:type =「...」不符。我可能會錯過什麼。希望你能注意到什麼是錯的。我一直在嘗試使用在線xsd驗證並修復它,但不起作用。所以我放棄了,決定寫在這裏。非常感謝您的幫助。謝謝!xml不匹配xsd模式 - 爲什麼?

這裏是XML:

<?xml version="1.0" encoding="utf-8" ?> 
<ValidatorList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > 
    <FieldValidator xsi:type="RequiredFieldValidator" PropertyName="BankTransactionNumber"> 
    <Next xsi:type="AsciiValidator" /> 
    </FieldValidator> 
    <FieldValidator xsi:type="RequiredFieldValidator" PropertyName="CurrencyIndicator"> 
    <Next xsi:type="StringLengthValidator" MaxLength="3" MinLength="3"> 
    <Next xsi:type="AlphaValidator" /> 
    </Next> 
</FieldValidator> 
</ValidatorList> 

XSD架構爲這個XML:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:element name="ValidatorList"> 
<xs:complexType> 
    <xs:sequence> 
    <xs:element name="FieldValidator" maxOccurs="unbounded" minOccurs="0"> 
     <xs:complexType mixed="true"> 
     <xs:sequence> 
      <xs:element name="Next" minOccurs="0"> 
      <xs:complexType mixed="true"> 
       <xs:sequence> 
       <xs:element name="Next" minOccurs="0"> 
        <xs:complexType mixed="true"> 
        <xs:sequence> 
         <xs:element name="Next" minOccurs="0"> 
         <xs:complexType> 
          <xs:simpleContent> 
          <xs:extension base="xs:string"> 
           <xs:attribute type="xs:byte" name="DecimalPlaces"/> 
          </xs:extension> 
          </xs:simpleContent> 
         </xs:complexType> 
         </xs:element> 
        </xs:sequence> 
        <xs:attribute type="xs:byte" name="MaxLength" use="optional"/> 
        <xs:attribute type="xs:byte" name="MinLength" use="optional"/> 
        <xs:attribute type="xs:string" name="AllowedValues" use="optional"/> 
        </xs:complexType> 
       </xs:element> 
       </xs:sequence> 
       <xs:attribute type="xs:byte" name="MaxLength" use="optional"/> 
       <xs:attribute type="xs:byte" name="MinLength" use="optional"/> 
       <xs:attribute type="xs:byte" name="DefaultValue" use="optional"/> 
      </xs:complexType> 
      </xs:element> 
      <xs:element type="xs:string" name="RegEx" minOccurs="0"/> 
     </xs:sequence> 
     <xs:attribute type="xs:string" name="PropertyName" use="optional"/> 
     <xs:attribute type="xs:byte" name="DefaultValue" use="optional"/> 
     <xs:attribute type="xs:string" name="TypeProperty" use="optional"/> 
     <xs:attribute type="xs:string" name="Regex" use="optional"/> 
     </xs:complexType> 
    </xs:element> 
    </xs:sequence> 
</xs:complexType> 

我得到這樣的錯誤消息:

Error - Line 4, 39: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 39; cvc-elt.4.2: Cannot resolve 'AsciiValidator' to a type definition for element 'Next'. 
Error - Line 6, 87: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 87; cvc-elt.4.2: Cannot resolve 'RequiredFieldValidator' to a type definition for element 'FieldValidator'. 
Error - Line 7, 72: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 72; cvc-elt.4.2: Cannot resolve 'StringLengthValidator' to a type definition for element 'Next'. 
Error - Line 8, 41: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 41; cvc-elt.4.2: Cannot resolve 'AlphaValidator' to a type definition for element 'Next'. 
+0

那麼,您定義的「AsciiValidator」類型在哪裏?我無法在模式中看到。也許嘗試將所有'xsi:type'更改爲'FieldValidator'(您唯一定義的唯一一個)。 –

+0

它是字符串類型。即時通訊不知道如何糾正xsd架構... – user235973457

+0

'AsciiValidator'是一個字符串類型? –

回答

2

xsi:type屬性的目的是當你有命名爲類型的架構層次結構和元素聲明類型爲基本類型,但其實際類型派生類型中的一種使用。在典型的例子是一個address型亞型有usAddressukAddress

在你的架構FieldValidatorNext元素使用匿名嵌套<complexType>定義,而不是命名的類型聲明,所以沒有辦法,你可以從這些派生的其他類型,所以在使用xsi:type時沒有意義。我將構建架構不同,採用頂級命名類型:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:complexType name="FieldValidatorType"> 
    <xs:sequence> 
     <xs:element name="Next" type="FieldValidatorType" minOccurs="0" /> 
    </xs:sequence> 
    <xs:attribute name="PropertyName" type="xs:string" use="optional" /> 
    </xs:complexType> 

    <xs:complexType name="RequiredFieldValidator"> 
    <xs:complexContent> 
     <xs:extension base="FieldValidatorType" /> 
    </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="AsciiValidator"> 
    <xs:complexContent> 
     <xs:extension base="FieldValidatorType" /> 
    </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="StringLengthValidator"> 
    <xs:complexContent> 
     <xs:extension base="FieldValidatorType"> 
     <xs:attribute type="xs:byte" name="MaxLength" use="optional"/> 
     <xs:attribute type="xs:byte" name="MinLength" use="optional"/> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 

    <!-- and similar complexType declarations for the other types such as 
     AlphaValidator --> 

    <xs:element name="ValidatorList"> 
    <xs:complexType> 
     <xs:sequence> 
     <!-- define the FieldValidator element by referring to the base 
      FieldValidatorType, instance documents can use xsi:type to 
      substitute subtypes such as StringLengthValidator --> 
     <xs:element name="FieldValidator" maxOccurs="unbounded" minOccurs="0" 
        type="FieldValidatorType" /> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

除了使xsi:type屬性正常工作,這已經追平了不同方面各自的驗證類型(的優勢,如MaxLength只對於StringLengthValidator有效)並且還支持Next嵌套的任意深度(因爲類型定義是遞歸的)。

+0

它幾乎工作,但有一個錯誤消息,我得到了:錯誤 - 第29,103行:org.xml.sax.SAXParseException; lineNumber:29; columnNumber:103; src-element.3:元素'FieldValidator'同時具有'type'屬性和'匿名類型'子元素,只有其中一個元素是允許的。 – user235973457

+0

@ user235973457錯誤消息說明了所有 - 'ValidateList'中的'type =「FieldValidatorType」'不是_嵌套'',而不是__。 –

+0

@ user235973457我試圖通過給出一個完整的''來使事情更加清晰。 –