W3C home > Mailing lists > Public > xmlschema-dev@w3.org > July 2000

Re: Complex types from another namespace

From: Henry S. Thompson <ht@cogsci.ed.ac.uk>
Date: 25 Jul 2000 13:19:06 +0100
To: "Paul Spencer" <paul.spencer@boynings.co.uk>
Cc: <xmlschema-dev@w3.org>
Message-ID: <f5baef6l7wk.fsf@cogsci.ed.ac.uk>
"Paul Spencer" <paul.spencer@boynings.co.uk> writes:

> I am currently developing several schemas based on a common set of
> complex data types. It seems reasonable to put the common types in one
> namespace and the "end-user" schemas in several others. My problem is
> that an instance document then has to be aware of which elements are
> from which namespace. Is this a fundamental feature of XML Schema or am
> I doing something wrong?
> 
> So for example, a simplified file for the common types might look like:
> 
> b.xsd:
> ======
> 
> <?xml version="1.0"?>
> <xsd:schema
> 	targetNamespace="http://example.com/b.xsd"
> 	xmlns:xsd="http://www.w3.org/1999/XMLSchema"
> 	xmlns:b="http://example.com/b.xsd"
> 	elementFormDefault="qualified"
> 	attributeFormDefault="unqualified">
> 
> 	<xsd:complexType name="AddressStructure">
> 		<xsd:element name="b:AddressLine" minOccurs="2" maxOccurs="5"
> type="xsd:string"/>
> 		<xsd:element name="b:PostCode" minOccurs="0" maxOccurs="1"
> type="xsd:string"/>
> 	</xsd:complexType>
> </xsd:schema>

There is a serious error above -- you must _not_ use QNames as the
names of elements or attributes!  Suppose you fix the offending lines
as follows:

	<xsd:element name="AddressLine" minOccurs="2"
                     maxOccurs="5" type="xsd:string"/>
	<xsd:element name="PostCode" minOccurs="0"
                     maxOccurs="1" type="xsd:string"/>

> Any required types would then be imported into what I am calling the
> "end-user" schema (the one that I want people to reference as the
> default in instance documents):
> 
> a.xsd:
> ======
> 
> <?xml version="1.0"?>
> <xsd:schema
> 	targetNamespace="http://example.com/a.xsd"
> 	xmlns:xsd="http://www.w3.org/1999/XMLSchema"
> 	xmlns:b="http://example.com/b.xsd"
> 	xmlns:a="http://example.com/a.xsd"
> 	elementFormDefault="qualified"
> 	attributeFormDefault="unqualified">
> 
> 	<xsd:import namespace="http://example.com/b.xsd"/>
> 
> 	<xsd:element name="Address" type="b:AddressStructure"/>
> </xsd:schema>

That's all fine.

> I can then create a simple instance document like this:
> 
> simple.xml
> ==========
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <Address
> 	xmlns="http://www.boynings.co.uk/GovTalk/Schemas/a.xsd"
> 	xmlns:b="http://www.boynings.co.uk/GovTalk/Schemas/b.xsd">
> 
> 	<b:AddressLine>1 Somestreet</b:AddressLine>
> 	<b:AddressLine>Sometown</b:AddressLine>
> </Address>
> 
> Is there any way I can do this so that the instance document does not
> need to be aware of which elements are from which namespace? I have just
> worded that in a way that makes me think I am trying to defeat the
> object of namespaces, but my aim is to ensure the uniqueness of names
> through my end-user schemas, which have knowledge of what data types
> they are using. I don't want the instance documents to have to know the
> way the schema is constructed (i.e. whether it is defining the complex
> types itself or importing them).

Since 'AddressLine' and 'PostCode' are local to 'AddressStructure',
if you change elementFormDefault to 'unqualified' in b.xsd you will
get a close instance, if I understand you correctly:

<?xml version="1.0" encoding="UTF-8"?>
<a:Address
	xmlns:a="http://www.boynings.co.uk/GovTalk/Schemas/a.xsd">

	<AddressLine>1 Somestreet</AddressLine>
	<AddressLine>Sometown</AddressLine>
</a:Address>

I find this a bit obscure/misleading -- the other alternative is to
merge the type definitions into the end-user namespace, by taking
advantage of the interaction between <include> and no-namespace
schemas:

b.xsd:
======

<?xml version="1.0"?>
<xsd:schema
	xmlns:xsd="http://www.w3.org/1999/XMLSchema"
	elementFormDefault="qualified"
	attributeFormDefault="unqualified">

	<xsd:complexType name="AddressStructure">
		<xsd:element name="AddressLine" minOccurs="2" maxOccurs="5"
type="xsd:string"/>
		<xsd:element name="PostCode" minOccurs="0" maxOccurs="1"
type="xsd:string"/>
	</xsd:complexType>
</xsd:schema>

a.xsd:
======

<?xml version="1.0"?>
<xsd:schema
	targetNamespace="http://example.com/a.xsd"
	xmlns:xsd="http://www.w3.org/1999/XMLSchema"
	xmlns:a="http://example.com/a.xsd"
	elementFormDefault="qualified"
	attributeFormDefault="unqualified">

	<xsd:include
            schemaLocation="http://www.boynings.co.uk/GovTalk/Schemas/b.xsd"/>

	<xsd:element name="Address" type="AddressStructure"/>
</xsd:schema>
 
instance:

<?xml version="1.0" encoding="UTF-8"?>
<Address
	xmlns="http://www.boynings.co.uk/GovTalk/Schemas/a.xsd">

	<AddressLine>1 Somestreet</AddressLine>
	<AddressLine>Sometown</AddressLine>
</Address>

ht
--
  Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
          W3C Fellow 1999--2001, part-time member of W3C Team
     2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
	    Fax: (44) 131 650-4587, e-mail: ht@cogsci.ed.ac.uk
		     URL: http://www.ltg.ed.ac.uk/~ht/
Received on Tuesday, 25 July 2000 08:19:13 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:14:17 GMT