62

I'm having this problem with referencing my XML Schema in an XML file.

I have my XSD in this path:

C:\environment\workspace\maven-ws\ProjectXmlSchema\email.xsd

But when in my XML file I'm trying to locate the schema like this, the XSD is not found:

<?xml version="1.0" encoding="UTF-8" ?>
    <email xmlns="http://www.w3schools.com"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.w3schools.com
                 file://C://environment//workspace//maven-ws//ProjextXmlSchema//email.xsd">

The only way the XSD is found is when it's in the same folder:

           xsi:schemaLocation="http://www.w3schools.com email.xsd"

So the question is this: How does the path have to look so that the XSD will be found if the XML file wasn't in the same folder as the XSD file?

By the way, the example I've been using was from MSDN: they're claiming it's supposed to work the way I tried to. But it doesn't.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Arturas M
  • 4,120
  • 18
  • 50
  • 80
  • For those of you wishing to reference a schema locally, you can easily launch the chrome web server plugin and point it to your schema's directory and then update your xml with the http address of the schema. – Raf Mar 07 '19 at 02:03
  • @Raf, the [accepted answer](https://stackoverflow.com/a/19254063/290085) already (over 5 years ago) suggested the idea of using a web browser to verify a local path to the XSD. – kjhughes Apr 22 '19 at 22:42
  • the answer does not mention accessing your `xsd` files through a local `web server`. Yes it does mention the idea of using `web browser` to verify the local path. In my case, I did not only need to verify but, also instruct `eclipse` not to lookup spring repos and instead go to my local web server (if I remember correctly, file option did not work for me). – Raf Apr 25 '19 at 16:02

3 Answers3

63

Add one more slash after file:// in the value of xsi:schemaLocation. (You have two; you need three. Think protocol://host/path where protocol is 'file' and host is empty here, yielding three slashes in a row.) You can also eliminate the double slashes along the path. I believe that the double slashes help with file systems that allow spaces in file and directory names, but you wisely avoided that complication in your path naming.

xsi:schemaLocation="http://www.w3schools.com file:///C:/environment/workspace/maven-ws/ProjextXmlSchema/email.xsd"

Still not working? I suggest that you carefully copy the full file specification for the XSD into the address bar of Chrome or Firefox:

file:///C:/environment/workspace/maven-ws/ProjextXmlSchema/email.xsd

If the XSD does not display in the browser, delete all but the last component of the path (email.xsd) and see if you can't display the parent directory. Continue in this manner, walking up the directory structure until you discover where the path diverges from the reality of your local filesystem.

If the XSD does displayed in the browser, state what XML processor you're using, and be prepared to hear that it's broken or that you must work around some limitation. I can tell you that the above fix will work with my Xerces-J-based validator.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Well sounds nice, but doesn't work as well... :/ Any more ideas? – Arturas M Oct 09 '13 at 15:18
  • 1
    Try checking the full file specification to the XSD via a browser. Answer above is updated with details. – kjhughes Oct 09 '13 at 15:52
  • 5
    Thank you very much. That was a superb idea trying to open it in a browser and look at/get the correct path. Thanks again, perfect! – Arturas M Oct 14 '13 at 04:07
  • This works only when we provide a complete path, how about if we only provide a file:///xsd/drools-spring.xsd – Vrajendra Singh Mandloi Jun 12 '18 at 08:05
  • 2
    Relative paths such as `xsd/drools-spring.xsd` are fine as long as the relative relationship between the directories is correctly captured. I suggest you post a new question and provide a [mcve] of your problem if you're having trouble. Thanks. – kjhughes Sep 06 '18 at 12:03
5

Maybe can help to check that the path to the xsd file has not 'strange' characters like 'é', or similar: I was having the same issue but when I changed to a path without the 'é' the error dissapeared.

gamoz
  • 131
  • 2
  • 4
  • 1
    Your remark helped me change a space in the filename to a (URL-encoded) `%20`. Maybe using URL-encoding might fix your problem? Or defining an XML-encoding like UTF-8, e.g. `` – Yahoo Serious May 18 '21 at 16:02
2

I think you can use this:

xsi:noNamespaceSchemaLocation="relativePATH/file.xsd"
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
Grumete86
  • 21
  • 1