File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes General XML problem with namespaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "General XML problem with namespaces" Watch "General XML problem with namespaces" New topic

General XML problem with namespaces

Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
My XML file:

My schema named "Client.xsd":

Both files reside in the same folder. I can't validate the file because of:"Cannot find the declaration of element 'cli:set_Id'. I admit that by now I am totally confused about the usage of namespaces. I do know what they are for, but I just cannot get it right.

[ September 25, 2006: Message edited by: Mike Himstead ]

[ September 25, 2006: Message edited by: Mike Himstead ]
[ September 25, 2006: Message edited by: Mike Himstead ]
vinay kumar
Ranch Hand

Joined: Sep 23, 2006
Posts: 31
Shouldn't the xsd be like:

<xsd:simpleType name="TId">
<xsd:restriction base="xsd:normalizedString"><xsd:maxLength value="16"/>

<xsd:element name="set_Id">
<xsd:element name="Id" type="cli:TId"/>

Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
First of all the schema itself is not WELLFORMED(note not the same as valid!!!) as the end tag for schema element is not prefixed with xsd - it should be </xsd:schema>. Hence its not even valid XML.

The problem isnt really namespace related, its just complicated by the fact that you are using them. Based on the instance doc, you seem to want the set_Id selement to be the root, with a child called Id which must be of type "normalizedString" with a restriction that its maxLength must be 16.

You defined a type called cli:TId as a xsd:normalizedString that is restricted to 16 characters, this is perfect. You can now reuse this.

You then defined an element called set_Id which is of type cli:TId. Hence set_Id cannot contain child elements (or attributes) as it is a simple type.
That is not what you want as you want set_Id to have a child called cli:Id.

Also, you try to define a 2nd element called Id of type cli:Id. but that wont work as you never defined the type cli:Id.

Here is what you should do
1) Leave the cli:TId alone - this is a valid simpleType derived by restriction.

2) Redfine the cli:set_Id element as follows, to contain the cli:Id element

This defines the root element set_Id to be a complexType with a following sequence of elements as children : Id (only one child in this case). The child Id is defined to be of type cli:TId so its of the restricted type defined earlier. (Note that defining the Id element inside the set_Id element, as opposed to top level like set_Id, means it cannot be a root of the document, which is what you want since set_Id is the required root)

3) Change the targetNamespace to be someURL (no spaces, instance doc does not like space, for reasons you'll see in a second)

This fixes the schema, here it is in full :

4) Instance doc also has a problem. The xsi:schemaLocation attribute takes 2 parameters sepatated by a space. The first is the namespace the schema is in, the second is the schema location.
Hence, it should read

Do you now see why whitespaces in the schema namespace name would be a bad idea? It would not be possible to identify where the namespace ended and the schema location started if it were like this

Hence the xmlns:cli="some URI" needs to be changed to xmlns:cli="someURI"

5) In the instance doc, there is no default namespace set, so the Id element must be fully qualified using the namespace defined (the local name is not enough)
It should read

This fixes the instance doc, here is is in full :

Hope this helps, im studying for XMLMaster Basic exam at the moment and know that schema/namespace issues can be really confusing.


<a href="" target="_blank" rel="nofollow">Use Code Tags!!</a>
I agree. Here's the link:
subject: General XML problem with namespaces
It's not a secret anymore!