How to use Schema that is on local machine in XML document
Asked Answered
M

1

25

I have limited knowledge of XML/Schema files.

So this should be a fairly simple question. How do you specify a local file for the schemaLocation?

<?xml version="1.0"?>

<note
  xmlns="http://www.w3schools.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.w3schools.com note.xsd">
  ...
</note>

This is a sample from www.w3.org and the part that specifies schema is in schemaLocation. I tried looking at the documentation, but how can you specify a local file?

Something like

xsi:schemaLocation="../relativepath/schemafolder not.xsd">

Thanks

Marron answered 31/1, 2011 at 21:41 Comment(0)
C
29

schemaLocation has to contain two values separated by whitespace: the namespace URI (this doesn't change) and the schema url.

So in your case

 xsi:schemaLocation="http://www.w3schools.com ../relativepath/schemafolder/note.xsd">

Don't be fooled by the namespace URI being a seemingly valid http url, that's just one of the little madnesses XML people invented. :)

Actually, you can specify schemas for several namespaces in one schemaLocation:

xsi:schemaLocation="namespace1 schemaurl1 namespace2 schemaurl2 ..."

(I also advise you to use relative paths with care: while they are extremely useful when you move your files around but still validate it with the same code (or tool), when you deploy your validation code in an application, the "working directory" might not be what you expected. That is not to say you shouldn't use relative paths, just to be aware of this when you get a weird looking exception about the schema not found.)

Claar answered 31/1, 2011 at 21:45 Comment(5)
So the link does not have to be valid??? I still have to put something in there? Even if its not used at all?Marron
@Marron It's not a link at all. It just looks like a link, but in fact it's just a name. It is called a namespace URI (not a URL!) and it has nothing to do with linking. The second argument of schemaLocation is the url (or path) pointing to your schema file.Claar
@Claar - a namespace URI is not 'just a name', it must be a valid URI. However, you're right in that using syntactically valid HTTP URLs (and URLs are just a subset of URIs) tends to be horribly confusing for people. I've always preferred the OASIS/ODF approach of using a URN.Cathicathie
@Nic Gibson You're right but I wanted to make the explanation as un-cofusing, as possible.Claar
thanks for clarifying... I am aware of possible problems with relative paths I just gave it as an example. Do you know an good reading material related to namespaces and schema files. (I found a few but you are more familiar with topic so you might give me better)Marron

© 2022 - 2024 — McMap. All rights reserved.