1

First time posting here. I'm having difficulties with some XLM and XSD. I keep getting the following error:

Cannot find the declaration of element 'car'. [13]

I've searched the site for help, but I can't seem to make any progress without adding more errors (which may be the problem anyway I guess). I'd appreciate any and all help.

Part of XML:

<car
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="carcatalog.xsd">
    <make><h2>Make: </h2>Honda</make>
    <model><h2>Model: </h2>Accord</model>
    <year><h2>Year: </h2>2020</year>
    <color><h2>Color: </h2>Red</color>
    <engine>
        <number_of_cylinders><h2>Number of Cylinders: </h2>4</number_of_cylinders>
        <fuel_system><h2>Fuel System: </h2>Fuel Injected</fuel_system>
    </engine>
    <number_of_doors><h2>Doors: </h2>4</number_of_doors>
    <transmission_type><h2>Transmission: </h2>Automatic</transmission_type>
    <accessories>
        <radio><h2>Radio: </h2>Yes</radio>
        <air_conditioner><h2>Air Conditioner: </h2>Yes</air_conditioner>
        <power_windows><h2>Power Windows: </h2>No</power_windows>
        <power_steering><h2>Power Steering: </h2>Yes</power_steering>
        <power_brakes><h2>Power Brakes: </h2>Yes</power_brakes>
    </accessories>

    <h1></h1>

    <make><h2>Make: </h2>Chevrolet</make>
    <model><h2>Model: </h2>Corvette</model>
    <year><h2>Year: </h2>2020</year>
    <color><h2>Color: </h2>Blue</color>
    <engine>
        <number_of_cylinders><h2>Number of Cylinders: </h2>8</number_of_cylinders>
        <fuel_system><h2>Fuel System: </h2>Fuel Injected</fuel_system>
    </engine>
    <number_of_doors><h2>Doors: </h2>2</number_of_doors>
    <transmission_type><h2>Transmission: </h2>Manual</transmission_type>
    <accessories>
        <radio><h2>Radio: </h2>Yes</radio>
        <air_conditioner><h2>Air Conditioner: </h2>Yes</air_conditioner>
        <power_windows><h2>Power Windows: </h2>Yes</power_windows>
        <power_steering><h2>Power Steering: </h2>Yes</power_steering>
        <power_brakes><h2>Power Brakes: </h2>Yes</power_brakes>
    </accessories>

    <h1></h1>

    <make><h2>Make: </h2>Toyota</make>
    <model><h2>Model: </h2>Tacoma</model>
    <year><h2>Year: </h2>2017</year>
    <color><h2>Color: </h2>Cement</color>
    <engine>
        <number_of_cylinders><h2>Number of Cylinders: </h2>6</number_of_cylinders>
        <fuel_system><h2>Fuel System: </h2>Fuel Injected</fuel_system>
    </engine>
    <number_of_doors><h2>Doors: </h2>4</number_of_doors>
    <transmission_type><h2>Transmission: </h2>Automatic</transmission_type>
    <accessories>
        <radio><h2>Radio: </h2>Yes</radio>
        <air_conditioner><h2>Air Conditioner: </h2>Yes</air_conditioner>
        <power_windows><h2>Power Windows: </h2>Yes</power_windows>
        <power_steering><h2>Power Steering: </h2>Yes</power_steering>
        <power_brakes><h2>Power Brakes: </h2>Yes</power_brakes>
    </accessories>
</car>

Part of XSD:

<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
    <xs:complexType>
    <xs:sequence>

    <xs:element name = "car" maxOccurs="unbounded">
        <xs:complexType>
            <xs:sequence>
                <xs:element name = "make" type="xs:string"/>
                <xs:element name = "model" type="xs:string"/>
                <xs:element name = "year" type="xs:positiveInteger"/>
                <xs:element name = "color" type="xs:string"/>
                <xs:element name="engine">
                    <xs:complexType>
                    <xs:sequence>
                        <xs:element name="number_of_cylinders" type="xs:positiveInteger"/>
                        <xs:element name="fuel_system">
                            <xs:complexType>   
                            <xs:sequence>
                                <xs:choice>
                                    <xs:element name="fuel_injected"/>
                                    <xs:element name="carbureted"/>
                                </xs:choice>
                            </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                    </xs:complexType>
                </xs:element>

                <xs:element name = "number_of_doors" type="xs:positiveInteger"/>
                <xs:element name = "transmission_type" type="xs:string"/>

                <xs:element name = "accessories">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name = "radio" minOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="yes"/>
                                        <xs:enumeration value="no"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name = "air_conditioner" minOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="yes"/>
                                        <xs:enumeration value="no"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name = "power_windows" minOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="yes"/>
                                        <xs:enumeration value="no"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name = "power_steering" minOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="yes"/>
                                        <xs:enumeration value="no"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name = "power_brakes" minOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="yes"/>
                                        <xs:enumeration value="no"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
djchach
  • 11
  • 3
  • 2
    This is not a [mcve] and so, it may be hard to answer. The `car` element seems to be in some (incomplete) namespace, but it is only a child of `catalog`. – zx485 Feb 16 '20 at 00:33
  • Agreed. Until you provide a [mcve], the most specific answer you can get are those answers provided in the duplicate links. (Since they cover (nearly) everything in this area, they'll probably cover your underspecified case too.) – kjhughes Feb 16 '20 at 02:30
  • Sorry everyone! I posted the code to hopefully give a more complete answer. And yeah @kjhughes I do have a feeling my answer is out there, but I've just struggled from a beginners point and I've used a lot of overstack to help my issues so far - I just can't get over this one. – djchach Feb 16 '20 at 14:53
  • I've reopened the question and provided an answer below. – kjhughes Feb 16 '20 at 19:03
  • Please [**accept**](https://meta.stackexchange.com/q/5234/234215) an answer or follow-up with how you feel your question is unanswered. Thanks. – kjhughes Mar 11 '20 at 01:43

1 Answers1

1

Reason for the error

In your case, the reason for the error is because the root element in your XML, car, does not have a top-level declaration in your XSD.

Remedies for this issue

Either move the declaration of car to the top-level in your XSD, or wrap car in your XML with catalog, which is defined at the top level and has car in its content model.

If you opt to move the declaration of car to the top-level in the XSD, you can reference it where needed in the content model of catalog (and other elements as necessary) via

<xs:element ref="car"  ... >

rather than xs:element/@name.

Other reasons for the error

There are many other reasons for an element not to be found during validation. See also:

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    @djchach: You're welcome. Please [**accept**](https://meta.stackexchange.com/q/5234/234215) this answer if it's helped. Thanks. (You'll then have sufficient reputation to upvote too, if you feel so inclined. ;-) – kjhughes Feb 17 '20 at 20:40