Fejl i xsd schema validering af xml
Hej Eksperter.Jeg har et problem. Jeg har downloadet et ubl xsd schema som fungerer perfekt når jeg bruger det fra xmlspy.
Hvis jeg derimod benytter det via det java kode jeg har lavet så fungerer det ikke. Jeg får følgende fejl
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'cac:DocumentReferenceType' to a(n) 'type definition' component.
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'udt:DateType' to a(n) 'type definition' component.
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'cbc:CopyIndicator' to a(n) 'element declaration' component.
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'res:AcknowledgementResponseCodeType' to a(n) 'type definition' component.
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'cur:CurrencyCodeType' to a(n) 'type definition' component.
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'cbc:IssueDate'. One of '{"urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":SellersID, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":GUID, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":AcknowledgementResponseCode, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":TransactionCurrencyCode, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":PricingCurrencyCode, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":EarliestDate, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":ValidityDurationMeasure, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":TotalPackagesQuantity, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":LineItemCountNumeric, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":ContractDocumentReference, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":QuoteDocumentReference, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":AdditionalDocumentReference, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":OriginatorParty, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":FreightForwarderParty, "urn:oasis:names:specification:ubl:schema:xsd:Order-1.0":DestinationCountry}' is expected.
dk.edbgruppen.genericdocumentprocess.exceptionhandling.GenericDocumentException
at dk.edbgruppen.junits.xsdtests.TestUblXsd.parse(TestUblXsd.java:143)
at dk.edbgruppen.junits.xsdtests.TestUblXsd.testUblOrderXsd(TestUblXsd.java:72)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:166)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:131)
at junit.framework.TestSuite.runTest(TestSuite.java:173)
at junit.framework.TestSuite.run(TestSuite.java:168)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Xsden kan findes her: http://docs.oasis-open.org/ubl/cd-UBL-1.0/xsd/maindoc/UBL-Order-1.0.xsd
Jeg har fundet frem til følgende link der forklarer problemet tror jeg.
http://forum.java.sun.com/thread.jspa?threadID=554680&messageID=2719072
Men efter at have læst det kan jeg stadig ikke få det til at virke. Er der nogle bud på hvad jeg kan have gjort forkert?
Java metoden jeg bruger ser således ud.
public Document parse(byte[] documentAsBytes, InputStream schema) throws Exception {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
if (schema != null) {
String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
factory.setNamespaceAware(true);
factory.setValidating(true);
try {
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
} catch (IllegalArgumentException x) {
}
factory.setAttribute(JAXP_SCHEMA_SOURCE, schema);
}
DocumentBuilder builder = factory.newDocumentBuilder();
ValidationHandler errorHandler = new ValidationHandler() {
public void warning(SAXParseException arg0) throws SAXException {
super.warning(arg0);
}
public void error(SAXParseException arg0) {
super.error(arg0);
}
public void fatalError(SAXParseException arg0) {
super.fatalError(arg0);
}
public String getError() {
return super.getError();
}
public String getFatalError() {
return super.getFatalError();
}
};
builder.setErrorHandler(errorHandler);
Document d = builder.parse(new BufferedInputStream(new ByteArrayInputStream(documentAsBytes)));
if (d == null) {
throw new IllegalStateException("No document created");
}
String error = errorHandler.getError();
System.out.println(error);
String fatalError = errorHandler.getFatalError();
System.out.println(fatalError);
if (error.length() > 0) {
throw new IllegalStateException(error);
} else if (fatalError.length() > 0) {
throw new IllegalStateException(fatalError);
}
return d;
} catch (Throwable ex) {
List list = new LinkedList();
//list.add(schemaPath);
throw new GenericDocumentException(1, 10, 2, ex, list);
}
}