XML 针对自制模式进行验证

XML validation against self-made schema

我需要通过 xsd 定义交换格式。我按照我的预期创建了一个示例 xml 文件,并想测试它是否符合我创建的本地 xsd,所以我尝试通过 IntelliJIDEA 验证它并得到以下错误。请指出我做错了什么。

<?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 中定义。我不明白问题的根源是什么

感谢您的帮助。

您的 XML 文档是以下架构的有效实例。我更改了以下内容:

  • 为最外层元素 dictionaryResponse 添加了 xs:element 声明。仅声明该名称的类型是不够的,您还必须在元素声明中使用该类型。
  • 已将 elementFormDefault="qualified" 添加到您的架构中,因为目标名称空间应适用于文档实例中的所有元素。没有它,您的模式需要 no 命名空间中的元素。
  • 使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 元素 - 您确定需要它的类型定义吗?