tags:

views:

1652

answers:

4

Hi all..

Been wondering, just as we use the -decleration to bind XML to a DTD, how do we do it with XSD?

MSDN sample:

<?xml version="1.0"?>
<Product ProductID="123" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="Product.xsd">
    <ProductName>Rugby jersey</ProductName>
</Product>

is it the xsi:NoNamespaceSchemaLocation that does the trick? Or is this just another namespace? :P

Thanks in advance, and sorry if this is a stupid question..

[EDIT] And is the

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

..line just for giving us a unique XML namespace, or does it also provide information on where the schema can be located ?

+2  A: 

xsi:noNamespaceSchemaLocation and xsi:schemaLocation both provide hints to XML processors who decide to obey those hints. But they are only hints. They do not necessarily cause your document to validate against the schema.

John Saunders
+2  A: 

Better check W3C tutorials (and specifications) concerning xml how-to's.

You want schemaLocation.

From http://www.w3schools.com/schema/schema_howto. :

<?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">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

noNamespaceSchemaLocation is different. Note that both are indeed only "hints" in theory, to a consumer of an XML document. I've never run into an xml processor that did not follow them; after all, it's a W3C recommendation. see http://www.w3.org/TR/xmlschema-1/

But indeed, it could go wrong, as here, but then again, it's concidered a bug for a reason.

To go short : I just trust it, with no harm so far :-)

I don't think any half-decent xml processor can ignore this 'hint' these days.

The urls are always for uniqueness, but in some cases some info will be provided under the URL.

Peter
If you're running in an environment in which access to the Internet is blocked, schemas won't load. Besides, your syntax is wrong. It's namespace<space>location<space>namespace<space>location...
John Saunders
it's just a copy paste of the W3C
Peter
+2  A: 

I generally just include the namespace and expect that if whomever is processing it cares to validate it, then they will get the schema and set up their processing environment so that it can find the XSD. I've had limited success with xsi:schemaLocation and such attributes. Most of the problems are usually centered around finding the XSD file itself. Some processors want the path to be included which is loads of fun if the XSD is on a file system instead of a web server.

Every processor seems to implement the lookup a little differently. Some use separate schema catalog objects, others require you to load and attach schemas separately. Unless you are providing the code to process documents, you are best off to not include xsi:schemaLocation or xsi:noNamespaceSchemaLocation IMHO. The only thing that their inclusion can do is hamstring whomever is processing your document into either placing the schema in the same location or find some way to make their chosen processor ignore or workaround the location specification.

As a side note, the biggest problem that I've run into was actually with DTDs that were specified using a SYSTEM declaration that referred to "c:\somepath\doc.dtd". The problem was that I was processing the documents on a FreeBSD box. I ended up writing my own resolver to map Windows-style paths to a local file system since I could not modify the documents themselves and was required to validate them.

D.Shawley
+1  A: 

It is not a stupid question, but John Saunders has it right.

just as we use the -decleration to bind XML to a DTD, how do we do it with XSD?

Here is the essence of the problem - you can't. One of the problems with the DTD approach was that the document specified the validation mechanism and not the document consumer. Post-DTD, you can take an XML document and validate it with XSD or RELAX NG or some other mechanism - they are decoupled (in theory at least). Any XSD link is only a hint and is optional. It is not possible to validate an arbitrary document.

McDowell
@McDowell: Thank you very much.
John Saunders