Re: Using multiple namespaces in a in an XML instance

Hi Lukas,

> The schema this points to is stuff.xsd, below. This is where I
> suspect the problems are, as you cannot simply say <element
> name="fruit:apple" type="fruit:apple-type"> type things. Also, I
> think this is where the references to the other schemas (below)
> should be, but can't figure out how to include them here. I have no
> intention of using the namespaces, they're just there to keep the
> validator happy(er).

All the elements you declare within a single schema document have to
belong to the same namespace. However, you can *reference* elements
from other namespaces no problem.

It looks as though you want to list the names of the elements the
stuff element can contain. In that case, you can use xs:element with
the ref attribute to refer to declarations in other documents, with:

<xsd:complexType name="stuff-type">
  <xsd:all>
    <xsd:element name="count" type="xsd:nonNegativeInteger" />
    <xsd:element ref="fruit:apple" />
    <xsd:element ref="fruit:orange" />
    <xsd:element ref="vegies:carrot" />
    <xsd:element ref="vegies:beans" />
    <xsd:element ref="cereals:rice" />
    <xsd:element ref="cereals:bread" />
  </xsd:all>
</xsd:complexType>

You also can tell the schema validator where to find the schemas that
are associated with these namespaces, using xs:import:

<xs:import namespace="http://www.stuff.com/fruit"
           schemaLocation="fruit.xsd" />

and so on. And/or you could use the xsi:schemaLocation attribute in
your instance to point the schema validator at the various schemas:

<stuff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="stuff.xsd"
       xsi:schemaLocation="http://www.stuff.com/fruit
                           fruit.xsd
                           http://www.stuff.com/vegies
                           vegies.xsd
                           http://www.stuff.com/cereals
                           cerials.xsd"
       xmlns:fruit="http://www.stuff.com/fruit"
       xmlns:vegies="http://www.stuff.com/vegies"
       xmlns:cereals="http://www.stuff.com/cereals">
...
</stuff>

I'm not sure whether it's appropriate in your real situation, but you
might want to consider using a wildcard rather than listing the
elements. Something like:

  <xs:sequence>
    <xs:element name="count" type="xsd:nonNegativeInteger" />
    <xs:any namespace="http://www.stuff.com/fruit
                       http://www.stuff.com/vegies
                       http://www.stuff.com/cereals"
            minOccurs="6" />
  </xs:sequence>

It obviously depends on how 'plug-and-play' you want your stuff
element to be.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Wednesday, 19 December 2001 14:34:24 UTC