Click to See Complete Forum and Search --> : namspace question


r0k3t
12-18-2008, 10:41 AM
So I have been doing some reading about namespaces, and there is something I don't really get so if someone could clear it up for me that would be great!

Lets take the example below, as many of you will recognize it is a SOAP document. Now, the root element is soap:Envelope right, makes sense cause I see the xmlns declaration in the element. It is the element called GetCategories that I don't understand... Why are they specifying the namespace like that? I mean, I have probably read an explanation of why but am being dense. Why does the namespace in that particular instance have no name associated with it, i.e. xmlns:soap, it is just xmlns.

Forgive me if these are elementary or obvious questions but I just am not putting it all together just yet.

Thanks



<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCategories xmlns="http://microsoft.com/webservices/" />
</soap:Body>
</soap:Envelope>

Charles
12-18-2008, 12:44 PM
Namespaces allow you to mix mark up languages without worrying about the tag names clashing. And they make it clear when you have extended the ML.

Your soap:Envelope element is really the http://schemas.xmlsoap.org/soap/envelope:Envelope element. But that would be too hard to keep typing so you have two short hands. You can specify the default namespace with the "xmlns" attribute. The current element and its children will default to that namespace. And you can create a short hand with the "xmlns:xxx attribute.

The following are all the same:<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCategories xmlns="http://microsoft.com/webservices/" />
</soap:Body>
</soap:Envelope>

<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<GetCategories xmlns="http://microsoft.com/webservices/" />
</Body>
</Envelope>

<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:crap="http://microsoft.com/webservices/">
<Body>
<crap:GetCategories/>
</Body>
</Envelope>

<foe:Envelope xmlns:fee="http://www.w3.org/2001/XMLSchema-instance" xmlns:fie="http://www.w3.org/2001/XMLSchema" xmlns:foe="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fum="http://microsoft.com/webservices/" >
<foe:Body>
<fum:GetCategories/>
</foe:Body>
</foe:Envelope>

r0k3t
12-18-2008, 01:24 PM
Charles - that was really a great reply - thank you so much.

Now - If I could ask just one more question to make sure I am understanding this. Using the third example you presented.


<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<GetCategories xmlns="http://microsoft.com/webservices/" />
</Body>
</Envelope>


What you have done is with xmlns="http://schemas.xmlsoap.org/soap/envelope/ is said "envelope is the default namespace for this document" - BUT... Later on we see the GetCategories element, this then changes the default namespace??? I am assuming it only applies to its children of GetCategories, correct??? (of course in this case it doesn't have any) - once the element is closed the default namespace reverts to envelope?

I think I am getting this correctly... I hope!

Thanks again

Charles
12-18-2008, 02:45 PM
Setting the default namespace applies to the element with the declaration and its children.