XML Schema - Beginner question

This should cover W3C XML Schema, Relax NG and DTD related problems.
tone
Posts: 14
Joined: Sun Apr 29, 2007 12:41 am
Location: Tennessee

XML Schema - Beginner question

Post by tone »

So I just started looking at Schemas and I'm a liitle confused as to how to validate an xml instance file using a schema in Oxygen. I have my xml file:

<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="C:\Users\tsoutherland.BAI\Desktop\XML Schema note.xsd">
<to>Tone</to>
<from>Andrea</from>
<heading>Reminder</heading>
<body>Hello!!</body>
</note>

And my schema is setup with contraints that this xml file constrain to. When I click the Validate button in oxygen I get a Failed to Read Schema Document - could not find the document. Am I referencing the schema correctly above? It's on my local machine and I just want to validate it for testing purposes. The schema file lives in the same directory as my xml file.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

The xsi:schemaLocation attribute should contain pairs of the form

Code: Select all


namespace schemaURI
In your case the namespace is http://www.w3schools.com and you can use a relative reference to the shema file note.xsd:

Code: Select all


xsi:schemaLocation="http://www.w3schools.com note.xsd"
oXygen provides an associate schema action that you can use to atomatically have the xsi:schemaLocation added with the proper value for namespace and schema file.

Best regards,
George
George Cristian Bina
tone
Posts: 14
Joined: Sun Apr 29, 2007 12:41 am
Location: Tennessee

Post by tone »

ok great that did it! Not sure how I missed that the first go round.

I understand that the URI refers to the .xsd schema file, but the namespace? What exactly is a namespace? I've read some technical specs, but I still don't understand what role the http://www.w3schools.com plays in my xml instance in the line.

xsi:schemaLocation="http://www.w3schools.com note.xsd"
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

The namespace is just an identifier that allows you to have elements with the same name but different semantics, for instance you can have XHTML table different from George's table. You declared a default namespace for your element with the xmlns="http://www.w3schools.com" and XML Schema requires the namespace and the schema file to associate the schema with the document.

Best Regards,
George
George Cristian Bina
tone
Posts: 14
Joined: Sun Apr 29, 2007 12:41 am
Location: Tennessee

Post by tone »

Is there any particular purpose to the url that is used? Would it matter if I changed it from, http://www.w3schools.com to something like http://www.foo.com while leaving the note.xsd the same?
tone
Posts: 14
Joined: Sun Apr 29, 2007 12:41 am
Location: Tennessee

Post by tone »

Is it basically a label for that particular schema?
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Yes, it matters, because the schema also has a targetNamespace that specifies the namespace for the elements it defines.

Ok, some more explanations.
In XML you have elements and attributes. Both have a name, initially that name was everything about them. But as the language evolved and people defined elements there were cases when more than one used the same name. Then at some point people wanted to have documents that contained both elements, but they had the same name while they still wanted to know which is what. So they needed something to tell which is what and namespaces were invented for that purpose. Now, in XML plus namespaces a name (element or attribute name) is composed from a local name and a namespace, in Clark notation that is represented

{namespace}localName

To have these names represented in a more compact form in XML they invented prefixes that are bound to namespaces through a namespace declaration
xmlns:prefix="namepsace"
and then we can write
prefix:localName
to specify the element or attribute {namespace}localName.

There is also a default namespace declaration
xmlns="namespace"
that applies only to elements and allow to write elements without a prefix while still being in the default namespace.

More, namespace declarations are inherited by children from their parents.

In your sample file you have a default namespace declaration that is inherited by all the elements, so note, to, from, heading and body are only the local name for your document elements, they also have the http://www.w3schools.com namespace.

You can write your document also with a prefix if you declare the http://www.w3schools.com namespace mapped to a prefix

<?xml version="1.0"?>
<p:note
xmlns:p="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="C:\Users\tsoutherland.BAI\Desktop\XML Schema note.xsd">
<p:to>Tone</p:to>
<p:from>Andrea</p:from>
<p:heading>Reminder</p:heading>
<p:body>Hello!!</p:body>
</p:note>

Hope things are more clear now.

George
George Cristian Bina
tone
Posts: 14
Joined: Sun Apr 29, 2007 12:41 am
Location: Tennessee

Post by tone »

ok. yes it is making much more sense now. And I really appreciate you taking the time to explain all this. This is how I'm understanding thus far:

The default namespace attribute in the xml file needs to match the default namespace attribute in the schema file, which is this line: xmlns="http://www.w3schools.com"

The targetNamespace attribute, only used in the schema file, overrides the default namespace attribute targetNamespace="http://www.w3schools.com", and probably does some other things, which I don't understand yet.

And this line xmlns:xs="http://www.w3.org/2001/XMLSchema" sets a prefix of "xs" to allow this namespace to be referenced by "xs" instead of the full namespace. This is helpful when referring to multiple namespaces within the same xml file.

And the schemaLocation attribute - xsi:schemaLocation="http://www.w3schools.com note.xsd" - tells the namespace ("http://www.w3schools.com") that the notes.xsd schema file will describe/define it.

Is that generally a correct interpretation of how this works?
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

tone wrote: [...]
The default namespace attribute in the xml file needs to match the default namespace attribute in the schema file, which is this line: xmlns="http://www.w3schools.com"
No. It does not matter how you write an element as long as you specify the same local name and namespace. See the example in my previous post when I wrote your sample document with a p prefix. It is the same as your initial document, it defines the same elements.
tone wrote: The targetNamespace attribute, only used in the schema file, overrides the default namespace attribute targetNamespace="http://www.w3schools.com", and probably does some other things, which I don't understand yet.
The targetNamespace attribute in the schema file specifies the namespace for which you define that schema components (elements, attributes, types, etc.). That is because when you define an element you do not use a name with a prefix (qualified name), you only can use a non qualified name, thus the namespace information need to come from some other place, and that is the targetNamespace attribute. As an implication, in XML Schema you cannot define components in different namespaces in the same file.
tone wrote: And this line xmlns:xs="http://www.w3.org/2001/XMLSchema" sets a prefix of "xs" to allow this namespace to be referenced by "xs" instead of the full namespace. This is helpful when referring to multiple namespaces within the same xml file.
Yes, the above line defines the XML Schema namespace http://www.w3.org/2001/XMLSchema mapped to the xs prefix, so all the names that begin with xs: represent names in the XML Schema namespace.
tone wrote: And the schemaLocation attribute - xsi:schemaLocation="http://www.w3schools.com note.xsd" - tells the namespace ("http://www.w3schools.com") that the notes.xsd schema file will describe/define it.
Yes.

Best Regards,
George
George Cristian Bina
tone
Posts: 14
Joined: Sun Apr 29, 2007 12:41 am
Location: Tennessee

Post by tone »

George,

Thanks for all your explanations, I'm definately clearer on many aspects of this than when I first delved into it.
tone
Posts: 14
Joined: Sun Apr 29, 2007 12:41 am
Location: Tennessee

Post by tone »

For the benefit of anyone else reading this post I found a good intro to namespaces on MSDN.

http://msdn2.microsoft.com/en-us/library/aa468565.aspx
Post Reply