2013-07-18 164 views
1

QXmlSchemaValidator在包含substitutionGroups時未驗證我的XML。在線工具(http://www.utilities-online.info/xsdvalidation/,http://www.freeformatter.com/xml-validator-xsd.html)根據XSD驗證XML,XSD和XML。QXmlSchemaValidator(Qt 4.8)是否支持XML模式(XSD)屬性「substitutionGroup」

錯誤:

Error XSDError in file: (XML), at line 44, column 14: Element image_id is not defined in this scope. 

相關代碼,XSD:

<xs:element name="image_ids" abstract="true"/> 
    <xs:element name="image_id" type="xs:nonNegativeInteger" substitutionGroup="image_ids"/> 
    <xs:element name="image_queue_id" type="xs:nonNegativeInteger" substitutionGroup="image_ids"/> 

    <xs:element name="image_slot"> 
    <xs:complexType>  
     <xs:all> 
     <xs:element ref="image_ids" maxOccurs="1"/> 
     <xs:element ref="caption" minOccurs="0" maxOccurs="1"/> 
     </xs:all> 
     <xs:attributeGroup ref="position"/> 
    </xs:complexType> 
    </xs:element> 

相關代碼,XML:

<image_slot x="7" y="110" width="55"> 
    <image_id>0</image_id> <-- error 
    <caption>some caption</caption> 
    </image_slot> 

QXmlSchemaValidator驗證XSD,但不是針對XSD的XML。擺脫substitutionGroup足以驗證XML,但這也意味着我失去了功能 - 現在將驗證不正確的XML文件。因此,我的問題是 - Qt實際上是否支持XML substitutionGroups,並且還有其他的我做錯了(其他工具沒有注意到)?

或者它是QXmlSchemaValidator中的一個錯誤,在這種情況下,我應該放棄這個想法並找到另一個解決方案?

編輯:不得不等待一天才能發佈我自己的答案。我必須再等一天才能接受它。

回答

0

事實證明,替代組的元素必須是相同的類型。在我的XSD中就是這種情況,但類型是在組的每個元素中定義的,而不是在抽象元素級別。下面的代碼解決了問題:

XDS代碼:

<xs:element name="image_ids" type="xs:nonNegativeInteger" abstract="true"/> 
    <xs:element name="image_id" substitutionGroup="image_ids"/> 
    <xs:element name="image_queue_id" substitutionGroup="image_ids"/> 

    <xs:element name="image_slot"> 
    <xs:complexType>  
     <xs:all> 
     <xs:element ref="image_ids" maxOccurs="1"/> 
      <xs:element ref="caption" minOccurs="0" maxOccurs="1"/> 
     </xs:all> 
     <xs:attributeGroup ref="position"/> 
    </xs:complexType> 
    </xs:element> 

XML代碼:

<image_slot x="7" y="110" width="55"> 
    <image_id>0</image_id> 
    <caption>some caption</caption> 
    </image_slot> 

我不確定爲什麼QXmlSchemaValidator驗證架構只拒絕事後驗證XML;希望這會幫助其他人面臨同樣的問題。