I want to validate an XML document against an XSD schema. I'm using libxmljs 0.33 with NodeJS 18. I keep receiving this error:
Error: Invalid XSD schema
at createPDF (/root/workspace/app/apps/docapi/src/modules/generator/src/de/fwht-recovery-dip/index.ts:20:24)
at Object.<anonymous> (/root/workspace/app/apps/docapi/src/modules/generator/test/de.fwht-recovery-dip.test.ts:6:31)
at Promise.then.completed (/root/workspace/app/node_modules/jest-circus/build/utils.js:298:28)
at new Promise (<anonymous>)
at callAsyncCircusFn (/root/workspace/app/node_modules/jest-circus/build/utils.js:231:10)
at _callCircusTest (/root/workspace/app/node_modules/jest-circus/build/run.js:316:40)
at _runTest (/root/workspace/app/node_modules/jest-circus/build/run.js:252:3)
at _runTestsForDescribeBlock (/root/workspace/app/node_modules/jest-circus/build/run.js:126:9)
at _runTestsForDescribeBlock (/root/workspace/app/node_modules/jest-circus/build/run.js:121:9)
at run (/root/workspace/app/node_modules/jest-circus/build/run.js:71:3)
at runAndTransformResultsToJestFormat (/root/workspace/app/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
at jestAdapter (/root/workspace/app/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
at runTestInternal (/root/workspace/app/node_modules/jest-runner/build/runTest.js:367:16)
at runTest (/root/workspace/app/node_modules/jest-runner/build/runTest.js:444:34)
This is the code I'm running to receive this error:
const schemasRoot = path.join(__dirname, 'schemas');
const xsdSchema = fs.readFileSync(path.join(schemasRoot, 'dip_flattened.xsd'), 'utf-8');
const xmlDocument = fs.readFileSync(path.join(schemasRoot, 'schema-test.xml'), 'utf-8');
const schema = libxml.parseXmlString(xsdSchema);
const document = libxml.parseXmlString(xmlDocument);
console.log(schema.validate(document));
I tried using the same XSD in Oxygen XML and it works fine.
I looked up the error and came across this issue. It mentioned issues regarding importing/including other schemas. I eliminated that problem completely by flattening the XSD files into one giant file. So it couldn't be that.
This Gist contains the XML and XSD schema. Thanks
The mistake in my code is quite dumb and trivial, you should use the call validate
method on the XML you want to validate rather than on the schema, the schema should be the parameter.
Sorry for the trouble.
It should be this way console.log(schema.validate(document));