• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Validating XML against Schema.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Siva shanmugam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried the above code.But the response is the same.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siva,
Here is a namespaceless working version.


Do you want it with namespaces?
Cheers,
Dan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With namespaces -

[ September 26, 2002: Message edited by: Dan Drillich ]
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
Can you please provide me the DOM code wherein you are setting the validation information on the parser?
Thanks.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic