[Home] [By Thread] [By Date] [Recent Entries]

  • To: Daniel Prager <danielp@d...>
  • Subject: Re: Fine namespace control in XML Schema
  • From: Eddie Robertsson <erobertsson@a...>
  • Date: Thu, 11 Jul 2002 14:03:55 +1000
  • Cc: xml-dev <xml-dev@l...>
  • References: <3D2CF3EF.6CFAD59E@d...>
  • User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.0) Gecko/20020530

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



Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member