Reputation: 43
I'm trying to validate an XML
file against XSD Schema
that I've written before. The java code to validate my xml file is shown below. When I try to validate the XML, I always get an error like : "Cannot find declaration of the root element".
Could you help me to solve this problem?
XML file
<?xml version="1.0" encoding="UTF-8"?>
<AllBooks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://myNameSpace.com"
schemaLocation="http://myNameSpace.com book.xsd">
<book>
<id>1</id>
<title>aşk ve gurur</title>
<author>james brown</author>
<category>science</category>
<availablity>100</availablity>
<price>5000</price>
</book>
<book>
<id>2</id>
<title>kskkdn</title>
<author>mşlfke</author>
<category>love</category>
<availablity>50</availablity>
<price>5000</price>
</book>
</AllBooks>
Schema file
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com" elementFormDefault="qualified">
<xs:element name="AllBooks">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="category" type="xs:string"/>
<xs:element name="availability" type="xs:integer"/>
<xs:element name="price" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And java code
static boolean validateAgainstXSD(InputStream xml, InputStream xsd)
{
try
{
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(xsd));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xml));
return true;
}
catch(Exception ex)
{
return false;
}
}
Upvotes: 2
Views: 269
Reputation: 111621
Your namespaces do not match up between the XSD and XML files. Also, availability
is misspelled as availablity
in the XML file. Corrections follow...
Use this XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://myNameSpace.com"
elementFormDefault="qualified">
<xs:element name="AllBooks">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="category" type="xs:string"/>
<xs:element name="availability" type="xs:integer"/>
<xs:element name="price" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Then this fixed XML instance document will be valid:
<?xml version="1.0" encoding="UTF-8"?>
<AllBooks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://myNameSpace.com"
xsi:schemaLocation="http://myNameSpace.com book.xsd">
<book>
<id>1</id>
<title>aşk ve gurur</title>
<author>james brown</author>
<category>science</category>
<availability>100</availability>
<price>5000</price>
</book>
<book>
<id>2</id>
<title>kskkdn</title>
<author>mşlfke</author>
<category>love</category>
<availability>50</availability>
<price>5000</price>
</book>
</AllBooks>
Upvotes: 1