views:

392

answers:

2

Hi I am trying to get my mind around XSDs, XML and namespaces but I can't get things to work the way I want them to.

I have an XSD which, at the moment, starts like this:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://www.example.com"&gt;
    <xs:import namespace="http://www.example.com" schemaLocation="Include.xsd" />

As you can see, it imports another xsd file, which starts like this:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" 
           targetNamespace="http://www.example.com" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://www.example.com"&gt;

Then I have some xml documents based on this schema:

<foobar>
    <whatever>....

Basically I don't care what the namespaces are, I just want the darn thing to work. Previously I wasn't using any namespace but it seems that I have to use them in order to import one XSD into another. What I don't want to do is have to supply prefixes to all the elements in my xml documents.

In order to achieve this, what combination of values do I need for the various attributes (targetNamespace, namespace, xmlns, elementFormDefault etc) in the XSD and XML documents?

Currently, any elements defined in the imported XSD document require namespace qualification in the XML.

+1  A: 

I would think you need to set your elementFormDefault to "unqualified" if you want to avoid the prefixes on your XML elements. The rest should be fine, I think - the namespace is the same between your master and your imported XSD - that's fine. You specify the xmlns= without a prefix - that's fine. You should be good to go.

Marc

marc_s
Thanks, you're right - I was closer than I thought. However I am finding that when I create my XML document, elements that are defined in the imported XSD require that the namespace be specified (even though both the imported XSD and the actual XSD each have elementFormDefault set to 'unqualified').
cbp
+2  A: 

If you aren't using namespaces or your schemas share the same namespaces, you would be much better off using xs:include rather than xs:import. Schema A can include schema B if B either has the same namespace as A or has no namespace declared (if the latter is the case, B 'adopts' the includer's namespace when included). So... something like:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://www.example.com"&gt;
    <xs:include schemaLocation="Include.xsd" />

<!-- ... -->

</xs:schema>
Nic Gibson
ahhh right! thanks
cbp