angelweave

August 22, 2003

#)(%&*@#)%(&#)(@% Namespaces So, here's a


So, here's a geek post. I don't do this very often.

I thought I knew XML. I'm a datahead, yeah. With a capital D. But I got thrown for a loop.

I read about namespaces...in many places, no less. I read the W3C stuff. I consulted a Wrox book. I was ready to go. So I constructed my schema, the thought being that I would have a master schema that would define the highest level elements simply and precisely, and secondary files would hold the scullery data elements - those in supporting roles. Okay - simple enough.

The namespaces had other evil ideas.

But, with help of the noble Hans, I have conquered the foe and feel the need to document the correlation between schema documents aligned to do the same thing and how the instance document makes the validation phone call to the proper schema. As far as I can tell, I found NOTHING on the web with the correlation of all of these things in one place. I aim to change that.

Okay - here's the papa schema (papaDog.xsd) - the one that contains the master elements. Copy it into a file of any name to test it. Honor me by keeping the name the same.

<?xml version="1.0" encoding="UTF-8" ?>
     <schema xmlns="http://www.w3.org/2001/XMLSchema"
     xmlns:example="http://angelweaving.blogspot.com/schema" targetNamespace="http://angelweaving.blogspot.com/schema"
     elementFormDefault="qualified" attributeFormDefault="unqualified">

     <include schemaLocation="http://angelweaving.blogspot.com/schema/babyBearSchema.xsd" />

     <element name="topDog" type="example:bigDogExample" />

     <complexType name="bigDogExample">
          <sequence>
               <element name="blah1" type="example:bigDogElement1" />
               <element name="blah2" type="example:bigDogElement2" />
          </sequence>
     </complexType>
</schema>

Fairly simple, no? Okay, the secondary schemas should follow this pattern (this one is named babyBearSchema.xsd). Feel free to copy it into a file with that name to test it.

<?xml version="1.0" encoding="UTF-8" ?>
<schema targetNamespace="http://angelweaving.blogspot.com/schema"
xmlns:example="http://angelweaving.blogspot.com/schema"
xmlns="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
     <complexType name="bigDogElement1">
          <sequence>
               <element name="blech1" type="string" />
               <element name="blech2" type="boolean" />
          </sequence>
     </complexType>

     <simpleType name="bigDogElement2">
          <restriction base="string">
               <maxLength value="27" />
          </restriction>
     </simpleType>
</schema>

Okay - so here are the two schemas. I find this SO non-intuitive as a developer. My simple mind kept trying to put the lower-level schema INTO the upper-level (Papa) schema. You know, like include files. But, no, of course, that's not how it goes. A namespace is like a room. All of my smaller schemas can use the same namespace since, as Hans pointed out, I control them all - so I can stop there from being a naming collision. As you can see, both schema "documents" refer to http://angelweaving.blogspot.com/schema as the reference for the "example" namespace.

Okay, realizing that, you're most of the way there. The next thing to take note of is the xmlns that stands alone. That points to the W3C's schema; this must be in your schema. This is consistent across both documents. The targetNamespace is also http://angelweaving.blogsot.com/schema in my example - both documents.

Last thing to notice: the reference to babyBearSchema.xsd should point to EXACTLY where this schema sits. This is papaDog.xsd that's pointing to it. Without this, no validation for you.

Now, these guys validate. Here's the validator I'm using to validate papaDog.xsd. I have babyBearSchema.xsd sitting in the proper place on my site.

And now, the instance document. Stop, get some popcorn and something to drink.

<?xml version="1.0" ?>
<topDog xmlns="http://angelweaving.blogspot.com/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://angelweaving.blogspot.com/schema http://angelweaving.blogspot.com/schema/papaDog.xsd">

     <blah1>
               <blech1>Atomic Dog</blech1>
               <blech2>0</blech2>
     </blah1>
     <blah2>abcdefghijklmnopqrstuvwxy</blah2>
</topDog>

Yeah, they work together quite nicely. Note that the instance document points to the same namespace. The xsi namespace points to the W3C's Schema place. The schemaLocation, though, is the kicker. This, again, is completely non-intuitive to a developer. Note that there are two "parameters" within the opening and closing quotes. WITH A SPACE BETWEEN. Aargh!

The first is our friendly namespace, and the section is the actual location of the schema.

I hope this helps. If you see any errors with this post, please e-mail me at angelweaving@hotmail.com.

And forgive my indentation. Forcing it with HTML workarounds is, well, tiring. Perhaps I'll correct it when I have more time.

hln

Posted by hln at August 22, 2003 11:47 PM | Technology
Comments