Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.xmlsoft.org/html/libxml-xmlIO.html#xmlRegisterInputCallbacks" rel="nofollow">xmlRegisterInputCallbacks</a> is what you're probably looking for.</p> <p>The advantage, they let you construct some kind of a virtual I/O layer. The disadvantage, inputcallbacks are set globally (there is however <a href="http://www.xmlsoft.org/html/libxml-xmlIO.html#xmlPopInputCallbacks" rel="nofollow">xmlPopInputCallbacks</a> and <a href="http://www.xmlsoft.org/html/libxml-xmlIO.html#xmlCleanupInputCallbacks" rel="nofollow">xmlCleanupInputCallbacks</a>).</p> <p>The code below (built upon code from <a href="http://knol2share.blogspot.be/2009/05/validate-xml-against-xsd-in-c.html" rel="nofollow">http://knol2share.blogspot.be</a>) demonstrates the use of <a href="http://www.xmlsoft.org/html/libxml-xmlIO.html#xmlCleanupInputCallbacks" rel="nofollow">xmlRegisterInputCallbacks</a>.</p> <p>All xml and xsd files are loaded from the file system, except when the URI contains "DataTypes.xsd" the schema is fetched from a string. (since schemaLocation is only a hint one could for example prefix schema's </p> <p>"<strong>test.xsd</strong>": the main xml schema (please ignore the reference to peacelane.org, namespaces are from a hobby project, just occurred to me now www.peacelane.org might exist, apparently it does...)</p> <pre class="lang-xml prettyprint-override"><code>&lt;schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://peacelane.org/ApplianceType/config/45/LIGHTING1/ARC" xmlns:dt="http://peacelane.org/ApplianceType/config/45/DataTypes" targetNamespace="http://peacelane.org/ApplianceType/config/45/LIGHTING1/ARC" elementFormDefault="qualified"&gt; &lt;import namespace="http://peacelane.org/ApplianceType/config/45/DataTypes" schemaLocation="DataTypes.xsd" /&gt; &lt;complexType name="ConfigType"&gt; &lt;sequence&gt; &lt;element name="housecode" type="dt:char" /&gt; &lt;/sequence&gt; &lt;/complexType&gt; &lt;element name="Config" type="tns:ConfigType"/&gt; &lt;/schema&gt; </code></pre> <p>"<strong>test.xml</strong>": a test xml to validate</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt; &lt;Config xmlns="http://peacelane.org/ApplianceType/config/45/LIGHTING1/ARC"&gt; &lt;housecode&gt;A&lt;/housecode&gt; &lt;/Config&gt; </code></pre> <p>"<strong>main.c</strong>": the actual code (update the paths for "XMLFileName" and "XSDFileName")</p> <pre class="lang-c prettyprint-override"><code>#define LIBXML_SCHEMAS_ENABLED #include &lt;libxml/xmlschemastypes.h&gt; #include &lt;stdio.h&gt; static const char *databaseSchema = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;" " &lt;schema elementFormDefault=\"qualified\" xmlns:tns=\"http://peacelane.org/ApplianceType/config/45/DataTypes\" targetNamespace=\"http://peacelane.org/ApplianceType/config/45/DataTypes\" xmlns=\"http://www.w3.org/2001/XMLSchema\"&gt;" " &lt;simpleType name=\"char\"&gt;" " &lt;restriction base=\"string\"&gt;" " &lt;length value=\"1\" /&gt;" " &lt;/restriction&gt;" " &lt;/simpleType&gt;" " &lt;/schema&gt;"; //----------------------------------------------------------------------------- //-- SQL Callbacks (~ simulated db actions) //----------------------------------------------------------------------------- static void* sqlOpen(const char * URI) { return ((void *) databaseSchema ); } static int sqlClose(void * context) { return (0); } static int sqlRead(void * context, char * buffer, int len) { const char* result= (const char *) context; int rlen = strlen(result); memcpy(buffer, result, rlen); return rlen +1; } static int sqlMatch(const char * URI) { if ((URI != NULL )&amp;&amp; (strstr(URI, "DataTypes.xsd") != NULL) )return 1; return 0; } //----------------------------------------------------------------------------- //-- File callbacks //----------------------------------------------------------------------------- static void* fileOpen(const char * URI) { if (URI == NULL ) return (NULL ); FILE* fh = fopen(URI, "rt"); return ((void *) fh); } static int fileClose(void * context) { FILE* fh = (FILE*) context; if (fh != NULL ) fclose(fh); return (0); } static int fileRead(void * context, char * buffer, int len) { FILE* fh = (FILE*) context; fseek(fh, 0L, SEEK_END); long flen = ftell(fh); rewind(fh); if (buffer != NULL ) fread(buffer, flen, 1, fh); return flen + 1; } static int fileMatch(const char * URI) { if ((URI != NULL )) if (strstr(URI, "DataTypes.xsd") == NULL ) { return (1); } return (0); } //----------------------------------------------------------------------------- //-- Main //----------------------------------------------------------------------------- int main(int argc, char *argv[]) { xmlDocPtr doc; xmlSchemaPtr schema = NULL; xmlSchemaParserCtxtPtr ctxt; char *XMLFileName = "/home/dogguts/Projects/libxml2tests/xsdparse/Debug/test.xml"; char *XSDFileName = "/home/dogguts/Projects/libxml2tests/xsdparse/Debug/test.xsd"; xmlLineNumbersDefault(1); if (xmlRegisterInputCallbacks(fileMatch, fileOpen, fileRead, fileClose) &lt; 0) { fprintf(stderr, "failed to register File handler\n"); exit(1); } if (xmlRegisterInputCallbacks(sqlMatch, sqlOpen, sqlRead, sqlClose) &lt; 0) { fprintf(stderr, "failed to register SQL handler\n"); exit(1); } ctxt = xmlSchemaNewParserCtxt(XSDFileName); xmlSchemaSetParserErrors(ctxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr); schema = xmlSchemaParse(ctxt); xmlSchemaFreeParserCtxt(ctxt); xmlSchemaDump(stdout, schema); doc = xmlReadFile(XMLFileName, NULL, 0); if (doc == NULL ) { fprintf(stderr, "Could not parse %s\n", XMLFileName); } else { xmlSchemaValidCtxtPtr ctxt; int ret; ctxt = xmlSchemaNewValidCtxt(schema); xmlSchemaSetValidErrors(ctxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr); ret = xmlSchemaValidateDoc(ctxt, doc); if (ret == 0) { printf("%s validates\n", XMLFileName); } else if (ret &gt; 0) { printf("%s fails to validate\n", XMLFileName); } else { printf("%s validation generated an internal error\n", XMLFileName); } xmlSchemaFreeValidCtxt(ctxt); xmlFreeDoc(doc); } if (schema != NULL ) xmlSchemaFree(schema); xmlSchemaCleanupTypes(); xmlCleanupParser(); xmlMemoryDump(); return (0); } </code></pre> <hr> <p><em>Notice that, for brevity, the above code doesnt' perform any checks wether (file, memory,...) operations succeeded.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload