Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD with imports and namespaces

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">
    <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">

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.

like image 542
cbp Avatar asked May 27 '09 04:05

cbp


2 Answers

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">
    <xs:include schemaLocation="Include.xsd" />

<!-- ... -->

</xs:schema>
like image 83
Nic Gibson Avatar answered Sep 20 '22 05:09

Nic Gibson


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

like image 41
marc_s Avatar answered Sep 19 '22 05:09

marc_s