views:

503

answers:

1

In programming languages, I'm used to

namespace foo
{
    namespace bar
    {
        void DoIt()
        {
        }
    }
}

int main()
{
    foo::bar::DoIt()
}

In a schema(xsd), I'd like to do similar things when defining complexTypes

<xsd:schema targetNamespace="http://www.stackoverflow.com/foo"&gt;
 <xsd:complexType name="bar1">
    ...
 </xsd:complexType>
 <xsd:complexType name="bar2">
 </xsd:complexType>
   ...
 <xsd:complexType name="foo1" type = "bar1">
   ...
 </xsd:complexType>
</xsd:schema>

Conceptually I would like bar1 and bar2 to be nested in a http://www.stackoverflow.com/foo::bar namespace (:: borrowed from C++). I've seen examples of what seems to be a completely second namespace defined for the bar stuff. In these examples, the bar stuff will be defined in a second namespace, "http://www.stackoverflow.com/foo/bar", as in the example below. This is the closest I've seen to a "nested" namespace.

So one thing I can kind of do is import the bar stuff from a different schema

<xsd:schema targetNamespace="http://www.stackoverflow.com/foo"
  xmlns:bar = "http://www.stackoverflow.com/foo/bar"&gt;
<xsd:schema namespace="http://www.stackoverflow.com/foo/bar"
 <xsd:complexType name="foo1">
   <xsd:element name="bar1Instance" type="bar:bar1"/>
   ...
 </xsd:complexType>
</xsd:schema>

Part of this is that I'm just trying to grok how namespaces are different in XML vs say C++. Is there a concept of nested namespaces in XML or is each namespace independent? How is the concept of nested namespaces implemented or typically done? Is it done like my example above? Are namespaces completely independent or is there anyway of creating a nesting relationship?

+2  A: 

In XML, each namespace is entirely independent. The actual string of a namespace is not parsed in any meaningful way. It just is. XML Namespace strings are typically URLs because this makes a good chance that people won't accidentally pick the same namespace for unrelated schemas.

If you Google, you will find examples of "nested XML namespaces." These examples aren't talking about what you're asking about, not in the sense of C++ (or Java or C#...) namespaces. These examples show XML documents with multiple namespaces in use.

An XML namespace string is simply an opaque and unique string that represents the schema. If two namespace strings happen to be very similar, it doesn't mean anything.

Eddie
Just one nit: XML namespaces are *required* to be legal IRIs (Internationalized Resource Identifier), which is a superset of URIs, which is a superset of URLs. Your real point is absolutely correct, though; the parser treats the namespace as a completely opaque string.Ref: http://www.w3.org/TR/REC-xml-names/
Peeja
@Peeja: Good clarification. Thanks.
Eddie