#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xinclude.h>
#include <libxml/xmlIO.h>
static const char *result = "<list><people>a</people><people>b</people></list>";
static const char *cur = NULL;
static int rlen;
static int
sqlMatch(const char * URI) {
if ((URI != NULL) && (!strncmp(URI, "sql:", 4)))
return(1);
return(0);
}
static void *
sqlOpen(const char * URI) {
if ((URI == NULL) || (strncmp(URI, "sql:", 4)))
return(NULL);
cur = result;
rlen = strlen(result);
return((void *) cur);
}
static int
sqlClose(void * context) {
if (context == NULL) return(-1);
cur = NULL;
rlen = 0;
return(0);
}
static int
sqlRead(void * context, char * buffer, int len) {
const char *ptr = (const char *) context;
if ((context == NULL) || (buffer == NULL) || (len < 0))
return(-1);
if (len > rlen) len = rlen;
memcpy(buffer, ptr, len);
rlen -= len;
return(len);
}
const char *include = "<?xml version='1.0'?>\n\
<document xmlns:xi=\"http://www.w3.org/2003/XInclude\">\n\
<p>List of people:</p>\n\
<xi:include href=\"sql:select_name_from_people\"/>\n\
</document>\n";
int main(void) {
xmlDocPtr doc;
LIBXML_TEST_VERSION
if (xmlRegisterInputCallbacks(sqlMatch, sqlOpen, sqlRead, sqlClose) < 0) {
fprintf(stderr, "failed to register SQL handler\n");
exit(1);
}
doc = xmlReadMemory(include, strlen(include), "include.xml", NULL, 0);
if (doc == NULL) {
fprintf(stderr, "failed to parse the including file\n");
exit(1);
}
if (xmlXIncludeProcess(doc) <= 0) {
fprintf(stderr, "XInclude processing failed\n");
exit(1);
}
xmlDocDump(stdout, doc);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();
return(0);
}