I am trying to validate a generated XML Document using libxmljs2 (seems to be the best tool for the job in Typescript), butdocument.validate() keeps throwing 'Invalid XML Schema' errors at me, without further explanation.
When an XML does not conform to a XSD, it gives detailed errors, but it says nothing about the XSD when it cannot handle it.
I tried to go 1 level up and asked libxmljs2 to validate my XSD (which is fine, according to XMLSpy at least) against the XML Schema Definition, I'm getting the same error. Maybe it was a silly test, and libxmljs2 cannot handle the XML Schema Definition for some reason.
But the bottom line is : I can't seem to have any information on what's going wrong when trying to validate my XML with this lib.
const xsdPath = path.join(__dirname, '/xsd/');
const xsd = parseXmlString(fs.readFileSync(path.join(xsdPath, 'my.xsd'), 'utf8'), { baseUrl: xsdPath });
try {
const valid = xmlDocument.validate(xsd); // throws
if (!valid) {
console.error(`XML is not valid against the XSD: ${JSON.stringify(this.doc.validationErrors)}`)
}
}
catch (e) {
console.error('I end up here. validate() throws an error about "invalid XML Schema"');
}
Please note that:
- xsdPath is correct, contains my .xsd files, and does end with a '/'
- I use the baseUrl option to circumvent the file loading problem. I tried changing the current working dir too, same issue
You say
Actually the schema-for-schema-documents is very far from simple, because it defines things like the primitive types (for example it declares
<xs:simpleType name="hexBinary">) which is something that requires special privileges: a normal schema isn't allowed to change these definitions. So some schema processors cannot handle this "metaschema", or need special configuration options to allow it.But the question is, why are you even doing this? The way to check that your schema is valid is to throw it at a schema processor, not to validate it against the schema-for-schema-documents.