1
votes

When attempting to display the XML Schema in XML spy I get the following error. "Schema has to begin with a element in namespace 'http://www.w3.org/2001/XMLSchema'"

I tried it with a simple sample xml and get the same error

<?xml version="1.0"?>
<Book xmlns:lib="http://www.library.com">
  <lib:Title>Sherlock Holmes</lib:Title>
  <lib:Author>Arthur Conan Doyle</lib:Author>
</Book>

Any Ideas?

2

2 Answers

8
votes

The example you've included is an instance document; it is not a schema. A schema would begin with something like:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:lib="http://www.library.com"
   targetNamespace="http://www.library.com"
   elementFormDefault="qualified">

Your example document is not necessarily malformed, but is unusual in that it would require two schema to validate: one for the http://www.library.com namespace, and one in no namespace for the Book element.

I suggest reading the XMLSchema Primer.

-1
votes

Your xml validator is trying to validate a document. The document says that it conforms to a schema at some location. THe validator fetches that schema. But omg: the document that it fetches is not an xml schema at all! It's just some random bit of xml.

To fix this:

A xml namespace name is just a name. In this case, http://www.library.com. The schema validator application needs to know how to get the xml +schema document+ for that name. In the absense of anything else, it's probably just doing an http fetch and getting an html page.

You need to tell your validator that the namespace http://www.library.com is defined by a schema document at http://www.library.com/static/theschema.xsd (or whatever). Associating xml namespaces with schema documents is application-dependent. Most apps that work with XML have a cache of some sort that you have to configure and set up.

If you are working in java, then you need to implement an EntityResolver (I think) which, when the parser asks for http://www.library.com, returns an input stream containing the XML. You'd usually do this as a java resource file.