2015-09-06 78 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元素 - 你确定你需要它的类型定义?