123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- #include <stdio.h>
- #include <string.h>
- #include <libxml/parser.h>
- #include <libxml/tree.h>
- #include <libxml/xinclude.h>
- #include <libxml/xmlIO.h>
- #ifdef LIBXML_XINCLUDE_ENABLED
- 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);
- }
- #ifdef LIBXML_OUTPUT_ENABLED
-
- xmlDocDump(stdout, doc);
- #endif
-
- xmlFreeDoc(doc);
-
- xmlCleanupParser();
-
- xmlMemoryDump();
- return(0);
- }
- #else
- int main(void) {
- fprintf(stderr, "XInclude support not compiled in\n");
- exit(1);
- }
- #endif
|