[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Fine namespace control in XML Schema

  • 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

example xsd
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



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.