Reputation: 3
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.
Upvotes: 0
Views: 10998
Reputation: 117
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.
Upvotes: -1
Reputation: 25054
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
.
Upvotes: 1
Reputation: 915
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>
Upvotes: 0