0

I am quite fresh with XML and even though I have managed quite well, I am now stuck with this tricky situation concerning XML Schema. Below you can see my XML document code and its respective Schema code. Firstly here is mine XML document code.

<?xml version="1.0" encoding="UTF-8"?>
<Cars
  xmlns="http://www.verkkokauppa.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema"
  xsi:schemaLocation="http://www.verkkokauppa.com 4Schema.xsd">

<Car>
    <Brand>BMW</Brand>
    <Model>535d</Model>
    <Gear>Automatic</Gear>
    <Year>2007</Year>
    <Info>It's a german.</Info>
</Car>
</Cars>

And below you can see my representative Schema code

<?xml version="1.0" encoding="UTF-8"?>
  <xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.verkkokauppa.com"
    xmlns="http://www.verkkokauppa.com" 
    elementFormDefault = "qualified">
  <xsd:element name = "Brand" type = "xsd:string"/>
  <xsd:element name = "Model" type = "xsd:string"/>
  <xsd:element name = "Gear" type = "xsd:string"/>
  <xsd:element name = "Year" type = "xsd:int"/>
  <xsd:element name = "Information" type = "xsd:string"/>
  <xsd:complexType name = "CarType">
      <xsd:sequence>
          <xsd:element ref = "Brand"/>
          <xsd:element ref = "Model"/>
          <xsd:element ref = "Gear"/>
          <xsd:element ref = "Year"/>
          <xsd:element ref = "Information" 
            minOccurs = "0" maxOccurs = "1" /> 
      </xsd:sequence>
  </xsd:complexType>
  <xsd:element name = "Cars">
      <xsd:complexType>
          <xsd:sequence>
              <xsd:element name = "Car" type = "CarType" 
               maxOccurs = "unbounded"/>
          </xsd:sequence>
      </xsd:complexType>
  </xsd:element>
</xsd:schema>    

Now, whenever I try to validate my XML document code I get an error: "cvc-elt.1: Cannot find the declaration of element 'Cars'. [6] "

This I think is to do with a namespace issue on the Cars element which is my root element, but I am really not sure.

If there is anyone who could guide me with this, I'd be most thankful.

3 Answers 3

1

Your guess is a good one, in the sense that failures to match namespaces are a frequent cause of error messages like this one. But your root element has the local name Cars in the namespace http://www.verkkokauppa.com, and your schema document for the namespace http://www.verkkokauppa.com declares an element whose local name is Cars. So I don't think it's a namespace issue. Perhaps I'm missing something subtle here.

If it's not a namespace matching issue, the most likely cause is that your schema validator is not finding the schema document.

The generic questions to ask in this case are: What validator are you using? How are you invoking it? How do you believe you are telling it where to find the schema document it should use?

If you are relying on the xsi:schemaLocation attribute on the root element, then have you checked that your schema document is present in the same directory as the input XML under the file name 4Schema.xsd? Have you checked that your validator will accept the schema-location hint? (It's a hint, not an instruction.) You should also check to make sure you've got the xsi:schemaLocation attribute correct.

In this specific case, you have an xsi:schemaLocation attribute, which is conventional, but you have bound the prefix xsi to the namespace http://www.w3.org/2001/XMLSchema, not to the namespace http://www.w3.org/2001/XMLSchema-instance. No XSD validator is going to do anything with an attribute named {http://www.w3.org/2001/XMLSchema}schemaLocation -- if they look for in-document schema information at all, they'll look for {http://www.w3.org/2001/XMLSchema-instance}schemaLocation.

Sign up to request clarification or add additional context in comments.

1 Comment

Good advice. The problem is solved now and it was fixed by adding that "-instance" into the name of my namespace. I still have lot to learn about this matter but I'm glad that I can take the next step now with my practices. Thank you a lot.
0

You need to understand basics of XML validation. Yes, your initial guess is right that's it is namaspace related issue. Other than that XML document structure was wrong. You have defined Information element into XSD however you have info element defined in XML. If you are beginner go through this tutorial to understand XML namespace. Hope below modified code help you.

Find below modified code

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://www.verkkokauppa.com" 
  xmlns="http://www.verkkokauppa.com" 
  elementFormDefault="qualified">
<xsd:element name = "Brand" type = "xsd:string"/>
<xsd:element name = "Model" type = "xsd:string"/>
<xsd:element name = "Gear" type = "xsd:string"/>
<xsd:element name = "Year" type = "xsd:int"/>
<xsd:element name = "Information" type = "xsd:string"/>
<xsd:complexType name = "CarType">
  <xsd:sequence>
      <xsd:element ref = "Brand"/>
      <xsd:element ref = "Model"/>
      <xsd:element ref = "Gear"/>
      <xsd:element ref = "Year"/>
      <xsd:element ref = "Information" minOccurs = "0" 
        maxOccurs = "1" /> 
  </xsd:sequence>
</xsd:complexType>
<xsd:element name = "Cars">
  <xsd:complexType>
      <xsd:sequence>
          <xsd:element name = "Car" type="CarType" 
            maxOccurs = "unbounded"/>
      </xsd:sequence>
  </xsd:complexType>
</xsd:element>
</xsd:schema>    

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Cars xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://www.verkkokauppa.com">
 <Car>
  <Brand>BMW</Brand>
  <Model>535d</Model>
  <Gear>Automatic</Gear>
  <Year>2007</Year>
  <Information>It's a german.</Information>
 </Car>
</Cars>

1 Comment

Why do you believe there is a namespace issue here? The root element of the XML is {http://www.verkkokauppa.com}Cars and the schema clearly declares a top-level element named {http://www.verkkokauppa.com}Cars. Where is the namespace issue?
-1

It depends on which XML schema validator you use. Maybe, you forgot to include the input parameter specifying the targetNamespace when you loaded the XSD file.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.