This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Problem with XML validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Problem with XML validation" Watch "Problem with XML validation" New topic
Author

Problem with XML validation

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Hi all,
For the below two files:

namespace.xml:


namespace.xsd:


The error i am getting is:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'B'. One of '{"":B}' is expected.

I am unable to understand that if elementFormDefault="unqualified" means that the local elements will not be qualified and will be assumed to be in parent's namespace,why is the xml file showing error(working in eclipse)?The namespace of the root in xml instance is defaultNamespace,but as per rule,<B> should not be qualified and assumed to belong to <A>'s namespace,which is same as defaultNamespace itself,and same is the targetNamespace of <B> in xsd file,then why is error coming?

Any guidance will be highly helpful.
Thanks ...
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
What is the error message? Have you tried to validate the A document outside of Eclipse?
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Hi Jimmy,
I have edited my code in my previous codes.Please have a look at them and now i have also mentioned the error occuring.

Any guidance will be helpul.

Thanks...
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
<restriction base="string">
<pattern value="\d{3}-\d{3}"/>
</restriction>

What is the purpose of these elements?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't know enough about Schema to say anything useful about that part of your question. But on the other hand I think there's something you don't understand about namespaces, and using Schema terminology to describe what you are trying to do doesn't help.

So suppose you used a namespace prefix for your namespace. Then which of these two documents do you think would be equivalent to what you originally posted?



g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/namespace" xmlns:tns="http://www.example.org/namespace" elementFormDefault="unqualified">

Your problem encountered is due to elementFormDefault being the default "unqualified". It means that any local element would not be living in the target namespace, instead, if not referenced to some specific imported schema, would be in null namespace. Hence, the error message:
One of '{"":B}' is expected.

This is understandably not the easiest part of w3 schema language to digest.

The line should be written like (which conforms to your intention) - watch the only difference in elementFormDefault.

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Thanks tsuji for the reply,
It almost solved my problem,but I still want to ask one more thing:-
Giving elementFormDefualt="unqualified" means that the local elements I declare in my schema wil not be added to my target namespace?
Am I correct or I am getting concepts wrong?

Please guide me through.
Thanks.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
Giving elementFormDefualt="unqualified" means that the local elements I declare in my schema wil not be added to my target namespace?
Am I correct or I am getting concepts wrong?

That's what it means. They will be decoupled from the tns.
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 215

Thanks tsuji,
I think I am getting hold of thing now
But,still,if the local elements are decoupled from the target namespace,then how come they are validated in the xml schema if i include the target namespace in the xml instance.How does the validator know in which namespace to find the definition for the local elemnts?I checked and found out that the local unqualifed elements belonged to null namespace,then how come they are verified according to their definition in the xml schema?

Thanks.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
It is a normal state that an xml document contains vocabulary drawn from multiple namespace. The same for a schema which actually stands for a collection of schema documents parsed in with specific objectives. Each schema document will be itself "validated" automatically. A vocab, content-model and datatype would be built into the memory. It is true that each xsd taken separately can have at most one target namespace. When either of elementFormDefault or attributeFormDefault is default, unqualified, the content-model and vocab would actually be populated with two kinds of data set, one with the tns and the other corresponds to that of the null ns. With the holistic schema built, there is no reason why it can't assess elements/attributes belonging to null ns. All these rest "verbal" to help the mental process to visualize how things work. Would this help a bit?! The remaining is the 0-&-1-things, I am afraid.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Reffering to the XML Schema "default" namespace as the "null" namespace might not be concise. The primary purpose of Namespace functionality is to permit the usage of identical element names when creating a document with elements from more than one XML-based language. When you are only using a single vocabulary, a clash of element names cannot occur and declaring a namespace is not really necessary or meaningful.

Aside, unqualified elements will belong to the default namespace. So when an XML Schema document is parsed elements can be qualified or not, it doesn't make a difference as there is only one vocabulary.

When an author is creating a XML Schema, he/she needs to consider if there could be element name clashes in XML instances. Again, this can only happen if there is more than one schema involved in document creation.

It is not normal for XML-based documents to contain elements from more than one language. This is an "exceptional" case and the Namespace functionality was created to permit these type of documents. This is one of the "extensions" built into XML on top of the SGML foundation.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
Reffering to the XML Schema "default" namespace as the "null" namespace might not be concise.

Concise? No, it is straightly wrong. But are you saying I were at certain places in my answers saying so? where?
 
 
subject: Problem with XML validation
 
Similar Threads
JAXB: Two xsd - Create single JAXBContext
help requied for <restriction base="ID">
XSD Schema Error in WSAD
Unable to refer global element