2 * xinclude.c : Code to implement XInclude processing
4 * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
5 * http://www.w3.org/TR/2003/WD-xinclude-20031110
7 * See Copyright for the status of this software.
16 #include <libxml/xmlmemory.h>
17 #include <libxml/tree.h>
18 #include <libxml/parser.h>
19 #include <libxml/uri.h>
20 #include <libxml/xpointer.h>
21 #include <libxml/parserInternals.h>
22 #include <libxml/xmlerror.h>
23 #include <libxml/encoding.h>
24 #include <libxml/globals.h>
26 #ifdef LIBXML_XINCLUDE_ENABLED
27 #include <libxml/xinclude.h>
30 #define XINCLUDE_MAX_DEPTH 40
32 /* #define DEBUG_XINCLUDE */
34 #ifdef LIBXML_DEBUG_ENABLED
35 #include <libxml/debugXML.h>
39 /************************************************************************
41 * XInclude context handling *
43 ************************************************************************/
48 typedef xmlChar *xmlURL;
50 typedef struct _xmlXIncludeRef xmlXIncludeRef;
51 typedef xmlXIncludeRef *xmlXIncludeRefPtr;
52 struct _xmlXIncludeRef {
53 xmlChar *URI; /* the fully resolved resource URL */
54 xmlChar *fragment; /* the fragment in the URI */
55 xmlDocPtr doc; /* the parsed document */
56 xmlNodePtr ref; /* the node making the reference in the source */
57 xmlNodePtr inc; /* the included copy */
58 int xml; /* xml or txt */
59 int count; /* how many refs use that specific doc */
60 xmlXPathObjectPtr xptr; /* the xpointer if needed */
61 int emptyFb; /* flag to show fallback empty */
64 struct _xmlXIncludeCtxt {
65 xmlDocPtr doc; /* the source document */
66 int incBase; /* the first include for this document */
67 int incNr; /* number of includes */
68 int incMax; /* size of includes tab */
69 xmlXIncludeRefPtr *incTab; /* array of included references */
71 int txtNr; /* number of unparsed documents */
72 int txtMax; /* size of unparsed documents tab */
73 xmlNodePtr *txtTab; /* array of unparsed text nodes */
74 xmlURL *txturlTab; /* array of unparsed text URLs */
76 xmlChar * url; /* the current URL processed */
77 int urlNr; /* number of URLs stacked */
78 int urlMax; /* size of URL stack */
79 xmlChar * *urlTab; /* URL stack */
81 int nbErrors; /* the number of errors detected */
82 int legacy; /* using XINCLUDE_OLD_NS */
83 int parseFlags; /* the flags used for parsing XML documents */
84 xmlChar * base; /* the current xml:base */
86 void *_private; /* application data */
90 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree);
93 /************************************************************************
95 * XInclude error handler *
97 ************************************************************************/
100 * xmlXIncludeErrMemory:
101 * @extra: extra information
103 * Handle an out of memory condition
106 xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
111 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
112 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
113 extra, NULL, NULL, 0, 0,
114 "Memory allocation failed : %s\n", extra);
119 * @ctxt: the XInclude context
120 * @node: the context node
121 * @msg: the error message
122 * @extra: extra information
124 * Handle an XInclude error
127 xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
128 const char *msg, const xmlChar *extra)
132 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
133 error, XML_ERR_ERROR, NULL, 0,
134 (const char *) extra, NULL, NULL, 0, 0,
135 msg, (const char *) extra);
141 * @ctxt: the XInclude context
142 * @node: the context node
143 * @msg: the error message
144 * @extra: extra information
146 * Emit an XInclude warning.
149 xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
150 const char *msg, const xmlChar *extra)
152 __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
153 error, XML_ERR_WARNING, NULL, 0,
154 (const char *) extra, NULL, NULL, 0, 0,
155 msg, (const char *) extra);
160 * xmlXIncludeGetProp:
161 * @ctxt: the XInclude context
163 * @name: the attribute name
165 * Get an XInclude attribute
167 * Returns the value (to be freed) or NULL if not found
170 xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
171 const xmlChar *name) {
174 ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
177 if (ctxt->legacy != 0) {
178 ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
182 ret = xmlGetProp(cur, name);
186 * xmlXIncludeFreeRef:
187 * @ref: the XInclude reference
189 * Free an XInclude reference
192 xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
195 #ifdef DEBUG_XINCLUDE
196 xmlGenericError(xmlGenericErrorContext, "Freeing ref\n");
198 if (ref->doc != NULL) {
199 #ifdef DEBUG_XINCLUDE
200 xmlGenericError(xmlGenericErrorContext, "Freeing doc %s\n", ref->URI);
202 xmlFreeDoc(ref->doc);
204 if (ref->URI != NULL)
206 if (ref->fragment != NULL)
207 xmlFree(ref->fragment);
208 if (ref->xptr != NULL)
209 xmlXPathFreeObject(ref->xptr);
215 * @ctxt: the XInclude context
216 * @URI: the resource URI
218 * Creates a new reference within an XInclude context
220 * Returns the new set
222 static xmlXIncludeRefPtr
223 xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
225 xmlXIncludeRefPtr ret;
227 #ifdef DEBUG_XINCLUDE
228 xmlGenericError(xmlGenericErrorContext, "New ref %s\n", URI);
230 ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
232 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
235 memset(ret, 0, sizeof(xmlXIncludeRef));
239 ret->URI = xmlStrdup(URI);
240 ret->fragment = NULL;
246 if (ctxt->incMax == 0) {
248 ctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(ctxt->incMax *
249 sizeof(ctxt->incTab[0]));
250 if (ctxt->incTab == NULL) {
251 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
252 xmlXIncludeFreeRef(ret);
256 if (ctxt->incNr >= ctxt->incMax) {
258 ctxt->incTab = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
259 ctxt->incMax * sizeof(ctxt->incTab[0]));
260 if (ctxt->incTab == NULL) {
261 xmlXIncludeErrMemory(ctxt, ref, "growing XInclude context");
262 xmlXIncludeFreeRef(ret);
266 ctxt->incTab[ctxt->incNr++] = ret;
271 * xmlXIncludeNewContext:
272 * @doc: an XML Document
274 * Creates a new XInclude context
276 * Returns the new set
279 xmlXIncludeNewContext(xmlDocPtr doc) {
280 xmlXIncludeCtxtPtr ret;
282 #ifdef DEBUG_XINCLUDE
283 xmlGenericError(xmlGenericErrorContext, "New context\n");
287 ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
289 xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
290 "creating XInclude context");
293 memset(ret, 0, sizeof(xmlXIncludeCtxt));
304 * xmlXIncludeURLPush:
305 * @ctxt: the parser context
308 * Pushes a new url on top of the url stack
310 * Returns -1 in case of error, the index in the stack otherwise
313 xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt,
314 const xmlChar *value)
316 if (ctxt->urlNr > XINCLUDE_MAX_DEPTH) {
317 xmlXIncludeErr(ctxt, NULL, XML_XINCLUDE_RECURSION,
318 "detected a recursion in %s\n", value);
321 if (ctxt->urlTab == NULL) {
324 ctxt->urlTab = (xmlChar * *) xmlMalloc(
325 ctxt->urlMax * sizeof(ctxt->urlTab[0]));
326 if (ctxt->urlTab == NULL) {
327 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
331 if (ctxt->urlNr >= ctxt->urlMax) {
334 (xmlChar * *) xmlRealloc(ctxt->urlTab,
336 sizeof(ctxt->urlTab[0]));
337 if (ctxt->urlTab == NULL) {
338 xmlXIncludeErrMemory(ctxt, NULL, "adding URL");
342 ctxt->url = ctxt->urlTab[ctxt->urlNr] = xmlStrdup(value);
343 return (ctxt->urlNr++);
348 * @ctxt: the parser context
350 * Pops the top URL from the URL stack
353 xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt)
357 if (ctxt->urlNr <= 0)
361 ctxt->url = ctxt->urlTab[ctxt->urlNr - 1];
364 ret = ctxt->urlTab[ctxt->urlNr];
365 ctxt->urlTab[ctxt->urlNr] = NULL;
371 * xmlXIncludeFreeContext:
372 * @ctxt: the XInclude context
374 * Free an XInclude context
377 xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
380 #ifdef DEBUG_XINCLUDE
381 xmlGenericError(xmlGenericErrorContext, "Freeing context\n");
385 while (ctxt->urlNr > 0)
386 xmlXIncludeURLPop(ctxt);
387 if (ctxt->urlTab != NULL)
388 xmlFree(ctxt->urlTab);
389 for (i = 0;i < ctxt->incNr;i++) {
390 if (ctxt->incTab[i] != NULL)
391 xmlXIncludeFreeRef(ctxt->incTab[i]);
393 if (ctxt->txturlTab != NULL) {
394 for (i = 0;i < ctxt->txtNr;i++) {
395 if (ctxt->txturlTab[i] != NULL)
396 xmlFree(ctxt->txturlTab[i]);
399 if (ctxt->incTab != NULL)
400 xmlFree(ctxt->incTab);
401 if (ctxt->txtTab != NULL)
402 xmlFree(ctxt->txtTab);
403 if (ctxt->txturlTab != NULL)
404 xmlFree(ctxt->txturlTab);
405 if (ctxt->base != NULL) {
412 * xmlXIncludeParseFile:
413 * @ctxt: the XInclude context
414 * @URL: the URL or file path
416 * parse a document for XInclude
419 xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
421 xmlParserCtxtPtr pctxt;
422 char *directory = NULL;
423 xmlParserInputPtr inputStream;
427 pctxt = xmlNewParserCtxt();
429 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
434 * pass in the application data to the parser context.
436 pctxt->_private = ctxt->_private;
439 * try to ensure that new documents included are actually
440 * built with the same dictionary as the including document.
442 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL) &&
443 (pctxt->dict != NULL)) {
444 xmlDictFree(pctxt->dict);
445 pctxt->dict = ctxt->doc->dict;
446 xmlDictReference(pctxt->dict);
449 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
451 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
452 if (inputStream == NULL) {
453 xmlFreeParserCtxt(pctxt);
457 inputPush(pctxt, inputStream);
459 if ((pctxt->directory == NULL) && (directory == NULL))
460 directory = xmlParserGetDirectory(URL);
461 if ((pctxt->directory == NULL) && (directory != NULL))
462 pctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
464 pctxt->loadsubset |= XML_DETECT_IDS;
466 xmlParseDocument(pctxt);
468 if (pctxt->wellFormed) {
473 if (pctxt->myDoc != NULL)
474 xmlFreeDoc(pctxt->myDoc);
477 xmlFreeParserCtxt(pctxt);
483 * xmlXIncludeAddNode:
484 * @ctxt: the XInclude context
487 * Add a new node to process to an XInclude context
490 xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
491 xmlXIncludeRefPtr ref;
494 xmlChar *fragment = NULL;
499 int xml = 1, i; /* default Issue 64 */
508 #ifdef DEBUG_XINCLUDE
509 xmlGenericError(xmlGenericErrorContext, "Add node\n");
512 * read the attributes
514 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
516 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
523 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
525 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
527 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
530 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
531 "invalid value %s for 'parse'\n", parse);
543 base = xmlNodeGetBase(ctxt->doc, cur);
545 URI = xmlBuildURI(href, ctxt->doc->URL);
547 URI = xmlBuildURI(href, base);
553 * Some escaping may be needed
555 escbase = xmlURIEscape(base);
556 eschref = xmlURIEscape(href);
557 URI = xmlBuildURI(eschref, escbase);
570 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
571 "failed build URL\n", NULL);
574 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
577 * Check the URL and remove any fragment identifier
579 uri = xmlParseURI((const char *)URI);
581 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
582 "invalid value URI %s\n", URI);
583 if (fragment != NULL)
589 if (uri->fragment != NULL) {
590 if (ctxt->legacy != 0) {
591 if (fragment == NULL) {
592 fragment = (xmlChar *) uri->fragment;
594 xmlFree(uri->fragment);
597 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
598 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
600 if (fragment != NULL)
606 uri->fragment = NULL;
608 URL = xmlSaveUri(uri);
612 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
613 "invalid value URI %s\n", URI);
614 if (fragment != NULL)
620 * Check the URL against the stack for recursions
622 if ((!local) && (xml == 1)) {
623 for (i = 0;i < ctxt->urlNr;i++) {
624 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
625 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
626 "detected a recursion in %s\n", URL);
632 ref = xmlXIncludeNewRef(ctxt, URL, cur);
636 ref->fragment = fragment;
645 * xmlXIncludeRecurseDoc:
646 * @ctxt: the XInclude context
647 * @doc: the new document
648 * @url: the associated URL
650 * The XInclude recursive nature is handled at this point.
653 xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
654 const xmlURL url ATTRIBUTE_UNUSED) {
655 xmlXIncludeCtxtPtr newctxt;
659 * Avoid recursion in already substitued resources
660 for (i = 0;i < ctxt->urlNr;i++) {
661 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
666 #ifdef DEBUG_XINCLUDE
667 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
670 * Handle recursion here.
673 newctxt = xmlXIncludeNewContext(doc);
674 if (newctxt != NULL) {
676 * Copy the existing document set
678 newctxt->incMax = ctxt->incMax;
679 newctxt->incNr = ctxt->incNr;
680 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
681 sizeof(newctxt->incTab[0]));
682 if (newctxt->incTab == NULL) {
683 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
690 newctxt->urlMax = ctxt->urlMax;
691 newctxt->urlNr = ctxt->urlNr;
692 newctxt->urlTab = ctxt->urlTab;
695 * Inherit the existing base
697 newctxt->base = xmlStrdup(ctxt->base);
700 * Inherit the documents already in use by other includes
702 newctxt->incBase = ctxt->incNr;
703 for (i = 0;i < ctxt->incNr;i++) {
704 newctxt->incTab[i] = ctxt->incTab[i];
705 newctxt->incTab[i]->count++; /* prevent the recursion from
709 * The new context should also inherit the Parse Flags
712 newctxt->parseFlags = ctxt->parseFlags;
713 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
714 for (i = 0;i < ctxt->incNr;i++) {
715 newctxt->incTab[i]->count--;
716 newctxt->incTab[i] = NULL;
719 /* urlTab may have been reallocated */
720 ctxt->urlTab = newctxt->urlTab;
721 ctxt->urlMax = newctxt->urlMax;
725 newctxt->urlTab = NULL;
727 xmlXIncludeFreeContext(newctxt);
729 #ifdef DEBUG_XINCLUDE
730 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
736 * @ctxt: the XInclude context
737 * @txt: the new text node
738 * @url: the associated URL
740 * Add a new txtument to the list
743 xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
744 #ifdef DEBUG_XINCLUDE
745 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
747 if (ctxt->txtMax == 0) {
749 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
750 sizeof(ctxt->txtTab[0]));
751 if (ctxt->txtTab == NULL) {
752 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
755 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
756 sizeof(ctxt->txturlTab[0]));
757 if (ctxt->txturlTab == NULL) {
758 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
762 if (ctxt->txtNr >= ctxt->txtMax) {
764 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
765 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
766 if (ctxt->txtTab == NULL) {
767 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
770 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
771 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
772 if (ctxt->txturlTab == NULL) {
773 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
777 ctxt->txtTab[ctxt->txtNr] = txt;
778 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
782 /************************************************************************
784 * Node copy with specific semantic *
786 ************************************************************************/
789 * xmlXIncludeCopyNode:
790 * @ctxt: the XInclude context
791 * @target: the document target
792 * @source: the document source
795 * Make a copy of the node while preserving the XInclude semantic
796 * of the Infoset copy
799 xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
800 xmlDocPtr source, xmlNodePtr elem) {
801 xmlNodePtr result = NULL;
803 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
806 if (elem->type == XML_DTD_NODE)
808 result = xmlDocCopyNode(elem, target, 1);
813 * xmlXIncludeCopyNodeList:
814 * @ctxt: the XInclude context
815 * @target: the document target
816 * @source: the document source
817 * @elem: the element list
819 * Make a copy of the node list while preserving the XInclude semantic
820 * of the Infoset copy
823 xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
824 xmlDocPtr source, xmlNodePtr elem) {
825 xmlNodePtr cur, res, result = NULL, last = NULL;
827 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
831 while (cur != NULL) {
832 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
834 if (result == NULL) {
848 * xmlXIncludeGetNthChild:
850 * @no: the child number
852 * Returns the @n'th element child of @cur or NULL
855 xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
860 for (i = 0;i <= no;cur = cur->next) {
863 if ((cur->type == XML_ELEMENT_NODE) ||
864 (cur->type == XML_DOCUMENT_NODE) ||
865 (cur->type == XML_HTML_DOCUMENT_NODE)) {
874 xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
876 * xmlXIncludeCopyRange:
877 * @ctxt: the XInclude context
878 * @target: the document target
879 * @source: the document source
880 * @obj: the XPointer result from the evaluation.
882 * Build a node list tree copy of the XPointer result.
884 * Returns an xmlNodePtr list or NULL.
885 * The caller has to free the node tree.
888 xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
889 xmlDocPtr source, xmlXPathObjectPtr range) {
890 /* pointers to generated nodes */
891 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
892 xmlNodePtr tmp, tmp2;
893 /* pointers to traversal nodes */
894 xmlNodePtr start, cur, end;
896 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
898 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
901 if (range->type != XPATH_RANGE)
903 start = (xmlNodePtr) range->user;
909 return(xmlDocCopyNode(start, target, 1));
912 index1 = range->index;
913 index2 = range->index2;
915 * level is depth of the current node under consideration
916 * list is the pointer to the root of the output tree
917 * listParent is a pointer to the parent of output tree (within
918 the included file) in case we need to add another level
919 * last is a pointer to the last node added to the output tree
920 * lastLevel is the depth of last (relative to the root)
922 while (cur != NULL) {
924 * Check if our output tree needs a parent
928 /* copy must include namespaces and properties */
929 tmp2 = xmlDocCopyNode(listParent, target, 2);
930 xmlAddChild(tmp2, list);
932 listParent = listParent->parent;
939 * Check whether we need to change our insertion point
941 while (level < lastLevel) {
945 if (cur == end) { /* Are we at the end of the range? */
946 if (cur->type == XML_TEXT_NODE) {
947 const xmlChar *content = cur->content;
950 if (content == NULL) {
951 tmp = xmlNewTextLen(NULL, 0);
954 if ((cur == start) && (index1 > 1)) {
955 content += (index1 - 1);
961 tmp = xmlNewTextLen(content, len);
963 /* single sub text node selection */
966 /* prune and return full set */
967 if (level == lastLevel)
968 xmlAddNextSibling(last, tmp);
970 xmlAddChild(last, tmp);
972 } else { /* ending node not a text node */
973 endLevel = level; /* remember the level of the end node */
975 /* last node - need to take care of properties + namespaces */
976 tmp = xmlDocCopyNode(cur, target, 2);
979 listParent = cur->parent;
981 if (level == lastLevel)
982 xmlAddNextSibling(last, tmp);
984 xmlAddChild(last, tmp);
991 end = xmlXIncludeGetNthChild(cur, index2 - 1);
994 if ((cur == start) && (index1 > 1)) {
995 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1000 level++; /* increment level to show change */
1002 * Now gather the remaining nodes from cur to end
1004 continue; /* while */
1006 } else if (cur == start) { /* Not at the end, are we at start? */
1007 if ((cur->type == XML_TEXT_NODE) ||
1008 (cur->type == XML_CDATA_SECTION_NODE)) {
1009 const xmlChar *content = cur->content;
1011 if (content == NULL) {
1012 tmp = xmlNewTextLen(NULL, 0);
1015 content += (index1 - 1);
1018 tmp = xmlNewText(content);
1021 listParent = cur->parent;
1022 } else { /* Not text node */
1024 * start of the range - need to take care of
1025 * properties and namespaces
1027 tmp = xmlDocCopyNode(cur, target, 2);
1029 listParent = cur->parent;
1030 if (index1 > 1) { /* Do we need to position? */
1031 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1032 level = lastLevel = 1;
1035 * Now gather the remaining nodes from cur to end
1037 continue; /* while */
1042 switch (cur->type) {
1044 case XML_ELEMENT_DECL:
1045 case XML_ATTRIBUTE_DECL:
1046 case XML_ENTITY_NODE:
1047 /* Do not copy DTD informations */
1049 case XML_ENTITY_DECL:
1050 /* handle crossing entities -> stack needed */
1052 case XML_XINCLUDE_START:
1053 case XML_XINCLUDE_END:
1054 /* don't consider it part of the tree content */
1056 case XML_ATTRIBUTE_NODE:
1057 /* Humm, should not happen ! */
1061 * Middle of the range - need to take care of
1062 * properties and namespaces
1064 tmp = xmlDocCopyNode(cur, target, 2);
1068 if (level == lastLevel)
1069 xmlAddNextSibling(last, tmp);
1071 xmlAddChild(last, tmp);
1078 * Skip to next node in document order
1080 cur = xmlXPtrAdvanceNode(cur, &level);
1081 if (endFlag && (level >= endLevel))
1088 * xmlXIncludeBuildNodeList:
1089 * @ctxt: the XInclude context
1090 * @target: the document target
1091 * @source: the document source
1092 * @obj: the XPointer result from the evaluation.
1094 * Build a node list tree copy of the XPointer result.
1095 * This will drop Attributes and Namespace declarations.
1097 * Returns an xmlNodePtr list or NULL.
1098 * the caller has to free the node tree.
1101 xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1102 xmlDocPtr source, xmlXPathObjectPtr obj) {
1103 xmlNodePtr list = NULL, last = NULL;
1108 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1111 switch (obj->type) {
1112 case XPATH_NODESET: {
1113 xmlNodeSetPtr set = obj->nodesetval;
1116 for (i = 0;i < set->nodeNr;i++) {
1117 if (set->nodeTab[i] == NULL)
1119 switch (set->nodeTab[i]->type) {
1121 case XML_CDATA_SECTION_NODE:
1122 case XML_ELEMENT_NODE:
1123 case XML_ENTITY_REF_NODE:
1124 case XML_ENTITY_NODE:
1126 case XML_COMMENT_NODE:
1127 case XML_DOCUMENT_NODE:
1128 case XML_HTML_DOCUMENT_NODE:
1129 #ifdef LIBXML_DOCB_ENABLED
1130 case XML_DOCB_DOCUMENT_NODE:
1132 case XML_XINCLUDE_END:
1134 case XML_XINCLUDE_START: {
1135 xmlNodePtr tmp, cur = set->nodeTab[i];
1138 while (cur != NULL) {
1141 case XML_CDATA_SECTION_NODE:
1142 case XML_ELEMENT_NODE:
1143 case XML_ENTITY_REF_NODE:
1144 case XML_ENTITY_NODE:
1146 case XML_COMMENT_NODE:
1147 tmp = xmlXIncludeCopyNode(ctxt, target,
1152 xmlAddNextSibling(last, tmp);
1164 case XML_ATTRIBUTE_NODE:
1165 case XML_NAMESPACE_DECL:
1166 case XML_DOCUMENT_TYPE_NODE:
1167 case XML_DOCUMENT_FRAG_NODE:
1168 case XML_NOTATION_NODE:
1170 case XML_ELEMENT_DECL:
1171 case XML_ATTRIBUTE_DECL:
1172 case XML_ENTITY_DECL:
1176 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1179 xmlAddNextSibling(last,
1180 xmlXIncludeCopyNode(ctxt, target, source,
1182 if (last->next != NULL)
1188 case XPATH_LOCATIONSET: {
1189 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1192 for (i = 0;i < set->locNr;i++) {
1194 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1197 xmlAddNextSibling(last,
1198 xmlXIncludeCopyXPointer(ctxt, target, source,
1201 while (last->next != NULL)
1207 #ifdef LIBXML_XPTR_ENABLED
1209 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
1212 /* points are ignored in XInclude */
1219 /************************************************************************
1221 * XInclude I/O handling *
1223 ************************************************************************/
1225 typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1226 typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1227 struct _xmlXIncludeMergeData {
1229 xmlXIncludeCtxtPtr ctxt;
1233 * xmlXIncludeMergeOneEntity:
1235 * @doc: the including doc
1236 * @nr: the entity name
1238 * Inplements the merge of one entity
1241 xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
1242 xmlChar *name ATTRIBUTE_UNUSED) {
1243 xmlEntityPtr ret, prev;
1245 xmlXIncludeCtxtPtr ctxt;
1247 if ((ent == NULL) || (data == NULL))
1251 if ((ctxt == NULL) || (doc == NULL))
1253 switch (ent->etype) {
1254 case XML_INTERNAL_PARAMETER_ENTITY:
1255 case XML_EXTERNAL_PARAMETER_ENTITY:
1256 case XML_INTERNAL_PREDEFINED_ENTITY:
1258 case XML_INTERNAL_GENERAL_ENTITY:
1259 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1260 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1263 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1264 ent->SystemID, ent->content);
1266 if (ent->URI != NULL)
1267 ret->URI = xmlStrdup(ent->URI);
1269 prev = xmlGetDocEntity(doc, ent->name);
1271 if (ent->etype != prev->etype)
1274 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1275 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1277 } else if ((ent->ExternalID != NULL) &&
1278 (prev->ExternalID != NULL)) {
1279 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1281 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1282 if (!xmlStrEqual(ent->content, prev->content))
1292 switch (ent->etype) {
1293 case XML_INTERNAL_PARAMETER_ENTITY:
1294 case XML_EXTERNAL_PARAMETER_ENTITY:
1295 case XML_INTERNAL_PREDEFINED_ENTITY:
1296 case XML_INTERNAL_GENERAL_ENTITY:
1297 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1299 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1302 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1303 "mismatch in redefinition of entity %s\n",
1308 * xmlXIncludeMergeEntities:
1309 * @ctxt: an XInclude context
1310 * @doc: the including doc
1311 * @from: the included doc
1313 * Inplements the entity merge
1315 * Returns 0 if merge succeeded, -1 if some processing failed
1318 xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1321 xmlDtdPtr target, source;
1326 if ((from == NULL) || (from->intSubset == NULL))
1329 target = doc->intSubset;
1330 if (target == NULL) {
1331 cur = xmlDocGetRootElement(doc);
1334 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1339 source = from->intSubset;
1340 if ((source != NULL) && (source->entities != NULL)) {
1341 xmlXIncludeMergeData data;
1346 xmlHashScan((xmlHashTablePtr) source->entities,
1347 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
1349 source = from->extSubset;
1350 if ((source != NULL) && (source->entities != NULL)) {
1351 xmlXIncludeMergeData data;
1357 * don't duplicate existing stuff when external subsets are the same
1359 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1360 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1361 xmlHashScan((xmlHashTablePtr) source->entities,
1362 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
1369 * xmlXIncludeLoadDoc:
1370 * @ctxt: the XInclude context
1371 * @url: the associated URL
1372 * @nr: the xinclude node number
1374 * Load the document, and store the result in the XInclude context
1376 * Returns 0 in case of success, -1 in case of failure
1379 xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1383 xmlChar *fragment = NULL;
1385 #ifdef LIBXML_XPTR_ENABLED
1389 #ifdef DEBUG_XINCLUDE
1390 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1393 * Check the URL and remove any fragment identifier
1395 uri = xmlParseURI((const char *)url);
1397 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1398 XML_XINCLUDE_HREF_URI,
1399 "invalid value URI %s\n", url);
1402 if (uri->fragment != NULL) {
1403 fragment = (xmlChar *) uri->fragment;
1404 uri->fragment = NULL;
1406 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1407 (ctxt->incTab[nr]->fragment != NULL)) {
1408 if (fragment != NULL) xmlFree(fragment);
1409 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1411 URL = xmlSaveUri(uri);
1414 if (ctxt->incTab != NULL)
1415 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1416 XML_XINCLUDE_HREF_URI,
1417 "invalid value URI %s\n", url);
1419 xmlXIncludeErr(ctxt, NULL,
1420 XML_XINCLUDE_HREF_URI,
1421 "invalid value URI %s\n", url);
1422 if (fragment != NULL)
1428 * Handling of references to the local document are done
1429 * directly through ctxt->doc.
1431 if ((URL[0] == 0) || (URL[0] == '#') ||
1432 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
1438 * Prevent reloading twice the document.
1440 for (i = 0; i < ctxt->incNr; i++) {
1441 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1442 (ctxt->incTab[i]->doc != NULL)) {
1443 doc = ctxt->incTab[i]->doc;
1444 #ifdef DEBUG_XINCLUDE
1445 printf("Already loaded %s\n", URL);
1454 #ifdef DEBUG_XINCLUDE
1455 printf("loading %s\n", URL);
1457 #ifdef LIBXML_XPTR_ENABLED
1459 * If this is an XPointer evaluation, we want to assure that
1460 * all entities have been resolved prior to processing the
1461 * referenced document
1463 saveFlags = ctxt->parseFlags;
1464 if (fragment != NULL) { /* if this is an XPointer eval */
1465 ctxt->parseFlags |= XML_PARSE_NOENT;
1469 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
1470 #ifdef LIBXML_XPTR_ENABLED
1471 ctxt->parseFlags = saveFlags;
1475 if (fragment != NULL)
1479 ctxt->incTab[nr]->doc = doc;
1481 * It's possible that the requested URL has been mapped to a
1482 * completely different location (e.g. through a catalog entry).
1483 * To check for this, we compare the URL with that of the doc
1484 * and change it if they disagree (bug 146988).
1486 if (!xmlStrEqual(URL, doc->URL)) {
1488 URL = xmlStrdup(doc->URL);
1490 for (i = nr + 1; i < ctxt->incNr; i++) {
1491 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1492 ctxt->incTab[nr]->count++;
1493 #ifdef DEBUG_XINCLUDE
1494 printf("Increasing %s count since reused\n", URL);
1501 * Make sure we have all entities fixed up
1503 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
1506 * We don't need the DTD anymore, free up space
1507 if (doc->intSubset != NULL) {
1508 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1509 xmlFreeNode((xmlNodePtr) doc->intSubset);
1510 doc->intSubset = NULL;
1512 if (doc->extSubset != NULL) {
1513 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1514 xmlFreeNode((xmlNodePtr) doc->extSubset);
1515 doc->extSubset = NULL;
1518 xmlXIncludeRecurseDoc(ctxt, doc, URL);
1521 if (fragment == NULL) {
1523 * Add the top children list as the replacement copy.
1527 /* Hopefully a DTD declaration won't be copied from
1528 * the same document */
1529 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
1531 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
1532 doc, doc->children);
1535 #ifdef LIBXML_XPTR_ENABLED
1538 * Computes the XPointer expression and make a copy used
1539 * as the replacement copy.
1541 xmlXPathObjectPtr xptr;
1542 xmlXPathContextPtr xptrctxt;
1546 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1549 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1551 if (xptrctxt == NULL) {
1552 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1553 XML_XINCLUDE_XPTR_FAILED,
1554 "could not create XPointer context\n", NULL);
1559 xptr = xmlXPtrEval(fragment, xptrctxt);
1561 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1562 XML_XINCLUDE_XPTR_FAILED,
1563 "XPointer evaluation failed: #%s\n",
1565 xmlXPathFreeContext(xptrctxt);
1570 switch (xptr->type) {
1571 case XPATH_UNDEFINED:
1577 case XPATH_XSLT_TREE:
1578 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1579 XML_XINCLUDE_XPTR_RESULT,
1580 "XPointer is not a range: #%s\n",
1582 xmlXPathFreeContext(xptrctxt);
1587 if ((xptr->nodesetval == NULL) ||
1588 (xptr->nodesetval->nodeNr <= 0)) {
1589 xmlXPathFreeContext(xptrctxt);
1596 case XPATH_LOCATIONSET:
1599 set = xptr->nodesetval;
1601 for (i = 0;i < set->nodeNr;i++) {
1602 if (set->nodeTab[i] == NULL)
1604 switch (set->nodeTab[i]->type) {
1606 case XML_CDATA_SECTION_NODE:
1607 case XML_ENTITY_REF_NODE:
1608 case XML_ENTITY_NODE:
1610 case XML_COMMENT_NODE:
1611 case XML_DOCUMENT_NODE:
1612 case XML_HTML_DOCUMENT_NODE:
1613 #ifdef LIBXML_DOCB_ENABLED
1614 case XML_DOCB_DOCUMENT_NODE:
1617 case XML_ELEMENT_NODE: {
1619 xmlNodePtr el = set->nodeTab[i];
1621 nodeBase = xmlNodeGetBase(el->doc, el);
1622 if (nodeBase != NULL) {
1623 if (!xmlStrEqual(nodeBase, el->doc->URL))
1624 xmlNodeSetBase(el, nodeBase);
1630 case XML_ATTRIBUTE_NODE:
1631 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1632 XML_XINCLUDE_XPTR_RESULT,
1633 "XPointer selects an attribute: #%s\n",
1635 set->nodeTab[i] = NULL;
1637 case XML_NAMESPACE_DECL:
1638 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1639 XML_XINCLUDE_XPTR_RESULT,
1640 "XPointer selects a namespace: #%s\n",
1642 set->nodeTab[i] = NULL;
1644 case XML_DOCUMENT_TYPE_NODE:
1645 case XML_DOCUMENT_FRAG_NODE:
1646 case XML_NOTATION_NODE:
1648 case XML_ELEMENT_DECL:
1649 case XML_ATTRIBUTE_DECL:
1650 case XML_ENTITY_DECL:
1651 case XML_XINCLUDE_START:
1652 case XML_XINCLUDE_END:
1653 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1654 XML_XINCLUDE_XPTR_RESULT,
1655 "XPointer selects unexpected nodes: #%s\n",
1657 set->nodeTab[i] = NULL;
1658 set->nodeTab[i] = NULL;
1664 ctxt->incTab[nr]->xptr = xptr;
1665 ctxt->incTab[nr]->inc = NULL;
1667 ctxt->incTab[nr]->inc =
1668 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1669 xmlXPathFreeObject(xptr);
1671 xmlXPathFreeContext(xptrctxt);
1677 * Do the xml:base fixup if needed
1679 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
1685 * The base is only adjusted if "necessary", i.e. if the xinclude node
1686 * has a base specified, or the URL is relative
1688 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1692 * No xml:base on the xinclude node, so we check whether the
1693 * URI base is different than (relative to) the context base
1695 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1696 if (curBase == NULL) { /* Error return */
1697 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1698 XML_XINCLUDE_HREF_URI,
1699 "trying to build relative URI from %s\n", URL);
1701 /* If the URI doesn't contain a slash, it's not relative */
1702 if (!xmlStrchr(curBase, (xmlChar) '/'))
1708 if (base != NULL) { /* Adjustment may be needed */
1709 node = ctxt->incTab[nr]->inc;
1710 while (node != NULL) {
1711 /* Only work on element nodes */
1712 if (node->type == XML_ELEMENT_NODE) {
1713 curBase = xmlNodeGetBase(node->doc, node);
1714 /* If no current base, set it */
1715 if (curBase == NULL) {
1716 xmlNodeSetBase(node, base);
1719 * If the current base is the same as the
1720 * URL of the document, then reset it to be
1721 * the specified xml:base or the relative URI
1723 if (xmlStrEqual(curBase, node->doc->URL)) {
1724 xmlNodeSetBase(node, base);
1727 * If the element already has an xml:base
1728 * set, then relativise it if necessary
1731 xmlBase = xmlGetNsProp(node,
1734 if (xmlBase != NULL) {
1736 relBase = xmlBuildURI(xmlBase, base);
1737 if (relBase == NULL) { /* error */
1738 xmlXIncludeErr(ctxt,
1739 ctxt->incTab[nr]->ref,
1740 XML_XINCLUDE_HREF_URI,
1741 "trying to rebuild base from %s\n",
1744 xmlNodeSetBase(node, relBase);
1758 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1759 (ctxt->incTab[nr]->count <= 1)) {
1760 #ifdef DEBUG_XINCLUDE
1761 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1763 xmlFreeDoc(ctxt->incTab[nr]->doc);
1764 ctxt->incTab[nr]->doc = NULL;
1771 * xmlXIncludeLoadTxt:
1772 * @ctxt: the XInclude context
1773 * @url: the associated URL
1774 * @nr: the xinclude node number
1776 * Load the content, and store the result in the XInclude context
1778 * Returns 0 in case of success, -1 in case of failure
1781 xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1782 xmlParserInputBufferPtr buf;
1787 xmlChar *encoding = NULL;
1788 xmlCharEncoding enc = (xmlCharEncoding) 0;
1791 * Check the URL and remove any fragment identifier
1793 uri = xmlParseURI((const char *)url);
1795 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1796 "invalid value URI %s\n", url);
1799 if (uri->fragment != NULL) {
1800 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1801 "fragment identifier forbidden for text: %s\n",
1802 (const xmlChar *) uri->fragment);
1806 URL = xmlSaveUri(uri);
1809 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1810 "invalid value URI %s\n", url);
1815 * Handling of references to the local document are done
1816 * directly through ctxt->doc.
1819 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1820 XML_XINCLUDE_TEXT_DOCUMENT,
1821 "text serialization of document not available\n", NULL);
1827 * Prevent reloading twice the document.
1829 for (i = 0; i < ctxt->txtNr; i++) {
1830 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1831 node = xmlCopyNode(ctxt->txtTab[i], 1);
1836 * Try to get the encoding if available
1838 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1839 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1841 if (encoding != NULL) {
1843 * TODO: we should not have to remap to the xmlCharEncoding
1844 * predefined set, a better interface than
1845 * xmlParserInputBufferCreateFilename should allow any
1846 * encoding supported by iconv
1848 enc = xmlParseCharEncoding((const char *) encoding);
1849 if (enc == XML_CHAR_ENCODING_ERROR) {
1850 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1851 XML_XINCLUDE_UNKNOWN_ENCODING,
1852 "encoding %s not supported\n", encoding);
1863 buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
1868 node = xmlNewText(NULL);
1871 * Scan all chars from the resource and add the to the node
1873 while (xmlParserInputBufferRead(buf, 128) > 0) {
1875 const xmlChar *content;
1877 content = xmlBufferContent(buf->buffer);
1878 len = xmlBufferLength(buf->buffer);
1879 for (i = 0;i < len;) {
1883 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1884 if (!IS_CHAR(cur)) {
1885 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1886 XML_XINCLUDE_INVALID_CHAR,
1887 "%s contains invalid char\n", URL);
1889 xmlNodeAddContentLen(node, &content[i], l);
1893 xmlBufferShrink(buf->buffer, len);
1895 xmlFreeParserInputBuffer(buf);
1896 xmlXIncludeAddTxt(ctxt, node, URL);
1900 * Add the element as the replacement copy.
1902 ctxt->incTab[nr]->inc = node;
1908 * xmlXIncludeLoadFallback:
1909 * @ctxt: the XInclude context
1910 * @fallback: the fallback node
1911 * @nr: the xinclude node number
1913 * Load the content of the fallback node, and store the result
1914 * in the XInclude context
1916 * Returns 0 in case of success, -1 in case of failure
1919 xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
1920 xmlXIncludeCtxtPtr newctxt;
1923 if ((fallback == NULL) || (ctxt == NULL))
1925 if (fallback->children != NULL) {
1927 * It's possible that the fallback also has 'includes'
1928 * (Bug 129969), so we re-process the fallback just in case
1930 newctxt = xmlXIncludeNewContext(ctxt->doc);
1931 if (newctxt == NULL)
1933 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
1934 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1935 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
1936 if (ctxt->nbErrors > 0)
1939 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
1940 xmlXIncludeFreeContext(newctxt);
1942 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1943 fallback->children);
1945 ctxt->incTab[nr]->inc = NULL;
1946 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
1951 /************************************************************************
1953 * XInclude Processing *
1955 ************************************************************************/
1958 * xmlXIncludePreProcessNode:
1959 * @ctxt: an XInclude context
1960 * @node: an XInclude node
1962 * Implement the XInclude preprocessing, currently just adding the element
1963 * for further processing.
1965 * Returns the result list or NULL in case of error
1968 xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1969 xmlXIncludeAddNode(ctxt, node);
1974 * xmlXIncludeLoadNode:
1975 * @ctxt: an XInclude context
1976 * @nr: the node number
1978 * Find and load the infoset replacement for the given node.
1980 * Returns 0 if substitution succeeded, -1 if some processing failed
1983 xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
1990 int xml = 1; /* default Issue 64 */
1995 if ((nr < 0) || (nr >= ctxt->incNr))
1997 cur = ctxt->incTab[nr]->ref;
2002 * read the attributes
2004 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
2006 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
2010 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
2011 if (parse != NULL) {
2012 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2014 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2017 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2018 XML_XINCLUDE_PARSE_VALUE,
2019 "invalid value %s for 'parse'\n", parse);
2031 base = xmlNodeGetBase(ctxt->doc, cur);
2033 URI = xmlBuildURI(href, ctxt->doc->URL);
2035 URI = xmlBuildURI(href, base);
2041 * Some escaping may be needed
2043 escbase = xmlURIEscape(base);
2044 eschref = xmlURIEscape(href);
2045 URI = xmlBuildURI(eschref, escbase);
2046 if (escbase != NULL)
2048 if (eschref != NULL)
2052 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2053 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
2062 #ifdef DEBUG_XINCLUDE
2063 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2064 xml ? "xml": "text");
2065 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2069 * Save the base for this include (saving the current one)
2071 oldBase = ctxt->base;
2075 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
2076 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2078 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2082 * Restore the original base before checking for fallback
2084 ctxt->base = oldBase;
2087 xmlNodePtr children;
2090 * Time to try a fallback if availble
2092 #ifdef DEBUG_XINCLUDE
2093 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2095 children = cur->children;
2096 while (children != NULL) {
2097 if ((children->type == XML_ELEMENT_NODE) &&
2098 (children->ns != NULL) &&
2099 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
2100 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2101 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
2102 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
2106 children = children->next;
2110 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2111 XML_XINCLUDE_NO_FALLBACK,
2112 "could not load %s, and no fallback was found\n",
2131 * xmlXIncludeIncludeNode:
2132 * @ctxt: an XInclude context
2133 * @nr: the node number
2135 * Inplement the infoset replacement for the given node
2137 * Returns 0 if substitution succeeded, -1 if some processing failed
2140 xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
2141 xmlNodePtr cur, end, list, tmp;
2145 if ((nr < 0) || (nr >= ctxt->incNr))
2147 cur = ctxt->incTab[nr]->ref;
2152 * If we stored an XPointer a late computation may be needed
2154 if ((ctxt->incTab[nr]->inc == NULL) &&
2155 (ctxt->incTab[nr]->xptr != NULL)) {
2156 ctxt->incTab[nr]->inc =
2157 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2158 ctxt->incTab[nr]->xptr);
2159 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2160 ctxt->incTab[nr]->xptr = NULL;
2162 list = ctxt->incTab[nr]->inc;
2163 ctxt->incTab[nr]->inc = NULL;
2166 * Check against the risk of generating a multi-rooted document
2168 if ((cur->parent != NULL) &&
2169 (cur->parent->type != XML_ELEMENT_NODE)) {
2173 while (tmp != NULL) {
2174 if (tmp->type == XML_ELEMENT_NODE)
2179 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2180 XML_XINCLUDE_MULTIPLE_ROOT,
2181 "XInclude error: would result in multiple root nodes\n",
2187 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2189 * Add the list of nodes
2191 while (list != NULL) {
2195 xmlAddPrevSibling(cur, end);
2201 * Change the current node as an XInclude start one, and add an
2204 cur->type = XML_XINCLUDE_START;
2205 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
2207 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2208 XML_XINCLUDE_BUILD_FAILED,
2209 "failed to build node\n", NULL);
2212 end->type = XML_XINCLUDE_END;
2213 xmlAddNextSibling(cur, end);
2216 * Add the list of nodes
2218 while (list != NULL) {
2222 xmlAddPrevSibling(end, cur);
2231 * xmlXIncludeTestNode:
2232 * @ctxt: the XInclude processing context
2233 * @node: an XInclude node
2235 * test if the node is an XInclude node
2237 * Returns 1 true, 0 otherwise
2240 xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2243 if (node->type != XML_ELEMENT_NODE)
2245 if (node->ns == NULL)
2247 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2248 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2249 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2250 if (ctxt->legacy == 0) {
2251 #if 0 /* wait for the XML Core Working Group to get something stable ! */
2252 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2253 "Deprecated XInclude namespace found, use %s",
2259 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2260 xmlNodePtr child = node->children;
2261 int nb_fallback = 0;
2263 while (child != NULL) {
2264 if ((child->type == XML_ELEMENT_NODE) &&
2265 (child->ns != NULL) &&
2266 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2267 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
2268 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
2269 xmlXIncludeErr(ctxt, node,
2270 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2271 "%s has an 'include' child\n",
2275 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2279 child = child->next;
2281 if (nb_fallback > 1) {
2282 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2283 "%s has multiple fallback children\n",
2289 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2290 if ((node->parent == NULL) ||
2291 (node->parent->type != XML_ELEMENT_NODE) ||
2292 (node->parent->ns == NULL) ||
2293 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2294 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
2295 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
2296 xmlXIncludeErr(ctxt, node,
2297 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2298 "%s is not the child of an 'include'\n",
2307 * xmlXIncludeDoProcess:
2308 * @ctxt: the XInclude processing context
2309 * @doc: an XML document
2310 * @tree: the top of the tree to process
2312 * Implement the XInclude substitution on the XML document @doc
2314 * Returns 0 if no substitution were done, -1 if some processing failed
2315 * or the number of substitutions done.
2318 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
2323 if ((doc == NULL) || (tree == NULL))
2328 if (doc->URL != NULL) {
2329 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2333 start = ctxt->incNr;
2336 * First phase: lookup the elements in the document
2339 if (xmlXIncludeTestNode(ctxt, cur) == 1)
2340 xmlXIncludePreProcessNode(ctxt, cur);
2341 while ((cur != NULL) && (cur != tree->parent)) {
2342 /* TODO: need to work on entities -> stack */
2343 if ((cur->children != NULL) &&
2344 (cur->children->type != XML_ENTITY_DECL) &&
2345 (cur->children->type != XML_XINCLUDE_START) &&
2346 (cur->children->type != XML_XINCLUDE_END)) {
2347 cur = cur->children;
2348 if (xmlXIncludeTestNode(ctxt, cur))
2349 xmlXIncludePreProcessNode(ctxt, cur);
2350 } else if (cur->next != NULL) {
2352 if (xmlXIncludeTestNode(ctxt, cur))
2353 xmlXIncludePreProcessNode(ctxt, cur);
2359 if ((cur == NULL) || (cur == tree->parent))
2361 if (cur->next != NULL) {
2363 if (xmlXIncludeTestNode(ctxt, cur))
2364 xmlXIncludePreProcessNode(ctxt, cur);
2367 } while (cur != NULL);
2372 * Second Phase : collect the infosets fragments
2374 for (i = start;i < ctxt->incNr; i++) {
2375 xmlXIncludeLoadNode(ctxt, i);
2380 * Third phase: extend the original document infoset.
2382 * Originally we bypassed the inclusion if there were any errors
2383 * encountered on any of the XIncludes. A bug was raised (bug
2384 * 132588) requesting that we output the XIncludes without error,
2385 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2386 * give some other problems in the future, but for now it seems to
2390 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2391 if ((ctxt->incTab[i]->inc != NULL) ||
2392 (ctxt->incTab[i]->xptr != NULL) ||
2393 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
2394 xmlXIncludeIncludeNode(ctxt, i);
2397 if (doc->URL != NULL)
2398 xmlXIncludeURLPop(ctxt);
2403 * xmlXIncludeSetFlags:
2404 * @ctxt: an XInclude processing context
2405 * @flags: a set of xmlParserOption used for parsing XML includes
2407 * Set the flags used for further processing of XML resources.
2409 * Returns 0 in case of success and -1 in case of error.
2412 xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2415 ctxt->parseFlags = flags;
2420 * xmlXIncludeProcessFlagsData:
2421 * @doc: an XML document
2422 * @flags: a set of xmlParserOption used for parsing XML includes
2423 * @data: application data that will be passed to the parser context
2424 * in the _private field of the parser context(s)
2426 * Implement the XInclude substitution on the XML document @doc
2428 * Returns 0 if no substitution were done, -1 if some processing failed
2429 * or the number of substitutions done.
2432 xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
2433 xmlXIncludeCtxtPtr ctxt;
2439 tree = xmlDocGetRootElement(doc);
2442 ctxt = xmlXIncludeNewContext(doc);
2445 ctxt->_private = data;
2446 ctxt->base = xmlStrdup((xmlChar *)doc->URL);
2447 xmlXIncludeSetFlags(ctxt, flags);
2448 ret = xmlXIncludeDoProcess(ctxt, doc, tree);
2449 if ((ret >= 0) && (ctxt->nbErrors > 0))
2452 xmlXIncludeFreeContext(ctxt);
2457 * xmlXIncludeProcessFlags:
2458 * @doc: an XML document
2459 * @flags: a set of xmlParserOption used for parsing XML includes
2461 * Implement the XInclude substitution on the XML document @doc
2463 * Returns 0 if no substitution were done, -1 if some processing failed
2464 * or the number of substitutions done.
2467 xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2468 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2472 * xmlXIncludeProcess:
2473 * @doc: an XML document
2475 * Implement the XInclude substitution on the XML document @doc
2477 * Returns 0 if no substitution were done, -1 if some processing failed
2478 * or the number of substitutions done.
2481 xmlXIncludeProcess(xmlDocPtr doc) {
2482 return(xmlXIncludeProcessFlags(doc, 0));
2486 * xmlXIncludeProcessTreeFlags:
2487 * @tree: a node in an XML document
2488 * @flags: a set of xmlParserOption used for parsing XML includes
2490 * Implement the XInclude substitution for the given subtree
2492 * Returns 0 if no substitution were done, -1 if some processing failed
2493 * or the number of substitutions done.
2496 xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2497 xmlXIncludeCtxtPtr ctxt;
2500 if ((tree == NULL) || (tree->doc == NULL))
2502 ctxt = xmlXIncludeNewContext(tree->doc);
2505 ctxt->base = xmlNodeGetBase(tree->doc, tree);
2506 xmlXIncludeSetFlags(ctxt, flags);
2507 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2508 if ((ret >= 0) && (ctxt->nbErrors > 0))
2511 xmlXIncludeFreeContext(ctxt);
2516 * xmlXIncludeProcessTree:
2517 * @tree: a node in an XML document
2519 * Implement the XInclude substitution for the given subtree
2521 * Returns 0 if no substitution were done, -1 if some processing failed
2522 * or the number of substitutions done.
2525 xmlXIncludeProcessTree(xmlNodePtr tree) {
2526 return(xmlXIncludeProcessTreeFlags(tree, 0));
2530 * xmlXIncludeProcessNode:
2531 * @ctxt: an existing XInclude context
2532 * @node: a node in an XML document
2534 * Implement the XInclude substitution for the given subtree reusing
2535 * the informations and data coming from the given context.
2537 * Returns 0 if no substitution were done, -1 if some processing failed
2538 * or the number of substitutions done.
2541 xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2544 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2546 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2547 if ((ret >= 0) && (ctxt->nbErrors > 0))
2552 #else /* !LIBXML_XINCLUDE_ENABLED */
2554 #define bottom_xinclude
2555 #include "elfgcchack.h"