Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

validate xml dom with schema

 
Harlan Vossos
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to validate a xml document already loaded into a dom against a xml schema. How do you do this.
ps. I am using xerces xml parser.
 
Johannes Hartman
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harlan,

I am no expert at this, but as far as my knowledge go I do not believe that is available at this moment. The schema recommendation was not officially finilized until May (If I recall correctly) of this year, and since schema went through so much change before the finilization, most tools only provide partial support for schemas. My best suggestion is "hang on" - I'm sure it will be available sometime soon here.
Otherwise, if you have to get on with it, maybe you could use DTD's for the time being? If this is not an option, how critical is performance having to go back and forth between the DOM and an XML file/stream?
 
Johannes Hartman
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harlan,
Check out these schema validators - I do not know whether any of them are actual in-memory validators, but if not, maybe you could get a workaround....
Xerces Java Parser:
xml.apache.org/xerces-j
IBM XML Schema Quality Checker: www.alphaworks.ibm.com/tech/xmlsqc
XML Scema Validator (XSV): www.ltg.ed.ac.uk/~ht/xsv-status.html
Let me know if you solved your problem....
Cheers!
Johannes
 
Manuel Palacio
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
What's the trick to validate an XML doc against a schema with Xerces? I can't make it work. Not even with the example program SAXCount with validation turned on.
The docs I'm using are the ones in the example that comes with xerces: personal-schema.xml and personal.xsd
And all I get is :
[Error] personal-schema.xml:4:48: Element type "personnel" must be declared.
[Error] personal-schema.xml:4:48: Attribute "xmlns:xsi" must be declared for element type "personnel".
...etc. Sounds like it can't find the schema or something but it's in the same directory.
I would really appreciate if you could tell me what I'm doing wrong! Thanks.
/Manuel
 
Caroline Iux
Ranch Hand
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post these two files here?
 
Manuel Palacio
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------------------------------------------
Sure-- The XML doc
----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<personnel xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation="personal.xsd">
<person id="Big.Boss" >
<name><family>Boss</family> <given>Big</given></name>
<email>chief@foo.com</email>
<link subordinates="one.worker two.worker three.worker four.worker five.worker"/>
</person>
<person id="one.worker">
<name><family>Worker</family> <given>One</given></name>
<email>one@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="two.worker">
<name><family>Worker</family> <given>Two</given></name>
<email>two@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="three.worker">
<name><family>Worker</family> <given>Three</given></name>
<email>three@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="four.worker">
<name><family>Worker</family> <given>Four</given></name>
<email>four@foo.com</email>
<link manager="Big.Boss"/>
</person>
<person id="five.worker">
<name><family>Worker</family> <given>Five</given></name>
<email>five@foo.com</email>
<link manager="Big.Boss"/>
</person>
</personnel>
---------------------------------------------------
The XSD
---------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
<element name="personnel">
<complexType>
<sequence>
<element ref="person" minOccurs='1' maxOccurs='unbounded'/>
</sequence>
</complexType>
<unique name="unique1">
<selector xpath="person"/>
<field xpath="@name"/>
</unique>
<key name='empid'>
<selector xpath="person"/>
<field xpath="@id"/>
</key>
<!--
<keyref name="keyref1" refer='empid'>
<selector xpath="person"/>
<field xpath="link/@manager"/>
</keyref>
-->
</element>
<element name="person">
<complexType>
<sequence>
<element ref="name"/>
<element ref="email" minOccurs='0' maxOccurs='unbounded'/>
<element ref="url" minOccurs='0' maxOccurs='unbounded'/>
<element ref="link" minOccurs='0' maxOccurs='1'/>
</sequence>
<attribute name="id" type="ID" use='required'/>
<attribute name="note" type="string"/>
<attribute name="contr" default="false">
<simpleType>
<restriction base = "string">
<enumeration value="true"/>
<enumeration value="false"/>
</restriction>
</simpleType>
</attribute>
<attribute name="salary" type="integer"/>
</complexType>
</element>
<element name="name">
<complexType>
<all>
<element ref="family"/>
<element ref="given"/>
</all>
</complexType>
</element>
<element name="family" type='string'/>
<element name="given" type='string'/>
<element name="email" type='string'/>
<element name="url">
<complexType>
<attribute name="href" type="string" default="http://"/>
</complexType>
</element>
<element name="link">
<complexType>
<attribute name="manager" type="IDREF"/>
<attribute name="subordinates" type="IDREFS"/>
</complexType>
</element>
<notation name='gif' public='-//APP/Photoshop/4.0' system='photoshop.exe'/>
</schema>
 
Caroline Iux
Ranch Hand
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to write a simple class to parse your file.
**************************************************
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.EntityResolver;
import org.apache.xerces.framework.XMLParser;
import org.apache.xerces.parsers.SAXParser;
import java.io.*;
/**
* A sample SAX2 counter. This sample program illustrates how to
* register a SAX2 ContentHandler and receive the callbacks in
* order to print information about the document.
*
* @version $Id: SAX2Count.java,v 1.6 2001/05/17 21:04:17 neilg Exp $
*/
public class SAXTest {
public static void main( String args[] ) {
try {
XMLParser parser = new SAXParser();
//set entity resolver, so always use the registered schema
parser.setEntityResolver( new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
// use the default behaviour
return new InputSource( "personal.xsd" );
}});
//set error handler and validation feature to true
parser.setErrorHandler(new DefaultHandler() {
public void error(SAXParseException e) throws SAXParseException {throw e; }
public void fatalError(SAXParseException e) throws SAXParseException {throw e; } public void warning(SAXParseException e) throws SAXParseException {
System.out.println( e.toString() );
}});
parser.setFeature( "http://xml.org/sax/features/validation", true);
long before = System.currentTimeMillis();
parser.parse( "personal.xml" );
long after = System.currentTimeMillis();
System.out.println(after - before);
} catch (org.xml.sax.SAXParseException spe) {
spe.printStackTrace(System.err);
} catch (org.xml.sax.SAXException se) {
if (se.getException() != null)
se.getException().printStackTrace(System.err);
else
se.printStackTrace(System.err);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
********************************************************
The message I got is:
********************************************************
org.xml.sax.SAXParseException: The content of element type "name" must match "all(family,given)".
at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1196) at org.apache.xerces.validators.common.XMLValidator.callEndElement(XMLValidator.java, Compiled Code) at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java, Compiled Code) at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java, Compiled Code)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1081)
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1122)
at SAXTest.main(SAXTest.java:98)
************************************************************
So it seems like it's doing the job. I am using xerces 1.4.1. Try the code above.
 
sateesh arumbaka
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Downloaded xerces 1.4.4 and am trying to run SAXCount and DOMCount with validation "on"....
its throwing some errors... like
"[Error] personal-schema.xml:3:50: Element type "personnel" must be declared."
what other settings,,, if any do I need to make/ take care of..
basically I want to run an XML file across a schema and see if the XML file is valid or not...
Thanks in advance..
Sateesh
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic