aspose file tools*
The moose likes XML and Related Technologies and the fly likes Help me for xsd creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Help me for xsd creation" Watch "Help me for xsd creation" New topic
Author

Help me for xsd creation

john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
I am including one xml file into other:


main.xml
-------------------------------------------------


suppose below is the foo.xml
-----------------------------------------------



xsd for foo.xml is shiporder.xsd.

Now i parsed the main.xml using dom, since there is no xsd defined for main.xml hence during dom parser validation i was getting following exception:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'root'.

can someone please let me know how to write an xsd for the main.xml so that I can parse the main.xml. I tried to write the same however not sure how to deal with "<!DOCTYPE doc " in xsd.

(there is already an xsd for foo.xml which i think have to include in main.xsd)


g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
[0] First, you have to put the root element name and that of the DOCTYPE agrees with each other. At present, it is "root" in one and "doc" in another. I take it "doc" as the actual name hereinbelow (or switch accordingly).

[1] The xsd for the main (the containing xsd and xml document) contains two main ingredients, namely, an xs:include to include the contained xsd of shiporder; and then the root element specification using a ref attribute to put through it to reference the included xsd.

[1.1] You don't have to put any xs elements concerning DOCTYPE, no need, not at all.

[2] In majority of cases, that is already done. But in this case that the xml block shiporder is actually included via an ENTITY reference, that is more delicate. You have to modify a bit the shiporder.xsd in order to succeed. Those modifications can be left there even it is used in other cases as a component xsd, so that is not tied to the main.xsd or however it is called.

[2.1] In the shiporder.xsd, you have some xs:element name="shiporder". The type may or may not be scripted in a complexType component, shiporderType, say. I suppose it is just for convenience. In the complexType element, add an attribute xml:base reference as follows.


[2.2] And then at the top-level add an import element to import the xml namespace (with remote schema location or your local copy of it if exists).


With all the above, it's done.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
Thanks for the reply, However I am still getting the same error, when parsing using dom parser.

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'root'.


Used the same xsd for main.xml posted by you



Seems like in above xsd definition for root tag is missing. Please correct?

I changed the schema for shiporder.xsd as well:




More over can you please elaborate on point[0]:
" First, you have to put the root element name and that of the DOCTYPE agrees with each other. At present, it is "root" in one and "doc" in another. I take it "doc" as the actual name hereinbelow (or switch accordingly). "

please let me know in case any further modification is required in the schema?
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
If your error message is saying "root", I would then expect your containing xml start with <root>... in that case the doctype should be written as this - and that is what I meant.


Forgot to mention the obvious related change you've to make in the xsd.


After changing that, it seems you've put in all the elements that I mentioned. It should perform well.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
sorry I tried this as well however again getting the same error...element type root is not declared.

you can also check the same in following tool for xml validation

http://pscode.org/xml/xmltools.html

more over I tried the same with xinclude as well

<?xml version="1.0"?>
<root>
<xi:include href="foo.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
</root>


but this is not including the foo.xml during parsing. All the following attributes are already set. How the same can be done with xinclude?

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
Make sure you've internet connection when you run the validating application. And then all the xsd, xml in appropriate directory. As to xi:include, I won't indulge into moving target. It is not necessary. However, if there are too many factors you are not sure what they are, I cannot debug what I don't see.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
let's leave xinclude for now. For the first scenario, the program that i wrote to parse the xml is also giving the same error. "root not defined."
All the files along with the program parsing those files are under the same directory.

I did all the changes as suggested by you however not sure where i am doing wrong.

following is the main.xml



following is the main.xsd


following is shiporder.xml



following is shiporder.xsd



Now following is the code to parser the xml


g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
Your dom parser cannot validate anything yet. You miss a large chunk of establishing a validator. I can send you a descent article where you can find sample code clearly laying out the loading of schema...
http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi/index.html

You can amend your code with the elements as shown in list 4 of the above reference without purposelessly inflating the code.

In loading the xml and xsd, you can set them up with main.xml and main.xsd alone. Let the engines load the included xml and xs:include schema of shiporder.xml and shiporder.xsd.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
1. you mean we should keep the xsd and xml as it is.

2. From the code we should remove

factory.setValidating(true);
factory.setXIncludeAware(true);

3. and let the validator validate the xml? using
validator.validate(source, result);

4 and file will have reference to main.xsd

File schemaLocation = new File("main.xsd");

Please correct.

More over can you pl elaborate on follwing lines?

"In loading the xml and xsd, you can set them up with main.xml and main.xsd alone. Let the engines load the included xml and xs:include schema of shiporder.xml and shiporder.xsd. "



g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
What do you want to get assured? This is not a purely mental exercise or a certification multiple choice kind of answer. I stand by my answer to your question and the answer on the construction of the schemas is the most explicit one can imagine already. Do you understand what I am saying on your code? The code is defective. Read the reference article - that will do you good.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
What I want to make sure that after reading the entire article I don't get the same error again....as already spend lot of time in this whereas the alternate is always available.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
Thanks a lot for the help ...
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
>What I want to make sure that after reading the entire article I don't get the same error again....as already spend lot of time in this whereas the alternate is always available.
Great, I find it wonderful and amusing.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 93
please read your private messages.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 547
    
    3
If you want to spare the part of establishing SchemaFactory etc etc, somewhat like the code you posted, letting the parsing engine to acquire its knowledge of the whereabout of the schema file implicitly, you can do the same like your shiporder.xml, namely, amend the main.xml with the noNamespaceSchemaLocation attribute, like this.

This would allow you to go for a quick test, although I would prefer to tighter control of the selection of schema files to validate against, but that's only a personal preference. (Also, why don't you make the encoding more consistent?)

...Peace in mind and good luck to your going research.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help me for xsd creation