jQuery in Action, 2nd edition*
The moose likes Product and Other Certifications and the fly likes Validating XML against Schema. 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 » Certification » Product and Other Certifications
Bookmark "Validating XML against Schema." Watch "Validating XML against Schema." New topic
Author

Validating XML against Schema.

Siva shanmugam
Greenhorn

Joined: Jul 18, 2002
Posts: 15
I am trying to validate an xml file against a schema.
Schema file mySchema.xsd
------ ---- ------------
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://mySpace.com" xmlns="http://mySpace.com">
<xs:element name="Parent">
<xs:complexType>
<xs:sequence>
<xs:element name="Child"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsd:schema>
Xml File
--------
<?xml version="1.0"?>
<Parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mySpace.com mySchema.xsd"
xmlns="http://mySpace.com">
<Child/>
</Parent>
While parsing the xml file using xerces DOM i am getting the following error.
Error General Schema Error: Grammar with uri 2: http://mySpace.com , can not found.
Error Element type "Parent" must be declared.
Error Element type "Child" must be declared.

I think the namespace which is refered in xml file is not understood by the parser.
Can anyone tell me, how can i make the parser to understand the Schema ?
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
You can use the parser.setPropertry
like
parser.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
"http://www.example.com/quote.html quotations.xsd");
I found this at page 27 of pdf tutorial at ibm tutorial on schema validation


Spritle Software Blogs
Siva shanmugam
Greenhorn

Joined: Jul 18, 2002
Posts: 15
I have tried the above code.But the response is the same.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1167
Siva,
Here is a namespaceless working version.


Do you want it with namespaces?
Cheers,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1167
With namespaces -

[ September 26, 2002: Message edited by: Dan Drillich ]
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
Siva,
Can you please give the DOM parser code you've tried. I'm trying to do the "setProperty" on the parser (which is the document builder object). It says that there is no such method defined for this class. I want to see exactly what is happening with my setup.
Thanks.
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
Dan,
Can you please provide me the DOM code wherein you are setting the validation information on the parser?
Thanks.
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
Got the stuff from the tutorial; I'll check out the thing mentioned above and then jump back into the discussion
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
Please try this out -
Schema File -
================================================
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://mySpace.com" xmlns="http://mySpace.com">
<xsd:element name="Parent">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Child"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
===============================================
Changed the stuff above from xs: to xsd: for some of the elements.
XML File ---
================================================
<?xml version="1.0"?>
<test arent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mySpace.com mySchema.xsd"
xmlns:test="http://mySpace.com">
<Child/>
</test arent>
=================================================
We need to explicity place the Parent element in the namespace as the elementFormDefault="unqualified" by default. This means that the inner elements should not be namespace qualified in the instance documents.
Little relevant portion of the java code -
================================================
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
"http://mySpace.com mySchema.xsd");
parser.parse("dd.xml");
doc = parser.getDocument();
=================================================
I'm using Roger's xsv validator.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Validating XML against Schema.
 
Similar Threads
newbie question: ref or complex type ?
please help to explain this sentence in the book of MH
WSDL to service
Validating a xml doc against schema
namespace