Hi,
I am working on patch adding xmlvalidate() functionality. LibXML 2.7.7 improved DTD, XSD, Relax-NG validation, so using
that.I have idea of creating system table for holding DTDs, XSDs, Relax-NGs (similar as on ORACLE).
Is that good idea? If so, how to implement that table? pg_attribute and pg_class had changed guite from PostgresSQL
8.0.
Including code, work on progress.
bool
validate_xml_with_xsd(const char* xsd, const char* xml)
{
bool result = false;
xmlSchemaPtr schema = NULL; // pointer to schema
xmlLineNumbersDefault(1); // set line numbering in xml if any errors occurated
//// create xsd
xmlSchemaParserCtxtPtr ctxt;
ctxt = xmlSchemaNewMemParserCtxt((const char*)xsd, strlen(xsd));
xmlSchemaSetParserErrors(ctxt,
(xmlSchemaValidityErrorFunc) fprintf,
(xmlSchemaValidityWarningFunc) fprintf,
stderr);
schema = xmlSchemaParse(ctxt);
xmlSchemaFreeParserCtxt(ctxt);
if (schema == NULL)
{
elog(ERROR, "ERROR with DTD");
}
/// crate XML
xmlDocPtr doc;
doc = xmlReadDoc((char *)xml ,"http://www.w3.org/2001/XMLSchema",NULL,0);
if (doc == NULL)
{
elog(ERROR, "nepodarilo se nacist xml soubor ze vstupu");
} else
{
xmlSchemaValidCtxtPtr ctxt;
int ret;
ctxt = xmlSchemaNewValidCtxt(schema);
xmlSchemaSetValidErrors(ctxt,
(xmlSchemaValidityErrorFunc) fprintf,
(xmlSchemaValidityWarningFunc) fprintf,
stderr);
ret = xmlSchemaValidateDoc(ctxt, doc);
if (ret == 0)
{
result = true;
elog(WARNING, "validation SUCCED");
} else
if (ret > 0) {
result = false;
elog(WARNING, "not validated");
} else
{
result = false;
elog(WARNING, "validation failed with internal error");
}
xmlSchemaFreeValidCtxt(ctxt);
xmlFreeDoc(doc);
}
if (schema != NULL)
{ // free
xmlSchemaFree(schema);
}
return result;
}