Re: [xml-dev] Fine namespace control in XML Schema

From
Eddie Robertsson <>
To
Daniel Prager <>
Date
2002-07-11T04:03:55Z
ID
<>
Thread
Re: [xml-dev] Fine namespace control in XML Schema
Hi Daniel,

>Is it possible to add elements to distinct namespaces in an
>XMLSchema and sub-elements to the global namespace
>(since contect will disambiguate these nicely)?
>
Certainly. See below.

>E.g. Consider the following XML document:
>
>----------------------------------------------------------------
><example xmlns:a="http://www.example.com/a"
>   xmlns:b="http://www.example.com/b">
>
> <a:frog>
>  <name> Spotted tree frog </name>
>  <colour> Blue with red spots </colour>
>  <leap> minimal </leap>
> </a:frog>
>
> <b:frog>
>  <name> Cane toad </name>
>  <poison> Nasty </poison>
>  <fecundity> enormous </fecundity>
> </b:frog>
></example>
>----------------------------------------------------------------
>
</snip>

>I can't figure out how to disambiguate the two frog
>elements in XML Schema (using XML namespaces).
>I have looked at `any' and `elementFormDefault',
>but neither seems to provide sufficient specifity.
>
The elementFormDefault attribute is the way to go. For this example you 
need to set elementFormDefault to "unqualified" for the schema documents 
that declares the two frog elements. Note that to pull this off using 
W3C XML Schema you need to define three different schema documents:

<!-- example.xsd -->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="unqualified" attributeFormDefault="unqualified" 
xmlns:a="http://www.example.com/a" xmlns:b="http://www.example.com/b">
    <xs:import namespace="http://www.example.com/a" 
schemaLocation="afrog.xsd"/>
    <xs:import namespace="http://www.example.com/b" 
schemaLocation="bfrog.xsd"/>
    <xs:element name="example">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="a:frog"/>
                <xs:element ref="b:frog"/>
            </xs:sequence>
        </xs:complexType>   
    </xs:element>
</xs:schema>

<!-- bfrog.xsd -->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="unqualified" attributeFormDefault="unqualified" 
targetNamespace="http://www.example.com/b">
    <xs:element name="frog">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="poison" type="xs:string"/>
                <xs:element name="fecundity" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

<!-- afrog.xsd -->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.com/a" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="unqualified" attributeFormDefault="unqualified">
    <xs:element name="frog">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="colour" type="xs:string"/>
                <xs:element name="leap" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The example.xsd schema should validate you example document.

Cheers,
/Eddie