Can't duplicate Oxygen's validation using Xerces-J 2.7.0
Posted: Tue Jul 12, 2005 10:06 pm
Hi,
With some help on another online forum, I have modified the SPML schema so I can validate an SPML message against the schema. To do this, I had to specify a namespace in all the <xsd:any> elements like this
<xsd:any namespace="##local" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
I also had to relax a regular expression in the DSML standard which the parser didn't appear to like.
Here are the schemas I started with:
http://www.oasis-open.org/committees/do ... ma-1.0.xsd
This imports these two schemas:
http://www.oasis-open.org/committees/ds ... DSMLv2.xsd
http://www.oasis-open.org/committees/se ... ion-01.xsd
The last one imports:
http://www.w3.org/TR/xmldsig-core/xmlds ... schema.xsd
After making the two modifications described above, I can create a .xml file containing an SPML request like this SearchRequest:
<?xml version="1.0" encoding="UTF-8"?>
<spml:searchRequest xmlns:spml='urn:oasis:names:tc:SPML:1:0' xmlns:dsml='urn:oasis:names:tc:DSML:2:0:core' requestID='myId'
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:oasis:names:tc:SPML:1:0 os-pstc-spml-schema-1.0.xsd">
<operationalAttributes>
<dsml:attr name='my_param_name'>
<dsml:value>param_name_value1</dsml:value>
</dsml:attr>
<dsml:attr name='my_second_param'>
<dsml:value>value2</dsml:value>
</dsml:attr>
</operationalAttributes>
<searchBase type='urn:oasis:names:tc:SPML:1:0#GenericString'>
<id>my_friend</id>
<identifierAttributes>
<dsml:attr name='idkey'>
<dsml:value>uniquenumber1</dsml:value>
</dsml:attr>
<dsml:attr name='secondidkey'>
<dsml:value>diffuniquenumber2</dsml:value>
</dsml:attr>
</identifierAttributes>
</searchBase>
</spml:searchRequest>
This can be validated in Oxygen, against the four schemas I have stored locally.
What I would like to do now is perform this programmatically in Java using Xerces which I believe Oxygen is based on. I wrote this program:
package org.samwise.code;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.DOMParser;
public class DOMSchemaValidator {
private class Validator extends DefaultHandler
{
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException = exception;
}
public void warning(SAXParseException exception) throws SAXException
{
}
}
public void validateSchema(String XmlDocumentUrl)
{
try
{
DOMParser domParser = new DOMParser();
System.out.println("created object");
domParser.setFeature("http://xml.org/sax/features/namespaces", true);
domParser.setFeature("http://xml.org/sax/features/validation",true);
domParser.setFeature("http://apache.org/xml/features/validation/schema",true); domParser.setFeature("http://apache.org/xml/features/validati ... king",true);
domParser.setFeature("http://apache.org/xml/features/honour-a ... aLocations", true);
Validator handler = new Validator();
domParser.setErrorHandler(handler);
domParser.parse(XmlDocumentUrl);
if(handler.validationError==true)
{
handler.saxParseException.printStackTrace();
}
else
System.out.println("XML Document is valid");
System.out.println("done the parse");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main (String args[])
{
System.out.println("Validating XML");
String XmlDocumentUrl = "SPMLXMLSchema/testspml.xml";
DOMSchemaValidator validator = new DOMSchemaValidator();
validator.validateSchema(XmlDocumentUrl);
System.exit(0);
}
}
However, using the Xerces-J 2.7.0 binaries, I keep getting this error message:
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'ds:Signature' to a(n) 'element declaration' component.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.traverseLocalElements(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.samwise.code.DOMSchemaValidator.validateSchema(DOMSchemaValidator.java:75)
at org.samwise.code.DOMSchemaValidator.main(DOMSchemaValidator.java:100)
Which indicates it cannot load the last XML Schema in the chain: xmldsig-core-schema.xsd.
To be clear, I have downloaded all four schemas and stored them locally, dropping the http:// parts of the schemaLocation in the relevant namespace import statements.
Any suggestions? Again, the validation works fine within Oxygen - so I assume I am misusing the DOM parser in my java code above.
Many thanks for any response,
Sam.
With some help on another online forum, I have modified the SPML schema so I can validate an SPML message against the schema. To do this, I had to specify a namespace in all the <xsd:any> elements like this
<xsd:any namespace="##local" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
I also had to relax a regular expression in the DSML standard which the parser didn't appear to like.
Here are the schemas I started with:
http://www.oasis-open.org/committees/do ... ma-1.0.xsd
This imports these two schemas:
http://www.oasis-open.org/committees/ds ... DSMLv2.xsd
http://www.oasis-open.org/committees/se ... ion-01.xsd
The last one imports:
http://www.w3.org/TR/xmldsig-core/xmlds ... schema.xsd
After making the two modifications described above, I can create a .xml file containing an SPML request like this SearchRequest:
<?xml version="1.0" encoding="UTF-8"?>
<spml:searchRequest xmlns:spml='urn:oasis:names:tc:SPML:1:0' xmlns:dsml='urn:oasis:names:tc:DSML:2:0:core' requestID='myId'
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:oasis:names:tc:SPML:1:0 os-pstc-spml-schema-1.0.xsd">
<operationalAttributes>
<dsml:attr name='my_param_name'>
<dsml:value>param_name_value1</dsml:value>
</dsml:attr>
<dsml:attr name='my_second_param'>
<dsml:value>value2</dsml:value>
</dsml:attr>
</operationalAttributes>
<searchBase type='urn:oasis:names:tc:SPML:1:0#GenericString'>
<id>my_friend</id>
<identifierAttributes>
<dsml:attr name='idkey'>
<dsml:value>uniquenumber1</dsml:value>
</dsml:attr>
<dsml:attr name='secondidkey'>
<dsml:value>diffuniquenumber2</dsml:value>
</dsml:attr>
</identifierAttributes>
</searchBase>
</spml:searchRequest>
This can be validated in Oxygen, against the four schemas I have stored locally.
What I would like to do now is perform this programmatically in Java using Xerces which I believe Oxygen is based on. I wrote this program:
package org.samwise.code;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.DOMParser;
public class DOMSchemaValidator {
private class Validator extends DefaultHandler
{
public boolean validationError = false;
public SAXParseException saxParseException = null;
public void error(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException = exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException = exception;
}
public void warning(SAXParseException exception) throws SAXException
{
}
}
public void validateSchema(String XmlDocumentUrl)
{
try
{
DOMParser domParser = new DOMParser();
System.out.println("created object");
domParser.setFeature("http://xml.org/sax/features/namespaces", true);
domParser.setFeature("http://xml.org/sax/features/validation",true);
domParser.setFeature("http://apache.org/xml/features/validation/schema",true); domParser.setFeature("http://apache.org/xml/features/validati ... king",true);
domParser.setFeature("http://apache.org/xml/features/honour-a ... aLocations", true);
Validator handler = new Validator();
domParser.setErrorHandler(handler);
domParser.parse(XmlDocumentUrl);
if(handler.validationError==true)
{
handler.saxParseException.printStackTrace();
}
else
System.out.println("XML Document is valid");
System.out.println("done the parse");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main (String args[])
{
System.out.println("Validating XML");
String XmlDocumentUrl = "SPMLXMLSchema/testspml.xml";
DOMSchemaValidator validator = new DOMSchemaValidator();
validator.validateSchema(XmlDocumentUrl);
System.exit(0);
}
}
However, using the Xerces-J 2.7.0 binaries, I keep getting this error message:
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'ds:Signature' to a(n) 'element declaration' component.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.traverseLocalElements(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.samwise.code.DOMSchemaValidator.validateSchema(DOMSchemaValidator.java:75)
at org.samwise.code.DOMSchemaValidator.main(DOMSchemaValidator.java:100)
Which indicates it cannot load the last XML Schema in the chain: xmldsig-core-schema.xsd.
To be clear, I have downloaded all four schemas and stored them locally, dropping the http:// parts of the schemaLocation in the relevant namespace import statements.
Any suggestions? Again, the validation works fine within Oxygen - so I assume I am misusing the DOM parser in my java code above.
Many thanks for any response,
Sam.