Confused about namespace in XML schema and in XML instance doc

J

Jamie Chen

Hello, need some help to understand this.(I am using DevSutio.Net)

I set the default name space to be "http://www.w3.org/2001/XMLSchema"
in my XML schema and I use my XML schema as the default namespace in
XML instance doc. But it turns out to invlaid. Need some help to
understand why.

Many thanks

///// Here is my schema
<?xml version="1.0" encoding="utf-8" ?>
<schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
xmlns:jchen="http://tempuri.org/XMLSchema.xsd"
xmlns="http://www.w3.org/2001/XMLSchema"> <<=I made this as my
default namespace
<complexType name="personType">
<sequence>
<element name="FirstName" type="string" />
<element name="LastName" type="string" />
</sequence>
</complexType>
<complexType name="peopleType">
<sequence maxOccurs="unbounded">
<element name="Person" type="jchen:personType" />
</sequence>
</complexType>
<element name="People" type="jchen:peopleType"></element>
</schema>

//// I then test my schema in DevStudio.Net and it becomes invalid
<?xml version="1.0" encoding="utf-8" ?>
<People xmlns="http://tempuri.org/XMLSchema.xsd"> <<=Use my schema as
default
<Person>
<FirstName> Jamie </FirstName>
<LastName> Chen </LastName>
</Person>
<Person>
<FirstName> Jonathan </FirstName>
<LastName> Chen </LastName>
</Person>
<People>

///// Person tag cannnot be found, this is very strange
 
R

Richard Tobin

Jamie Chen said:
<complexType name="peopleType">
<sequence maxOccurs="unbounded">
<element name="Person" type="jchen:personType" />

This declares a local element "Person". By default, local element
declarations apply to elements in no namespace.

You have used the (more common) convention of having all your elements
in the same namespace, rather than making the non-top-level ones be in
no namespace. So you need to add an attribute
elementFormDefault="qualified" to the schema element. (Or you could
add elementForm="qualified" to the local element declaration, but if
you're going to follow the usual convention it's simpler to set the
default at the top.)

-- Richard
 
P

Priscilla Walmsley

Hi,

Your "Person" element is locally declared, which means that by default
it should be in no namespace. In your instance, since you use a default
namespace declaration, the Person element is considered to be in that
namespace, hence the mismatch.

You have two choices:

1. Add elementFormDefault="unqualified" to your schema element.

2. Change your instance so it looks like this, with a prefix defined and
the prefix only on the root element:

<ex:people xmlns:ex="http://tempuri.org/XMLSchema.xsd">
<Person>
<FirstName> Jamie </FirstName>
<LastName> Chen </LastName>
</Person>
<Person>
<FirstName> Jonathan </FirstName>
<LastName> Chen </LastName>
</Person>
<ex:people>

Hope that helps,
Priscilla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top