2015-09-06 83 views
1

我需要通過xsd定義交換格式。我創建了一個示例xml文件,因爲我打算這樣做,並且想要測試它是否符合我創建的本地xsd,因此我嘗試通過IntelliJIDEA驗證它並獲得了以下錯誤。請指出我做錯了什麼。針對自制模式的XML驗證

<?xml version="1.0" encoding="UTF-8"?> 
<dictionaryResponse 
     xmlns="http://dictionaries.persistence.com" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://dictionaries.persistence.com dictionaries.xsd"> 
    <entry> 
     <dictionaryCode>2</dictionaryCode> 
     <id>1</id> 
     <code>C</code> 
    </entry> 
    <entry> 
     <dictionaryCode>2</dictionaryCode> 
     <id>2</id> 
     <code>V</code> 
    </entry> 
    <entry> 
     <dictionaryCode>2</dictionaryCode> 
     <id>3</id> 
     <code>R2</code> 
    </entry> 
    <entry> 
     <dictionaryCode>2</dictionaryCode> 
     <id>4</id> 
     <code>TM</code> 
    </entry> 
    <entry> 
     <dictionaryCode>2</dictionaryCode> 
     <id>5</id> 
     <code>SN</code> 
    </entry> 
    <entry> 
     <dictionaryCode>2</dictionaryCode> 
     <id>6</id> 
     <code>SA</code> 
    </entry> 
</dictionaryResponse> 

dictionaries.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<xs:schema version="1.0" targetNamespace="http://dictionaries.persistence.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:complexType name="dictionaryRequest"> 
     <xs:sequence> 
      <xs:element name="dictionaryCode" type="xs:string"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="dictionaryResponse"> 
     <xs:sequence> 
      <xs:element name="entry" type="entry" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="entry"> 
     <xs:sequence> 
      <xs:element name="dictionaryCode" type="xs:string"/> 
      <xs:element name="id" type="xs:string"/> 
      <xs:element name="code" type="xs:string"/> 
     </xs:sequence> 
    </xs:complexType> 

</xs:schema> 

錯誤:

Error:(12, 74) src-resolve.4.1: Error resolving component 'entry'. It was detected that 'entry' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///opt/idea-IC-141.1532.4/dictionaries.xsd'. If 'entry' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'entry' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:///opt/idea-IC-141.1532.4/dictionaries.xsd'.

Error:(5, 83) cvc-elt.1: Cannot find the declaration of element 'dictionaryResponse'.

條目中相同的XSD定義。我不明白他問題的根源是什麼

感謝您的幫助。

+0

回到您問題的第一個版本:請不要從您的帖子中刪除重要信息,非現場鏈接不夠穩定 - 您的帳戶似乎已經過期。 –

回答

2

您的XML文檔是以下模式的有效實例。我改變了以下內容:

  • 爲最外面的元素添加了xs:element聲明,dictionaryResponse。聲明這種名稱的類型是不夠的,您還必須在元素聲明中使用這種類型的
  • elementFormDefault="qualified"添加到您的模式中,因爲目標名稱空間應該應用於文檔實例中的所有元素。沒有它,你的模式需要在沒有命名空間的元素。
  • 製造http://dictionaries.persistence.com模式文檔更改類型的entry元素entryType
  • 的默認命名空間:不要爲元素名稱和類型的名稱使用相同的名稱,這可能會導致很多混亂。

XML模式

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<xs:schema version="1.0" targetNamespace="http://dictionaries.persistence.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" xmlns="http://dictionaries.persistence.com"> 

    <xs:element name="dictionaryResponse" type="dictionaryResponseType"/> 

    <xs:complexType name="dictionaryRequest"> 
     <xs:sequence> 
      <xs:element name="dictionaryCode" type="xs:string"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="dictionaryResponseType"> 
     <xs:sequence> 
      <xs:element name="entry" type="entryType" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="entryType"> 
     <xs:sequence> 
      <xs:element name="dictionaryCode" type="xs:string"/> 
      <xs:element name="id" type="xs:string"/> 
      <xs:element name="code" type="xs:string"/> 
     </xs:sequence> 
    </xs:complexType> 

</xs:schema> 

還有就是你的樣本文檔中沒有dictionaryRequest元素 - 你確定你需要它的類型定義?