yaromir
yaromir

Reputation: 385

XML validation against self-made schema

I need to define an interchange format via xsd. I created an example xml file as i intended it to be and want to test that it conforms to the local xsd i created so i tried to validate it via IntelliJIDEA and got following errors. Please point me to what i did wrong.

<?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>

Errors:

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'.

Entry is defined in the same xsd. I can't understand what's the source of he problem

Thanks for help.

Upvotes: 1

Views: 3054

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22637

Your XML document is a valid instance of the schema below. I have changed the following:

  • added an xs:element declaration for the outermost element, dictionaryResponse. It is not enough to declare a type of that name, you also have to use this type in an element declaration.
  • Added elementFormDefault="qualified" to your schema, because the target namespace should apply to all elements in your document instances. Without it, your schema expects elements that are in no namespace.
  • Made http://dictionaries.persistence.com the default namespace of the schema document
  • Changed type of entry element to entryType: Do not use the same name for the element name and the name of the type, this can cause a lot of confusion.

XML Schema

<?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>

There is no dictionaryRequest element in your sample document - are you sure you need a type definition for it?

Upvotes: 2

Related Questions