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 xmlParserInputPtr inputStream;
426 pctxt = xmlNewParserCtxt();
428 xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
433 * pass in the application data to the parser context.
435 pctxt->_private = ctxt->_private;
438 * try to ensure that new documents included are actually
439 * built with the same dictionary as the including document.
441 if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
442 if (pctxt->dict != NULL)
443 xmlDictFree(pctxt->dict);
444 pctxt->dict = ctxt->doc->dict;
445 xmlDictReference(pctxt->dict);
448 xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
450 inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
451 if (inputStream == NULL) {
452 xmlFreeParserCtxt(pctxt);
456 inputPush(pctxt, inputStream);
458 if (pctxt->directory == NULL)
459 pctxt->directory = xmlParserGetDirectory(URL);
461 pctxt->loadsubset |= XML_DETECT_IDS;
463 xmlParseDocument(pctxt);
465 if (pctxt->wellFormed) {
470 if (pctxt->myDoc != NULL)
471 xmlFreeDoc(pctxt->myDoc);
474 xmlFreeParserCtxt(pctxt);
480 * xmlXIncludeAddNode:
481 * @ctxt: the XInclude context
484 * Add a new node to process to an XInclude context
487 xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
488 xmlXIncludeRefPtr ref;
491 xmlChar *fragment = NULL;
496 int xml = 1, i; /* default Issue 64 */
505 #ifdef DEBUG_XINCLUDE
506 xmlGenericError(xmlGenericErrorContext, "Add node\n");
509 * read the attributes
511 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
513 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
517 if ((href[0] == '#') || (href[0] == 0))
519 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
521 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
523 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
526 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
527 "invalid value %s for 'parse'\n", parse);
539 base = xmlNodeGetBase(ctxt->doc, cur);
541 URI = xmlBuildURI(href, ctxt->doc->URL);
543 URI = xmlBuildURI(href, base);
549 * Some escaping may be needed
551 escbase = xmlURIEscape(base);
552 eschref = xmlURIEscape(href);
553 URI = xmlBuildURI(eschref, escbase);
566 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
567 "failed build URL\n", NULL);
570 fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
573 * Check the URL and remove any fragment identifier
575 uri = xmlParseURI((const char *)URI);
577 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
578 "invalid value URI %s\n", URI);
579 if (fragment != NULL)
585 if (uri->fragment != NULL) {
586 if (ctxt->legacy != 0) {
587 if (fragment == NULL) {
588 fragment = (xmlChar *) uri->fragment;
590 xmlFree(uri->fragment);
593 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
594 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
596 if (fragment != NULL)
602 uri->fragment = NULL;
604 URL = xmlSaveUri(uri);
608 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
609 "invalid value URI %s\n", URI);
610 if (fragment != NULL)
616 * If local and xml then we need a fragment
618 if ((local == 1) && (xml == 1) &&
619 ((fragment == NULL) || (fragment[0] == 0))) {
620 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
621 "detected a local recursion with no xpointer in %s\n",
623 if (fragment != NULL)
629 * Check the URL against the stack for recursions
631 if ((!local) && (xml == 1)) {
632 for (i = 0;i < ctxt->urlNr;i++) {
633 if (xmlStrEqual(URL, ctxt->urlTab[i])) {
634 xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
635 "detected a recursion in %s\n", URL);
641 ref = xmlXIncludeNewRef(ctxt, URL, cur);
645 ref->fragment = fragment;
654 * xmlXIncludeRecurseDoc:
655 * @ctxt: the XInclude context
656 * @doc: the new document
657 * @url: the associated URL
659 * The XInclude recursive nature is handled at this point.
662 xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
663 const xmlURL url ATTRIBUTE_UNUSED) {
664 xmlXIncludeCtxtPtr newctxt;
668 * Avoid recursion in already substitued resources
669 for (i = 0;i < ctxt->urlNr;i++) {
670 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
675 #ifdef DEBUG_XINCLUDE
676 xmlGenericError(xmlGenericErrorContext, "Recursing in doc %s\n", doc->URL);
679 * Handle recursion here.
682 newctxt = xmlXIncludeNewContext(doc);
683 if (newctxt != NULL) {
685 * Copy the private user data
687 newctxt->_private = ctxt->_private;
689 * Copy the existing document set
691 newctxt->incMax = ctxt->incMax;
692 newctxt->incNr = ctxt->incNr;
693 newctxt->incTab = (xmlXIncludeRefPtr *) xmlMalloc(newctxt->incMax *
694 sizeof(newctxt->incTab[0]));
695 if (newctxt->incTab == NULL) {
696 xmlXIncludeErrMemory(ctxt, (xmlNodePtr) doc, "processing doc");
703 newctxt->urlMax = ctxt->urlMax;
704 newctxt->urlNr = ctxt->urlNr;
705 newctxt->urlTab = ctxt->urlTab;
708 * Inherit the existing base
710 newctxt->base = xmlStrdup(ctxt->base);
713 * Inherit the documents already in use by other includes
715 newctxt->incBase = ctxt->incNr;
716 for (i = 0;i < ctxt->incNr;i++) {
717 newctxt->incTab[i] = ctxt->incTab[i];
718 newctxt->incTab[i]->count++; /* prevent the recursion from
722 * The new context should also inherit the Parse Flags
725 newctxt->parseFlags = ctxt->parseFlags;
726 xmlXIncludeDoProcess(newctxt, doc, xmlDocGetRootElement(doc));
727 for (i = 0;i < ctxt->incNr;i++) {
728 newctxt->incTab[i]->count--;
729 newctxt->incTab[i] = NULL;
732 /* urlTab may have been reallocated */
733 ctxt->urlTab = newctxt->urlTab;
734 ctxt->urlMax = newctxt->urlMax;
738 newctxt->urlTab = NULL;
740 xmlXIncludeFreeContext(newctxt);
742 #ifdef DEBUG_XINCLUDE
743 xmlGenericError(xmlGenericErrorContext, "Done recursing in doc %s\n", url);
749 * @ctxt: the XInclude context
750 * @txt: the new text node
751 * @url: the associated URL
753 * Add a new txtument to the list
756 xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
757 #ifdef DEBUG_XINCLUDE
758 xmlGenericError(xmlGenericErrorContext, "Adding text %s\n", url);
760 if (ctxt->txtMax == 0) {
762 ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
763 sizeof(ctxt->txtTab[0]));
764 if (ctxt->txtTab == NULL) {
765 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
768 ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
769 sizeof(ctxt->txturlTab[0]));
770 if (ctxt->txturlTab == NULL) {
771 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
775 if (ctxt->txtNr >= ctxt->txtMax) {
777 ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
778 ctxt->txtMax * sizeof(ctxt->txtTab[0]));
779 if (ctxt->txtTab == NULL) {
780 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
783 ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
784 ctxt->txtMax * sizeof(ctxt->txturlTab[0]));
785 if (ctxt->txturlTab == NULL) {
786 xmlXIncludeErrMemory(ctxt, NULL, "processing text");
790 ctxt->txtTab[ctxt->txtNr] = txt;
791 ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
795 /************************************************************************
797 * Node copy with specific semantic *
799 ************************************************************************/
802 xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
803 xmlDocPtr source, xmlNodePtr elem);
806 * xmlXIncludeCopyNode:
807 * @ctxt: the XInclude context
808 * @target: the document target
809 * @source: the document source
812 * Make a copy of the node while preserving the XInclude semantic
813 * of the Infoset copy
816 xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
817 xmlDocPtr source, xmlNodePtr elem) {
818 xmlNodePtr result = NULL;
820 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
823 if (elem->type == XML_DTD_NODE)
825 if (elem->type == XML_DOCUMENT_NODE)
826 result = xmlXIncludeCopyNodeList(ctxt, target, source, elem->children);
828 result = xmlDocCopyNode(elem, target, 1);
833 * xmlXIncludeCopyNodeList:
834 * @ctxt: the XInclude context
835 * @target: the document target
836 * @source: the document source
837 * @elem: the element list
839 * Make a copy of the node list while preserving the XInclude semantic
840 * of the Infoset copy
843 xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
844 xmlDocPtr source, xmlNodePtr elem) {
845 xmlNodePtr cur, res, result = NULL, last = NULL;
847 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
851 while (cur != NULL) {
852 res = xmlXIncludeCopyNode(ctxt, target, source, cur);
854 if (result == NULL) {
868 * xmlXIncludeGetNthChild:
870 * @no: the child number
872 * Returns the @n'th element child of @cur or NULL
875 xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
880 for (i = 0;i <= no;cur = cur->next) {
883 if ((cur->type == XML_ELEMENT_NODE) ||
884 (cur->type == XML_DOCUMENT_NODE) ||
885 (cur->type == XML_HTML_DOCUMENT_NODE)) {
894 xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
896 * xmlXIncludeCopyRange:
897 * @ctxt: the XInclude context
898 * @target: the document target
899 * @source: the document source
900 * @obj: the XPointer result from the evaluation.
902 * Build a node list tree copy of the XPointer result.
904 * Returns an xmlNodePtr list or NULL.
905 * The caller has to free the node tree.
908 xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
909 xmlDocPtr source, xmlXPathObjectPtr range) {
910 /* pointers to generated nodes */
911 xmlNodePtr list = NULL, last = NULL, listParent = NULL;
912 xmlNodePtr tmp, tmp2;
913 /* pointers to traversal nodes */
914 xmlNodePtr start, cur, end;
916 int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
918 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
921 if (range->type != XPATH_RANGE)
923 start = (xmlNodePtr) range->user;
929 return(xmlDocCopyNode(start, target, 1));
932 index1 = range->index;
933 index2 = range->index2;
935 * level is depth of the current node under consideration
936 * list is the pointer to the root of the output tree
937 * listParent is a pointer to the parent of output tree (within
938 the included file) in case we need to add another level
939 * last is a pointer to the last node added to the output tree
940 * lastLevel is the depth of last (relative to the root)
942 while (cur != NULL) {
944 * Check if our output tree needs a parent
948 /* copy must include namespaces and properties */
949 tmp2 = xmlDocCopyNode(listParent, target, 2);
950 xmlAddChild(tmp2, list);
952 listParent = listParent->parent;
959 * Check whether we need to change our insertion point
961 while (level < lastLevel) {
965 if (cur == end) { /* Are we at the end of the range? */
966 if (cur->type == XML_TEXT_NODE) {
967 const xmlChar *content = cur->content;
970 if (content == NULL) {
971 tmp = xmlNewTextLen(NULL, 0);
974 if ((cur == start) && (index1 > 1)) {
975 content += (index1 - 1);
980 tmp = xmlNewTextLen(content, len);
982 /* single sub text node selection */
985 /* prune and return full set */
986 if (level == lastLevel)
987 xmlAddNextSibling(last, tmp);
989 xmlAddChild(last, tmp);
991 } else { /* ending node not a text node */
992 endLevel = level; /* remember the level of the end node */
994 /* last node - need to take care of properties + namespaces */
995 tmp = xmlDocCopyNode(cur, target, 2);
998 listParent = cur->parent;
1000 if (level == lastLevel)
1001 xmlAddNextSibling(last, tmp);
1003 xmlAddChild(last, tmp);
1010 end = xmlXIncludeGetNthChild(cur, index2 - 1);
1013 if ((cur == start) && (index1 > 1)) {
1014 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1017 cur = cur->children;
1019 level++; /* increment level to show change */
1021 * Now gather the remaining nodes from cur to end
1023 continue; /* while */
1025 } else if (cur == start) { /* Not at the end, are we at start? */
1026 if ((cur->type == XML_TEXT_NODE) ||
1027 (cur->type == XML_CDATA_SECTION_NODE)) {
1028 const xmlChar *content = cur->content;
1030 if (content == NULL) {
1031 tmp = xmlNewTextLen(NULL, 0);
1034 content += (index1 - 1);
1037 tmp = xmlNewText(content);
1040 listParent = cur->parent;
1041 } else { /* Not text node */
1043 * start of the range - need to take care of
1044 * properties and namespaces
1046 tmp = xmlDocCopyNode(cur, target, 2);
1048 listParent = cur->parent;
1049 if (index1 > 1) { /* Do we need to position? */
1050 cur = xmlXIncludeGetNthChild(cur, index1 - 1);
1051 level = lastLevel = 1;
1054 * Now gather the remaining nodes from cur to end
1056 continue; /* while */
1061 switch (cur->type) {
1063 case XML_ELEMENT_DECL:
1064 case XML_ATTRIBUTE_DECL:
1065 case XML_ENTITY_NODE:
1066 /* Do not copy DTD informations */
1068 case XML_ENTITY_DECL:
1069 /* handle crossing entities -> stack needed */
1071 case XML_XINCLUDE_START:
1072 case XML_XINCLUDE_END:
1073 /* don't consider it part of the tree content */
1075 case XML_ATTRIBUTE_NODE:
1076 /* Humm, should not happen ! */
1080 * Middle of the range - need to take care of
1081 * properties and namespaces
1083 tmp = xmlDocCopyNode(cur, target, 2);
1087 if (level == lastLevel)
1088 xmlAddNextSibling(last, tmp);
1090 xmlAddChild(last, tmp);
1097 * Skip to next node in document order
1099 cur = xmlXPtrAdvanceNode(cur, &level);
1100 if (endFlag && (level >= endLevel))
1107 * xmlXIncludeBuildNodeList:
1108 * @ctxt: the XInclude context
1109 * @target: the document target
1110 * @source: the document source
1111 * @obj: the XPointer result from the evaluation.
1113 * Build a node list tree copy of the XPointer result.
1114 * This will drop Attributes and Namespace declarations.
1116 * Returns an xmlNodePtr list or NULL.
1117 * the caller has to free the node tree.
1120 xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlDocPtr target,
1121 xmlDocPtr source, xmlXPathObjectPtr obj) {
1122 xmlNodePtr list = NULL, last = NULL;
1127 if ((ctxt == NULL) || (target == NULL) || (source == NULL) ||
1130 switch (obj->type) {
1131 case XPATH_NODESET: {
1132 xmlNodeSetPtr set = obj->nodesetval;
1135 for (i = 0;i < set->nodeNr;i++) {
1136 if (set->nodeTab[i] == NULL)
1138 switch (set->nodeTab[i]->type) {
1140 case XML_CDATA_SECTION_NODE:
1141 case XML_ELEMENT_NODE:
1142 case XML_ENTITY_REF_NODE:
1143 case XML_ENTITY_NODE:
1145 case XML_COMMENT_NODE:
1146 case XML_DOCUMENT_NODE:
1147 case XML_HTML_DOCUMENT_NODE:
1148 #ifdef LIBXML_DOCB_ENABLED
1149 case XML_DOCB_DOCUMENT_NODE:
1151 case XML_XINCLUDE_END:
1153 case XML_XINCLUDE_START: {
1154 xmlNodePtr tmp, cur = set->nodeTab[i];
1157 while (cur != NULL) {
1160 case XML_CDATA_SECTION_NODE:
1161 case XML_ELEMENT_NODE:
1162 case XML_ENTITY_REF_NODE:
1163 case XML_ENTITY_NODE:
1165 case XML_COMMENT_NODE:
1166 tmp = xmlXIncludeCopyNode(ctxt, target,
1171 xmlAddNextSibling(last, tmp);
1183 case XML_ATTRIBUTE_NODE:
1184 case XML_NAMESPACE_DECL:
1185 case XML_DOCUMENT_TYPE_NODE:
1186 case XML_DOCUMENT_FRAG_NODE:
1187 case XML_NOTATION_NODE:
1189 case XML_ELEMENT_DECL:
1190 case XML_ATTRIBUTE_DECL:
1191 case XML_ENTITY_DECL:
1195 list = last = xmlXIncludeCopyNode(ctxt, target, source,
1198 xmlAddNextSibling(last,
1199 xmlXIncludeCopyNode(ctxt, target, source,
1201 if (last->next != NULL)
1207 case XPATH_LOCATIONSET: {
1208 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1211 for (i = 0;i < set->locNr;i++) {
1213 list = last = xmlXIncludeCopyXPointer(ctxt, target, source,
1216 xmlAddNextSibling(last,
1217 xmlXIncludeCopyXPointer(ctxt, target, source,
1220 while (last->next != NULL)
1226 #ifdef LIBXML_XPTR_ENABLED
1228 return(xmlXIncludeCopyRange(ctxt, target, source, obj));
1231 /* points are ignored in XInclude */
1238 /************************************************************************
1240 * XInclude I/O handling *
1242 ************************************************************************/
1244 typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
1245 typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
1246 struct _xmlXIncludeMergeData {
1248 xmlXIncludeCtxtPtr ctxt;
1252 * xmlXIncludeMergeOneEntity:
1254 * @doc: the including doc
1255 * @nr: the entity name
1257 * Inplements the merge of one entity
1260 xmlXIncludeMergeEntity(xmlEntityPtr ent, xmlXIncludeMergeDataPtr data,
1261 xmlChar *name ATTRIBUTE_UNUSED) {
1262 xmlEntityPtr ret, prev;
1264 xmlXIncludeCtxtPtr ctxt;
1266 if ((ent == NULL) || (data == NULL))
1270 if ((ctxt == NULL) || (doc == NULL))
1272 switch (ent->etype) {
1273 case XML_INTERNAL_PARAMETER_ENTITY:
1274 case XML_EXTERNAL_PARAMETER_ENTITY:
1275 case XML_INTERNAL_PREDEFINED_ENTITY:
1277 case XML_INTERNAL_GENERAL_ENTITY:
1278 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1279 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1282 ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
1283 ent->SystemID, ent->content);
1285 if (ent->URI != NULL)
1286 ret->URI = xmlStrdup(ent->URI);
1288 prev = xmlGetDocEntity(doc, ent->name);
1290 if (ent->etype != prev->etype)
1293 if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
1294 if (!xmlStrEqual(ent->SystemID, prev->SystemID))
1296 } else if ((ent->ExternalID != NULL) &&
1297 (prev->ExternalID != NULL)) {
1298 if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
1300 } else if ((ent->content != NULL) && (prev->content != NULL)) {
1301 if (!xmlStrEqual(ent->content, prev->content))
1311 switch (ent->etype) {
1312 case XML_INTERNAL_PARAMETER_ENTITY:
1313 case XML_EXTERNAL_PARAMETER_ENTITY:
1314 case XML_INTERNAL_PREDEFINED_ENTITY:
1315 case XML_INTERNAL_GENERAL_ENTITY:
1316 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1318 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1321 xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
1322 "mismatch in redefinition of entity %s\n",
1327 * xmlXIncludeMergeEntities:
1328 * @ctxt: an XInclude context
1329 * @doc: the including doc
1330 * @from: the included doc
1332 * Inplements the entity merge
1334 * Returns 0 if merge succeeded, -1 if some processing failed
1337 xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
1340 xmlDtdPtr target, source;
1345 if ((from == NULL) || (from->intSubset == NULL))
1348 target = doc->intSubset;
1349 if (target == NULL) {
1350 cur = xmlDocGetRootElement(doc);
1353 target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
1358 source = from->intSubset;
1359 if ((source != NULL) && (source->entities != NULL)) {
1360 xmlXIncludeMergeData data;
1365 xmlHashScan((xmlHashTablePtr) source->entities,
1366 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
1368 source = from->extSubset;
1369 if ((source != NULL) && (source->entities != NULL)) {
1370 xmlXIncludeMergeData data;
1376 * don't duplicate existing stuff when external subsets are the same
1378 if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
1379 (!xmlStrEqual(target->SystemID, source->SystemID))) {
1380 xmlHashScan((xmlHashTablePtr) source->entities,
1381 (xmlHashScanner) xmlXIncludeMergeEntity, &data);
1388 * xmlXIncludeLoadDoc:
1389 * @ctxt: the XInclude context
1390 * @url: the associated URL
1391 * @nr: the xinclude node number
1393 * Load the document, and store the result in the XInclude context
1395 * Returns 0 in case of success, -1 in case of failure
1398 xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1402 xmlChar *fragment = NULL;
1404 #ifdef LIBXML_XPTR_ENABLED
1408 #ifdef DEBUG_XINCLUDE
1409 xmlGenericError(xmlGenericErrorContext, "Loading doc %s:%d\n", url, nr);
1412 * Check the URL and remove any fragment identifier
1414 uri = xmlParseURI((const char *)url);
1416 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1417 XML_XINCLUDE_HREF_URI,
1418 "invalid value URI %s\n", url);
1421 if (uri->fragment != NULL) {
1422 fragment = (xmlChar *) uri->fragment;
1423 uri->fragment = NULL;
1425 if ((ctxt->incTab != NULL) && (ctxt->incTab[nr] != NULL) &&
1426 (ctxt->incTab[nr]->fragment != NULL)) {
1427 if (fragment != NULL) xmlFree(fragment);
1428 fragment = xmlStrdup(ctxt->incTab[nr]->fragment);
1430 URL = xmlSaveUri(uri);
1433 if (ctxt->incTab != NULL)
1434 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1435 XML_XINCLUDE_HREF_URI,
1436 "invalid value URI %s\n", url);
1438 xmlXIncludeErr(ctxt, NULL,
1439 XML_XINCLUDE_HREF_URI,
1440 "invalid value URI %s\n", url);
1441 if (fragment != NULL)
1447 * Handling of references to the local document are done
1448 * directly through ctxt->doc.
1450 if ((URL[0] == 0) || (URL[0] == '#') ||
1451 ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
1457 * Prevent reloading twice the document.
1459 for (i = 0; i < ctxt->incNr; i++) {
1460 if ((xmlStrEqual(URL, ctxt->incTab[i]->URI)) &&
1461 (ctxt->incTab[i]->doc != NULL)) {
1462 doc = ctxt->incTab[i]->doc;
1463 #ifdef DEBUG_XINCLUDE
1464 printf("Already loaded %s\n", URL);
1473 #ifdef DEBUG_XINCLUDE
1474 printf("loading %s\n", URL);
1476 #ifdef LIBXML_XPTR_ENABLED
1478 * If this is an XPointer evaluation, we want to assure that
1479 * all entities have been resolved prior to processing the
1480 * referenced document
1482 saveFlags = ctxt->parseFlags;
1483 if (fragment != NULL) { /* if this is an XPointer eval */
1484 ctxt->parseFlags |= XML_PARSE_NOENT;
1488 doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
1489 #ifdef LIBXML_XPTR_ENABLED
1490 ctxt->parseFlags = saveFlags;
1494 if (fragment != NULL)
1498 ctxt->incTab[nr]->doc = doc;
1500 * It's possible that the requested URL has been mapped to a
1501 * completely different location (e.g. through a catalog entry).
1502 * To check for this, we compare the URL with that of the doc
1503 * and change it if they disagree (bug 146988).
1505 if (!xmlStrEqual(URL, doc->URL)) {
1507 URL = xmlStrdup(doc->URL);
1509 for (i = nr + 1; i < ctxt->incNr; i++) {
1510 if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
1511 ctxt->incTab[nr]->count++;
1512 #ifdef DEBUG_XINCLUDE
1513 printf("Increasing %s count since reused\n", URL);
1520 * Make sure we have all entities fixed up
1522 xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
1525 * We don't need the DTD anymore, free up space
1526 if (doc->intSubset != NULL) {
1527 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1528 xmlFreeNode((xmlNodePtr) doc->intSubset);
1529 doc->intSubset = NULL;
1531 if (doc->extSubset != NULL) {
1532 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1533 xmlFreeNode((xmlNodePtr) doc->extSubset);
1534 doc->extSubset = NULL;
1537 xmlXIncludeRecurseDoc(ctxt, doc, URL);
1540 if (fragment == NULL) {
1542 * Add the top children list as the replacement copy.
1546 /* Hopefully a DTD declaration won't be copied from
1547 * the same document */
1548 ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
1550 ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
1551 doc, doc->children);
1554 #ifdef LIBXML_XPTR_ENABLED
1557 * Computes the XPointer expression and make a copy used
1558 * as the replacement copy.
1560 xmlXPathObjectPtr xptr;
1561 xmlXPathContextPtr xptrctxt;
1565 xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
1568 xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
1570 if (xptrctxt == NULL) {
1571 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1572 XML_XINCLUDE_XPTR_FAILED,
1573 "could not create XPointer context\n", NULL);
1578 xptr = xmlXPtrEval(fragment, xptrctxt);
1580 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1581 XML_XINCLUDE_XPTR_FAILED,
1582 "XPointer evaluation failed: #%s\n",
1584 xmlXPathFreeContext(xptrctxt);
1589 switch (xptr->type) {
1590 case XPATH_UNDEFINED:
1596 case XPATH_XSLT_TREE:
1597 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1598 XML_XINCLUDE_XPTR_RESULT,
1599 "XPointer is not a range: #%s\n",
1601 xmlXPathFreeContext(xptrctxt);
1606 if ((xptr->nodesetval == NULL) ||
1607 (xptr->nodesetval->nodeNr <= 0)) {
1608 xmlXPathFreeContext(xptrctxt);
1615 case XPATH_LOCATIONSET:
1618 set = xptr->nodesetval;
1620 for (i = 0;i < set->nodeNr;i++) {
1621 if (set->nodeTab[i] == NULL)
1623 switch (set->nodeTab[i]->type) {
1624 case XML_ELEMENT_NODE:
1626 case XML_CDATA_SECTION_NODE:
1627 case XML_ENTITY_REF_NODE:
1628 case XML_ENTITY_NODE:
1630 case XML_COMMENT_NODE:
1631 case XML_DOCUMENT_NODE:
1632 case XML_HTML_DOCUMENT_NODE:
1633 #ifdef LIBXML_DOCB_ENABLED
1634 case XML_DOCB_DOCUMENT_NODE:
1638 case XML_ATTRIBUTE_NODE:
1639 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1640 XML_XINCLUDE_XPTR_RESULT,
1641 "XPointer selects an attribute: #%s\n",
1643 set->nodeTab[i] = NULL;
1645 case XML_NAMESPACE_DECL:
1646 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1647 XML_XINCLUDE_XPTR_RESULT,
1648 "XPointer selects a namespace: #%s\n",
1650 set->nodeTab[i] = NULL;
1652 case XML_DOCUMENT_TYPE_NODE:
1653 case XML_DOCUMENT_FRAG_NODE:
1654 case XML_NOTATION_NODE:
1656 case XML_ELEMENT_DECL:
1657 case XML_ATTRIBUTE_DECL:
1658 case XML_ENTITY_DECL:
1659 case XML_XINCLUDE_START:
1660 case XML_XINCLUDE_END:
1661 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1662 XML_XINCLUDE_XPTR_RESULT,
1663 "XPointer selects unexpected nodes: #%s\n",
1665 set->nodeTab[i] = NULL;
1666 set->nodeTab[i] = NULL;
1672 ctxt->incTab[nr]->xptr = xptr;
1673 ctxt->incTab[nr]->inc = NULL;
1675 ctxt->incTab[nr]->inc =
1676 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
1677 xmlXPathFreeObject(xptr);
1679 xmlXPathFreeContext(xptrctxt);
1685 * Do the xml:base fixup if needed
1687 if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/')) &&
1688 (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
1689 (!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
1695 * The base is only adjusted if "necessary", i.e. if the xinclude node
1696 * has a base specified, or the URL is relative
1698 base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
1702 * No xml:base on the xinclude node, so we check whether the
1703 * URI base is different than (relative to) the context base
1705 curBase = xmlBuildRelativeURI(URL, ctxt->base);
1706 if (curBase == NULL) { /* Error return */
1707 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1708 XML_XINCLUDE_HREF_URI,
1709 "trying to build relative URI from %s\n", URL);
1711 /* If the URI doesn't contain a slash, it's not relative */
1712 if (!xmlStrchr(curBase, (xmlChar) '/'))
1718 if (base != NULL) { /* Adjustment may be needed */
1719 node = ctxt->incTab[nr]->inc;
1720 while (node != NULL) {
1721 /* Only work on element nodes */
1722 if (node->type == XML_ELEMENT_NODE) {
1723 curBase = xmlNodeGetBase(node->doc, node);
1724 /* If no current base, set it */
1725 if (curBase == NULL) {
1726 xmlNodeSetBase(node, base);
1729 * If the current base is the same as the
1730 * URL of the document, then reset it to be
1731 * the specified xml:base or the relative URI
1733 if (xmlStrEqual(curBase, node->doc->URL)) {
1734 xmlNodeSetBase(node, base);
1737 * If the element already has an xml:base
1738 * set, then relativise it if necessary
1741 xmlBase = xmlGetNsProp(node,
1744 if (xmlBase != NULL) {
1746 relBase = xmlBuildURI(xmlBase, base);
1747 if (relBase == NULL) { /* error */
1748 xmlXIncludeErr(ctxt,
1749 ctxt->incTab[nr]->ref,
1750 XML_XINCLUDE_HREF_URI,
1751 "trying to rebuild base from %s\n",
1754 xmlNodeSetBase(node, relBase);
1768 if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
1769 (ctxt->incTab[nr]->count <= 1)) {
1770 #ifdef DEBUG_XINCLUDE
1771 printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
1773 xmlFreeDoc(ctxt->incTab[nr]->doc);
1774 ctxt->incTab[nr]->doc = NULL;
1781 * xmlXIncludeLoadTxt:
1782 * @ctxt: the XInclude context
1783 * @url: the associated URL
1784 * @nr: the xinclude node number
1786 * Load the content, and store the result in the XInclude context
1788 * Returns 0 in case of success, -1 in case of failure
1791 xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
1792 xmlParserInputBufferPtr buf;
1797 xmlChar *encoding = NULL;
1798 xmlCharEncoding enc = (xmlCharEncoding) 0;
1799 xmlParserCtxtPtr pctxt;
1800 xmlParserInputPtr inputStream;
1803 * Check the URL and remove any fragment identifier
1805 uri = xmlParseURI((const char *)url);
1807 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1808 "invalid value URI %s\n", url);
1811 if (uri->fragment != NULL) {
1812 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
1813 "fragment identifier forbidden for text: %s\n",
1814 (const xmlChar *) uri->fragment);
1818 URL = xmlSaveUri(uri);
1821 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
1822 "invalid value URI %s\n", url);
1827 * Handling of references to the local document are done
1828 * directly through ctxt->doc.
1831 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1832 XML_XINCLUDE_TEXT_DOCUMENT,
1833 "text serialization of document not available\n", NULL);
1839 * Prevent reloading twice the document.
1841 for (i = 0; i < ctxt->txtNr; i++) {
1842 if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
1843 node = xmlCopyNode(ctxt->txtTab[i], 1);
1848 * Try to get the encoding if available
1850 if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
1851 encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
1853 if (encoding != NULL) {
1855 * TODO: we should not have to remap to the xmlCharEncoding
1856 * predefined set, a better interface than
1857 * xmlParserInputBufferCreateFilename should allow any
1858 * encoding supported by iconv
1860 enc = xmlParseCharEncoding((const char *) encoding);
1861 if (enc == XML_CHAR_ENCODING_ERROR) {
1862 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1863 XML_XINCLUDE_UNKNOWN_ENCODING,
1864 "encoding %s not supported\n", encoding);
1875 pctxt = xmlNewParserCtxt();
1876 inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt);
1877 if(inputStream == NULL) {
1878 xmlFreeParserCtxt(pctxt);
1882 buf = inputStream->buf;
1884 xmlFreeInputStream (inputStream);
1885 xmlFreeParserCtxt(pctxt);
1890 xmlCharEncCloseFunc(buf->encoder);
1891 buf->encoder = xmlGetCharEncodingHandler(enc);
1892 node = xmlNewText(NULL);
1895 * Scan all chars from the resource and add the to the node
1897 while (xmlParserInputBufferRead(buf, 128) > 0) {
1899 const xmlChar *content;
1901 content = xmlBufferContent(buf->buffer);
1902 len = xmlBufferLength(buf->buffer);
1903 for (i = 0;i < len;) {
1907 cur = xmlStringCurrentChar(NULL, &content[i], &l);
1908 if (!IS_CHAR(cur)) {
1909 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
1910 XML_XINCLUDE_INVALID_CHAR,
1911 "%s contains invalid char\n", URL);
1912 xmlFreeParserInputBuffer(buf);
1916 xmlNodeAddContentLen(node, &content[i], l);
1920 xmlBufferShrink(buf->buffer, len);
1922 xmlFreeParserCtxt(pctxt);
1923 xmlXIncludeAddTxt(ctxt, node, URL);
1924 xmlFreeInputStream(inputStream);
1928 * Add the element as the replacement copy.
1930 ctxt->incTab[nr]->inc = node;
1936 * xmlXIncludeLoadFallback:
1937 * @ctxt: the XInclude context
1938 * @fallback: the fallback node
1939 * @nr: the xinclude node number
1941 * Load the content of the fallback node, and store the result
1942 * in the XInclude context
1944 * Returns 0 in case of success, -1 in case of failure
1947 xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
1948 xmlXIncludeCtxtPtr newctxt;
1951 if ((fallback == NULL) || (ctxt == NULL))
1953 if (fallback->children != NULL) {
1955 * It's possible that the fallback also has 'includes'
1956 * (Bug 129969), so we re-process the fallback just in case
1958 newctxt = xmlXIncludeNewContext(ctxt->doc);
1959 if (newctxt == NULL)
1961 newctxt->_private = ctxt->_private;
1962 newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
1963 xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
1964 ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
1965 if (ctxt->nbErrors > 0)
1968 ret = 0; /* xmlXIncludeDoProcess can return +ve number */
1969 xmlXIncludeFreeContext(newctxt);
1971 ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
1972 fallback->children);
1974 ctxt->incTab[nr]->inc = NULL;
1975 ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
1980 /************************************************************************
1982 * XInclude Processing *
1984 ************************************************************************/
1987 * xmlXIncludePreProcessNode:
1988 * @ctxt: an XInclude context
1989 * @node: an XInclude node
1991 * Implement the XInclude preprocessing, currently just adding the element
1992 * for further processing.
1994 * Returns the result list or NULL in case of error
1997 xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
1998 xmlXIncludeAddNode(ctxt, node);
2003 * xmlXIncludeLoadNode:
2004 * @ctxt: an XInclude context
2005 * @nr: the node number
2007 * Find and load the infoset replacement for the given node.
2009 * Returns 0 if substitution succeeded, -1 if some processing failed
2012 xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, int nr) {
2019 int xml = 1; /* default Issue 64 */
2024 if ((nr < 0) || (nr >= ctxt->incNr))
2026 cur = ctxt->incTab[nr]->ref;
2031 * read the attributes
2033 href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
2035 href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
2039 parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
2040 if (parse != NULL) {
2041 if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
2043 else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
2046 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2047 XML_XINCLUDE_PARSE_VALUE,
2048 "invalid value %s for 'parse'\n", parse);
2060 base = xmlNodeGetBase(ctxt->doc, cur);
2062 URI = xmlBuildURI(href, ctxt->doc->URL);
2064 URI = xmlBuildURI(href, base);
2070 * Some escaping may be needed
2072 escbase = xmlURIEscape(base);
2073 eschref = xmlURIEscape(href);
2074 URI = xmlBuildURI(eschref, escbase);
2075 if (escbase != NULL)
2077 if (eschref != NULL)
2081 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2082 XML_XINCLUDE_HREF_URI, "failed build URL\n", NULL);
2091 #ifdef DEBUG_XINCLUDE
2092 xmlGenericError(xmlGenericErrorContext, "parse: %s\n",
2093 xml ? "xml": "text");
2094 xmlGenericError(xmlGenericErrorContext, "URI: %s\n", URI);
2098 * Save the base for this include (saving the current one)
2100 oldBase = ctxt->base;
2104 ret = xmlXIncludeLoadDoc(ctxt, URI, nr);
2105 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2107 ret = xmlXIncludeLoadTxt(ctxt, URI, nr);
2111 * Restore the original base before checking for fallback
2113 ctxt->base = oldBase;
2116 xmlNodePtr children;
2119 * Time to try a fallback if availble
2121 #ifdef DEBUG_XINCLUDE
2122 xmlGenericError(xmlGenericErrorContext, "error looking for fallback\n");
2124 children = cur->children;
2125 while (children != NULL) {
2126 if ((children->type == XML_ELEMENT_NODE) &&
2127 (children->ns != NULL) &&
2128 (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
2129 ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
2130 (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
2131 ret = xmlXIncludeLoadFallback(ctxt, children, nr);
2135 children = children->next;
2139 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2140 XML_XINCLUDE_NO_FALLBACK,
2141 "could not load %s, and no fallback was found\n",
2160 * xmlXIncludeIncludeNode:
2161 * @ctxt: an XInclude context
2162 * @nr: the node number
2164 * Inplement the infoset replacement for the given node
2166 * Returns 0 if substitution succeeded, -1 if some processing failed
2169 xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, int nr) {
2170 xmlNodePtr cur, end, list, tmp;
2174 if ((nr < 0) || (nr >= ctxt->incNr))
2176 cur = ctxt->incTab[nr]->ref;
2181 * If we stored an XPointer a late computation may be needed
2183 if ((ctxt->incTab[nr]->inc == NULL) &&
2184 (ctxt->incTab[nr]->xptr != NULL)) {
2185 ctxt->incTab[nr]->inc =
2186 xmlXIncludeCopyXPointer(ctxt, ctxt->doc, ctxt->doc,
2187 ctxt->incTab[nr]->xptr);
2188 xmlXPathFreeObject(ctxt->incTab[nr]->xptr);
2189 ctxt->incTab[nr]->xptr = NULL;
2191 list = ctxt->incTab[nr]->inc;
2192 ctxt->incTab[nr]->inc = NULL;
2195 * Check against the risk of generating a multi-rooted document
2197 if ((cur->parent != NULL) &&
2198 (cur->parent->type != XML_ELEMENT_NODE)) {
2202 while (tmp != NULL) {
2203 if (tmp->type == XML_ELEMENT_NODE)
2208 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2209 XML_XINCLUDE_MULTIPLE_ROOT,
2210 "XInclude error: would result in multiple root nodes\n",
2216 if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
2218 * Add the list of nodes
2220 while (list != NULL) {
2224 xmlAddPrevSibling(cur, end);
2230 * Change the current node as an XInclude start one, and add an
2233 cur->type = XML_XINCLUDE_START;
2234 end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
2236 xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
2237 XML_XINCLUDE_BUILD_FAILED,
2238 "failed to build node\n", NULL);
2241 end->type = XML_XINCLUDE_END;
2242 xmlAddNextSibling(cur, end);
2245 * Add the list of nodes
2247 while (list != NULL) {
2251 xmlAddPrevSibling(end, cur);
2260 * xmlXIncludeTestNode:
2261 * @ctxt: the XInclude processing context
2262 * @node: an XInclude node
2264 * test if the node is an XInclude node
2266 * Returns 1 true, 0 otherwise
2269 xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2272 if (node->type != XML_ELEMENT_NODE)
2274 if (node->ns == NULL)
2276 if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
2277 (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
2278 if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
2279 if (ctxt->legacy == 0) {
2280 #if 0 /* wait for the XML Core Working Group to get something stable ! */
2281 xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
2282 "Deprecated XInclude namespace found, use %s",
2288 if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
2289 xmlNodePtr child = node->children;
2290 int nb_fallback = 0;
2292 while (child != NULL) {
2293 if ((child->type == XML_ELEMENT_NODE) &&
2294 (child->ns != NULL) &&
2295 ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
2296 (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
2297 if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
2298 xmlXIncludeErr(ctxt, node,
2299 XML_XINCLUDE_INCLUDE_IN_INCLUDE,
2300 "%s has an 'include' child\n",
2304 if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
2308 child = child->next;
2310 if (nb_fallback > 1) {
2311 xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
2312 "%s has multiple fallback children\n",
2318 if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
2319 if ((node->parent == NULL) ||
2320 (node->parent->type != XML_ELEMENT_NODE) ||
2321 (node->parent->ns == NULL) ||
2322 ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
2323 (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
2324 (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
2325 xmlXIncludeErr(ctxt, node,
2326 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
2327 "%s is not the child of an 'include'\n",
2336 * xmlXIncludeDoProcess:
2337 * @ctxt: the XInclude processing context
2338 * @doc: an XML document
2339 * @tree: the top of the tree to process
2341 * Implement the XInclude substitution on the XML document @doc
2343 * Returns 0 if no substitution were done, -1 if some processing failed
2344 * or the number of substitutions done.
2347 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr tree) {
2352 if ((doc == NULL) || (tree == NULL))
2357 if (doc->URL != NULL) {
2358 ret = xmlXIncludeURLPush(ctxt, doc->URL);
2362 start = ctxt->incNr;
2365 * First phase: lookup the elements in the document
2368 if (xmlXIncludeTestNode(ctxt, cur) == 1)
2369 xmlXIncludePreProcessNode(ctxt, cur);
2370 while ((cur != NULL) && (cur != tree->parent)) {
2371 /* TODO: need to work on entities -> stack */
2372 if ((cur->children != NULL) &&
2373 (cur->children->type != XML_ENTITY_DECL) &&
2374 (cur->children->type != XML_XINCLUDE_START) &&
2375 (cur->children->type != XML_XINCLUDE_END)) {
2376 cur = cur->children;
2377 if (xmlXIncludeTestNode(ctxt, cur))
2378 xmlXIncludePreProcessNode(ctxt, cur);
2379 } else if (cur->next != NULL) {
2381 if (xmlXIncludeTestNode(ctxt, cur))
2382 xmlXIncludePreProcessNode(ctxt, cur);
2388 if ((cur == NULL) || (cur == tree->parent))
2390 if (cur->next != NULL) {
2392 if (xmlXIncludeTestNode(ctxt, cur))
2393 xmlXIncludePreProcessNode(ctxt, cur);
2396 } while (cur != NULL);
2401 * Second Phase : collect the infosets fragments
2403 for (i = start;i < ctxt->incNr; i++) {
2404 xmlXIncludeLoadNode(ctxt, i);
2409 * Third phase: extend the original document infoset.
2411 * Originally we bypassed the inclusion if there were any errors
2412 * encountered on any of the XIncludes. A bug was raised (bug
2413 * 132588) requesting that we output the XIncludes without error,
2414 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2415 * give some other problems in the future, but for now it seems to
2419 for (i = ctxt->incBase;i < ctxt->incNr; i++) {
2420 if ((ctxt->incTab[i]->inc != NULL) ||
2421 (ctxt->incTab[i]->xptr != NULL) ||
2422 (ctxt->incTab[i]->emptyFb != 0)) /* (empty fallback) */
2423 xmlXIncludeIncludeNode(ctxt, i);
2426 if (doc->URL != NULL)
2427 xmlXIncludeURLPop(ctxt);
2432 * xmlXIncludeSetFlags:
2433 * @ctxt: an XInclude processing context
2434 * @flags: a set of xmlParserOption used for parsing XML includes
2436 * Set the flags used for further processing of XML resources.
2438 * Returns 0 in case of success and -1 in case of error.
2441 xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
2444 ctxt->parseFlags = flags;
2449 * xmlXIncludeProcessTreeFlagsData:
2450 * @tree: an XML node
2451 * @flags: a set of xmlParserOption used for parsing XML includes
2452 * @data: application data that will be passed to the parser context
2453 * in the _private field of the parser context(s)
2455 * Implement the XInclude substitution on the XML node @tree
2457 * Returns 0 if no substitution were done, -1 if some processing failed
2458 * or the number of substitutions done.
2462 xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
2463 xmlXIncludeCtxtPtr ctxt;
2466 if ((tree == NULL) || (tree->doc == NULL))
2469 ctxt = xmlXIncludeNewContext(tree->doc);
2472 ctxt->_private = data;
2473 ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
2474 xmlXIncludeSetFlags(ctxt, flags);
2475 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2476 if ((ret >= 0) && (ctxt->nbErrors > 0))
2479 xmlXIncludeFreeContext(ctxt);
2484 * xmlXIncludeProcessFlagsData:
2485 * @doc: an XML document
2486 * @flags: a set of xmlParserOption used for parsing XML includes
2487 * @data: application data that will be passed to the parser context
2488 * in the _private field of the parser context(s)
2490 * Implement the XInclude substitution on the XML document @doc
2492 * Returns 0 if no substitution were done, -1 if some processing failed
2493 * or the number of substitutions done.
2496 xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
2501 tree = xmlDocGetRootElement(doc);
2504 return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
2508 * xmlXIncludeProcessFlags:
2509 * @doc: an XML document
2510 * @flags: a set of xmlParserOption used for parsing XML includes
2512 * Implement the XInclude substitution on the XML document @doc
2514 * Returns 0 if no substitution were done, -1 if some processing failed
2515 * or the number of substitutions done.
2518 xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
2519 return xmlXIncludeProcessFlagsData(doc, flags, NULL);
2523 * xmlXIncludeProcess:
2524 * @doc: an XML document
2526 * Implement the XInclude substitution on the XML document @doc
2528 * Returns 0 if no substitution were done, -1 if some processing failed
2529 * or the number of substitutions done.
2532 xmlXIncludeProcess(xmlDocPtr doc) {
2533 return(xmlXIncludeProcessFlags(doc, 0));
2537 * xmlXIncludeProcessTreeFlags:
2538 * @tree: a node in an XML document
2539 * @flags: a set of xmlParserOption used for parsing XML includes
2541 * Implement the XInclude substitution for the given subtree
2543 * Returns 0 if no substitution were done, -1 if some processing failed
2544 * or the number of substitutions done.
2547 xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
2548 xmlXIncludeCtxtPtr ctxt;
2551 if ((tree == NULL) || (tree->doc == NULL))
2553 ctxt = xmlXIncludeNewContext(tree->doc);
2556 ctxt->base = xmlNodeGetBase(tree->doc, tree);
2557 xmlXIncludeSetFlags(ctxt, flags);
2558 ret = xmlXIncludeDoProcess(ctxt, tree->doc, tree);
2559 if ((ret >= 0) && (ctxt->nbErrors > 0))
2562 xmlXIncludeFreeContext(ctxt);
2567 * xmlXIncludeProcessTree:
2568 * @tree: a node in an XML document
2570 * Implement the XInclude substitution for the given subtree
2572 * Returns 0 if no substitution were done, -1 if some processing failed
2573 * or the number of substitutions done.
2576 xmlXIncludeProcessTree(xmlNodePtr tree) {
2577 return(xmlXIncludeProcessTreeFlags(tree, 0));
2581 * xmlXIncludeProcessNode:
2582 * @ctxt: an existing XInclude context
2583 * @node: a node in an XML document
2585 * Implement the XInclude substitution for the given subtree reusing
2586 * the informations and data coming from the given context.
2588 * Returns 0 if no substitution were done, -1 if some processing failed
2589 * or the number of substitutions done.
2592 xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
2595 if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL))
2597 ret = xmlXIncludeDoProcess(ctxt, node->doc, node);
2598 if ((ret >= 0) && (ctxt->nbErrors > 0))
2603 #else /* !LIBXML_XINCLUDE_ENABLED */
2605 #define bottom_xinclude
2606 #include "elfgcchack.h"