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/xpath.h>
21 #include <libxml/xpointer.h>
22 #include <libxml/parserInternals.h>
23 #include <libxml/xmlerror.h>
24 #include <libxml/encoding.h>
25 #include <libxml/globals.h>
27 #ifdef LIBXML_XINCLUDE_ENABLED
28 #include <libxml/xinclude.h>
32 #define XINCLUDE_MAX_DEPTH 40
34 /* #define DEBUG_XINCLUDE */
36 #ifdef LIBXML_DEBUG_ENABLED
37 #include <libxml/debugXML.h>
41 /************************************************************************
43 * XInclude context handling *
45 ************************************************************************/
50 typedef xmlChar
*xmlURL
;
52 typedef struct _xmlXIncludeRef xmlXIncludeRef
;
53 typedef xmlXIncludeRef
*xmlXIncludeRefPtr
;
54 struct _xmlXIncludeRef
{
55 xmlChar
*URI
; /* the fully resolved resource URL */
56 xmlChar
*fragment
; /* the fragment in the URI */
57 xmlDocPtr doc
; /* the parsed document */
58 xmlNodePtr ref
; /* the node making the reference in the source */
59 xmlNodePtr inc
; /* the included copy */
60 int xml
; /* xml or txt */
61 int count
; /* how many refs use that specific doc */
62 xmlXPathObjectPtr xptr
; /* the xpointer if needed */
63 int emptyFb
; /* flag to show fallback empty */
66 struct _xmlXIncludeCtxt
{
67 xmlDocPtr doc
; /* the source document */
68 int incBase
; /* the first include for this document */
69 int incNr
; /* number of includes */
70 int incMax
; /* size of includes tab */
71 xmlXIncludeRefPtr
*incTab
; /* array of included references */
73 int txtNr
; /* number of unparsed documents */
74 int txtMax
; /* size of unparsed documents tab */
75 xmlNodePtr
*txtTab
; /* array of unparsed text nodes */
76 xmlURL
*txturlTab
; /* array of unparsed text URLs */
78 xmlChar
* url
; /* the current URL processed */
79 int urlNr
; /* number of URLs stacked */
80 int urlMax
; /* size of URL stack */
81 xmlChar
* *urlTab
; /* URL stack */
83 int nbErrors
; /* the number of errors detected */
84 int legacy
; /* using XINCLUDE_OLD_NS */
85 int parseFlags
; /* the flags used for parsing XML documents */
86 xmlChar
* base
; /* the current xml:base */
88 void *_private
; /* application data */
92 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr doc
, xmlNodePtr tree
);
95 /************************************************************************
97 * XInclude error handler *
99 ************************************************************************/
102 * xmlXIncludeErrMemory:
103 * @extra: extra information
105 * Handle an out of memory condition
108 xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
,
113 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, node
, XML_FROM_XINCLUDE
,
114 XML_ERR_NO_MEMORY
, XML_ERR_ERROR
, NULL
, 0,
115 extra
, NULL
, NULL
, 0, 0,
116 "Memory allocation failed : %s\n", extra
);
121 * @ctxt: the XInclude context
122 * @node: the context node
123 * @msg: the error message
124 * @extra: extra information
126 * Handle an XInclude error
128 static void LIBXML_ATTR_FORMAT(4,0)
129 xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
, int error
,
130 const char *msg
, const xmlChar
*extra
)
134 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, node
, XML_FROM_XINCLUDE
,
135 error
, XML_ERR_ERROR
, NULL
, 0,
136 (const char *) extra
, NULL
, NULL
, 0, 0,
137 msg
, (const char *) extra
);
143 * @ctxt: the XInclude context
144 * @node: the context node
145 * @msg: the error message
146 * @extra: extra information
148 * Emit an XInclude warning.
150 static void LIBXML_ATTR_FORMAT(4,0)
151 xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
, int error
,
152 const char *msg
, const xmlChar
*extra
)
154 __xmlRaiseError(NULL
, NULL
, NULL
, ctxt
, node
, XML_FROM_XINCLUDE
,
155 error
, XML_ERR_WARNING
, NULL
, 0,
156 (const char *) extra
, NULL
, NULL
, 0, 0,
157 msg
, (const char *) extra
);
162 * xmlXIncludeGetProp:
163 * @ctxt: the XInclude context
165 * @name: the attribute name
167 * Get an XInclude attribute
169 * Returns the value (to be freed) or NULL if not found
172 xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr cur
,
173 const xmlChar
*name
) {
176 ret
= xmlGetNsProp(cur
, XINCLUDE_NS
, name
);
179 if (ctxt
->legacy
!= 0) {
180 ret
= xmlGetNsProp(cur
, XINCLUDE_OLD_NS
, name
);
184 ret
= xmlGetProp(cur
, name
);
188 * xmlXIncludeFreeRef:
189 * @ref: the XInclude reference
191 * Free an XInclude reference
194 xmlXIncludeFreeRef(xmlXIncludeRefPtr ref
) {
197 #ifdef DEBUG_XINCLUDE
198 xmlGenericError(xmlGenericErrorContext
, "Freeing ref\n");
200 if (ref
->doc
!= NULL
) {
201 #ifdef DEBUG_XINCLUDE
202 xmlGenericError(xmlGenericErrorContext
, "Freeing doc %s\n", ref
->URI
);
204 xmlFreeDoc(ref
->doc
);
206 if (ref
->URI
!= NULL
)
208 if (ref
->fragment
!= NULL
)
209 xmlFree(ref
->fragment
);
210 if (ref
->xptr
!= NULL
)
211 xmlXPathFreeObject(ref
->xptr
);
217 * @ctxt: the XInclude context
218 * @URI: the resource URI
220 * Creates a new reference within an XInclude context
222 * Returns the new set
224 static xmlXIncludeRefPtr
225 xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt
, const xmlChar
*URI
,
227 xmlXIncludeRefPtr ret
;
229 #ifdef DEBUG_XINCLUDE
230 xmlGenericError(xmlGenericErrorContext
, "New ref %s\n", URI
);
232 ret
= (xmlXIncludeRefPtr
) xmlMalloc(sizeof(xmlXIncludeRef
));
234 xmlXIncludeErrMemory(ctxt
, ref
, "growing XInclude context");
237 memset(ret
, 0, sizeof(xmlXIncludeRef
));
241 ret
->URI
= xmlStrdup(URI
);
242 ret
->fragment
= NULL
;
248 if (ctxt
->incMax
== 0) {
250 ctxt
->incTab
= (xmlXIncludeRefPtr
*) xmlMalloc(ctxt
->incMax
*
251 sizeof(ctxt
->incTab
[0]));
252 if (ctxt
->incTab
== NULL
) {
253 xmlXIncludeErrMemory(ctxt
, ref
, "growing XInclude context");
254 xmlXIncludeFreeRef(ret
);
258 if (ctxt
->incNr
>= ctxt
->incMax
) {
260 ctxt
->incTab
= (xmlXIncludeRefPtr
*) xmlRealloc(ctxt
->incTab
,
261 ctxt
->incMax
* sizeof(ctxt
->incTab
[0]));
262 if (ctxt
->incTab
== NULL
) {
263 xmlXIncludeErrMemory(ctxt
, ref
, "growing XInclude context");
264 xmlXIncludeFreeRef(ret
);
268 ctxt
->incTab
[ctxt
->incNr
++] = ret
;
273 * xmlXIncludeNewContext:
274 * @doc: an XML Document
276 * Creates a new XInclude context
278 * Returns the new set
281 xmlXIncludeNewContext(xmlDocPtr doc
) {
282 xmlXIncludeCtxtPtr ret
;
284 #ifdef DEBUG_XINCLUDE
285 xmlGenericError(xmlGenericErrorContext
, "New context\n");
289 ret
= (xmlXIncludeCtxtPtr
) xmlMalloc(sizeof(xmlXIncludeCtxt
));
291 xmlXIncludeErrMemory(NULL
, (xmlNodePtr
) doc
,
292 "creating XInclude context");
295 memset(ret
, 0, sizeof(xmlXIncludeCtxt
));
306 * xmlXIncludeURLPush:
307 * @ctxt: the parser context
310 * Pushes a new url on top of the url stack
312 * Returns -1 in case of error, the index in the stack otherwise
315 xmlXIncludeURLPush(xmlXIncludeCtxtPtr ctxt
,
316 const xmlChar
*value
)
318 if (ctxt
->urlNr
> XINCLUDE_MAX_DEPTH
) {
319 xmlXIncludeErr(ctxt
, NULL
, XML_XINCLUDE_RECURSION
,
320 "detected a recursion in %s\n", value
);
323 if (ctxt
->urlTab
== NULL
) {
326 ctxt
->urlTab
= (xmlChar
* *) xmlMalloc(
327 ctxt
->urlMax
* sizeof(ctxt
->urlTab
[0]));
328 if (ctxt
->urlTab
== NULL
) {
329 xmlXIncludeErrMemory(ctxt
, NULL
, "adding URL");
333 if (ctxt
->urlNr
>= ctxt
->urlMax
) {
336 (xmlChar
* *) xmlRealloc(ctxt
->urlTab
,
338 sizeof(ctxt
->urlTab
[0]));
339 if (ctxt
->urlTab
== NULL
) {
340 xmlXIncludeErrMemory(ctxt
, NULL
, "adding URL");
344 ctxt
->url
= ctxt
->urlTab
[ctxt
->urlNr
] = xmlStrdup(value
);
345 return (ctxt
->urlNr
++);
350 * @ctxt: the parser context
352 * Pops the top URL from the URL stack
355 xmlXIncludeURLPop(xmlXIncludeCtxtPtr ctxt
)
359 if (ctxt
->urlNr
<= 0)
363 ctxt
->url
= ctxt
->urlTab
[ctxt
->urlNr
- 1];
366 ret
= ctxt
->urlTab
[ctxt
->urlNr
];
367 ctxt
->urlTab
[ctxt
->urlNr
] = NULL
;
373 * xmlXIncludeFreeContext:
374 * @ctxt: the XInclude context
376 * Free an XInclude context
379 xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt
) {
382 #ifdef DEBUG_XINCLUDE
383 xmlGenericError(xmlGenericErrorContext
, "Freeing context\n");
387 while (ctxt
->urlNr
> 0)
388 xmlXIncludeURLPop(ctxt
);
389 if (ctxt
->urlTab
!= NULL
)
390 xmlFree(ctxt
->urlTab
);
391 for (i
= 0;i
< ctxt
->incNr
;i
++) {
392 if (ctxt
->incTab
[i
] != NULL
)
393 xmlXIncludeFreeRef(ctxt
->incTab
[i
]);
395 if (ctxt
->txturlTab
!= NULL
) {
396 for (i
= 0;i
< ctxt
->txtNr
;i
++) {
397 if (ctxt
->txturlTab
[i
] != NULL
)
398 xmlFree(ctxt
->txturlTab
[i
]);
401 if (ctxt
->incTab
!= NULL
)
402 xmlFree(ctxt
->incTab
);
403 if (ctxt
->txtTab
!= NULL
)
404 xmlFree(ctxt
->txtTab
);
405 if (ctxt
->txturlTab
!= NULL
)
406 xmlFree(ctxt
->txturlTab
);
407 if (ctxt
->base
!= NULL
) {
414 * xmlXIncludeParseFile:
415 * @ctxt: the XInclude context
416 * @URL: the URL or file path
418 * parse a document for XInclude
421 xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt
, const char *URL
) {
423 xmlParserCtxtPtr pctxt
;
424 xmlParserInputPtr inputStream
;
428 pctxt
= xmlNewParserCtxt();
430 xmlXIncludeErrMemory(ctxt
, NULL
, "cannot allocate parser context");
435 * pass in the application data to the parser context.
437 pctxt
->_private
= ctxt
->_private
;
440 * try to ensure that new documents included are actually
441 * built with the same dictionary as the including document.
443 if ((ctxt
->doc
!= NULL
) && (ctxt
->doc
->dict
!= NULL
)) {
444 if (pctxt
->dict
!= NULL
)
445 xmlDictFree(pctxt
->dict
);
446 pctxt
->dict
= ctxt
->doc
->dict
;
447 xmlDictReference(pctxt
->dict
);
450 xmlCtxtUseOptions(pctxt
, ctxt
->parseFlags
| XML_PARSE_DTDLOAD
);
452 inputStream
= xmlLoadExternalEntity(URL
, NULL
, pctxt
);
453 if (inputStream
== NULL
) {
454 xmlFreeParserCtxt(pctxt
);
458 inputPush(pctxt
, inputStream
);
460 if (pctxt
->directory
== NULL
)
461 pctxt
->directory
= xmlParserGetDirectory(URL
);
463 pctxt
->loadsubset
|= XML_DETECT_IDS
;
465 xmlParseDocument(pctxt
);
467 if (pctxt
->wellFormed
) {
472 if (pctxt
->myDoc
!= NULL
)
473 xmlFreeDoc(pctxt
->myDoc
);
476 xmlFreeParserCtxt(pctxt
);
482 * xmlXIncludeAddNode:
483 * @ctxt: the XInclude context
486 * Add a new node to process to an XInclude context
489 xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr cur
) {
490 xmlXIncludeRefPtr ref
;
493 xmlChar
*fragment
= NULL
;
498 int xml
= 1, i
; /* default Issue 64 */
507 #ifdef DEBUG_XINCLUDE
508 xmlGenericError(xmlGenericErrorContext
, "Add node\n");
511 * read the attributes
513 href
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_HREF
);
515 href
= xmlStrdup(BAD_CAST
""); /* @@@@ href is now optional */
519 if ((href
[0] == '#') || (href
[0] == 0))
521 parse
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_PARSE
);
523 if (xmlStrEqual(parse
, XINCLUDE_PARSE_XML
))
525 else if (xmlStrEqual(parse
, XINCLUDE_PARSE_TEXT
))
528 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_PARSE_VALUE
,
529 "invalid value %s for 'parse'\n", parse
);
541 base
= xmlNodeGetBase(ctxt
->doc
, cur
);
543 URI
= xmlBuildURI(href
, ctxt
->doc
->URL
);
545 URI
= xmlBuildURI(href
, base
);
551 * Some escaping may be needed
553 escbase
= xmlURIEscape(base
);
554 eschref
= xmlURIEscape(href
);
555 URI
= xmlBuildURI(eschref
, escbase
);
568 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_HREF_URI
,
569 "failed build URL\n", NULL
);
572 fragment
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_PARSE_XPOINTER
);
575 * Check the URL and remove any fragment identifier
577 uri
= xmlParseURI((const char *)URI
);
579 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_HREF_URI
,
580 "invalid value URI %s\n", URI
);
581 if (fragment
!= NULL
)
587 if (uri
->fragment
!= NULL
) {
588 if (ctxt
->legacy
!= 0) {
589 if (fragment
== NULL
) {
590 fragment
= (xmlChar
*) uri
->fragment
;
592 xmlFree(uri
->fragment
);
595 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_FRAGMENT_ID
,
596 "Invalid fragment identifier in URI %s use the xpointer attribute\n",
598 if (fragment
!= NULL
)
604 uri
->fragment
= NULL
;
606 URL
= xmlSaveUri(uri
);
610 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_HREF_URI
,
611 "invalid value URI %s\n", URI
);
612 if (fragment
!= NULL
)
618 * If local and xml then we need a fragment
620 if ((local
== 1) && (xml
== 1) &&
621 ((fragment
== NULL
) || (fragment
[0] == 0))) {
622 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_RECURSION
,
623 "detected a local recursion with no xpointer in %s\n",
625 if (fragment
!= NULL
)
631 * Check the URL against the stack for recursions
633 if ((!local
) && (xml
== 1)) {
634 for (i
= 0;i
< ctxt
->urlNr
;i
++) {
635 if (xmlStrEqual(URL
, ctxt
->urlTab
[i
])) {
636 xmlXIncludeErr(ctxt
, cur
, XML_XINCLUDE_RECURSION
,
637 "detected a recursion in %s\n", URL
);
643 ref
= xmlXIncludeNewRef(ctxt
, URL
, cur
);
647 ref
->fragment
= fragment
;
656 * xmlXIncludeRecurseDoc:
657 * @ctxt: the XInclude context
658 * @doc: the new document
659 * @url: the associated URL
661 * The XInclude recursive nature is handled at this point.
664 xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr doc
,
665 const xmlURL url ATTRIBUTE_UNUSED
) {
666 xmlXIncludeCtxtPtr newctxt
;
670 * Avoid recursion in already substitued resources
671 for (i = 0;i < ctxt->urlNr;i++) {
672 if (xmlStrEqual(doc->URL, ctxt->urlTab[i]))
677 #ifdef DEBUG_XINCLUDE
678 xmlGenericError(xmlGenericErrorContext
, "Recursing in doc %s\n", doc
->URL
);
681 * Handle recursion here.
684 newctxt
= xmlXIncludeNewContext(doc
);
685 if (newctxt
!= NULL
) {
687 * Copy the private user data
689 newctxt
->_private
= ctxt
->_private
;
691 * Copy the existing document set
693 newctxt
->incMax
= ctxt
->incMax
;
694 newctxt
->incNr
= ctxt
->incNr
;
695 newctxt
->incTab
= (xmlXIncludeRefPtr
*) xmlMalloc(newctxt
->incMax
*
696 sizeof(newctxt
->incTab
[0]));
697 if (newctxt
->incTab
== NULL
) {
698 xmlXIncludeErrMemory(ctxt
, (xmlNodePtr
) doc
, "processing doc");
705 newctxt
->urlMax
= ctxt
->urlMax
;
706 newctxt
->urlNr
= ctxt
->urlNr
;
707 newctxt
->urlTab
= ctxt
->urlTab
;
710 * Inherit the existing base
712 newctxt
->base
= xmlStrdup(ctxt
->base
);
715 * Inherit the documents already in use by other includes
717 newctxt
->incBase
= ctxt
->incNr
;
718 for (i
= 0;i
< ctxt
->incNr
;i
++) {
719 newctxt
->incTab
[i
] = ctxt
->incTab
[i
];
720 newctxt
->incTab
[i
]->count
++; /* prevent the recursion from
724 * The new context should also inherit the Parse Flags
727 newctxt
->parseFlags
= ctxt
->parseFlags
;
728 xmlXIncludeDoProcess(newctxt
, doc
, xmlDocGetRootElement(doc
));
729 for (i
= 0;i
< ctxt
->incNr
;i
++) {
730 newctxt
->incTab
[i
]->count
--;
731 newctxt
->incTab
[i
] = NULL
;
734 /* urlTab may have been reallocated */
735 ctxt
->urlTab
= newctxt
->urlTab
;
736 ctxt
->urlMax
= newctxt
->urlMax
;
740 newctxt
->urlTab
= NULL
;
742 xmlXIncludeFreeContext(newctxt
);
744 #ifdef DEBUG_XINCLUDE
745 xmlGenericError(xmlGenericErrorContext
, "Done recursing in doc %s\n", url
);
751 * @ctxt: the XInclude context
752 * @txt: the new text node
753 * @url: the associated URL
755 * Add a new txtument to the list
758 xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr txt
, const xmlURL url
) {
759 #ifdef DEBUG_XINCLUDE
760 xmlGenericError(xmlGenericErrorContext
, "Adding text %s\n", url
);
762 if (ctxt
->txtMax
== 0) {
764 ctxt
->txtTab
= (xmlNodePtr
*) xmlMalloc(ctxt
->txtMax
*
765 sizeof(ctxt
->txtTab
[0]));
766 if (ctxt
->txtTab
== NULL
) {
767 xmlXIncludeErrMemory(ctxt
, NULL
, "processing text");
770 ctxt
->txturlTab
= (xmlURL
*) xmlMalloc(ctxt
->txtMax
*
771 sizeof(ctxt
->txturlTab
[0]));
772 if (ctxt
->txturlTab
== NULL
) {
773 xmlXIncludeErrMemory(ctxt
, NULL
, "processing text");
777 if (ctxt
->txtNr
>= ctxt
->txtMax
) {
779 ctxt
->txtTab
= (xmlNodePtr
*) xmlRealloc(ctxt
->txtTab
,
780 ctxt
->txtMax
* sizeof(ctxt
->txtTab
[0]));
781 if (ctxt
->txtTab
== NULL
) {
782 xmlXIncludeErrMemory(ctxt
, NULL
, "processing text");
785 ctxt
->txturlTab
= (xmlURL
*) xmlRealloc(ctxt
->txturlTab
,
786 ctxt
->txtMax
* sizeof(ctxt
->txturlTab
[0]));
787 if (ctxt
->txturlTab
== NULL
) {
788 xmlXIncludeErrMemory(ctxt
, NULL
, "processing text");
792 ctxt
->txtTab
[ctxt
->txtNr
] = txt
;
793 ctxt
->txturlTab
[ctxt
->txtNr
] = xmlStrdup(url
);
797 /************************************************************************
799 * Node copy with specific semantic *
801 ************************************************************************/
804 xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr target
,
805 xmlDocPtr source
, xmlNodePtr elem
);
808 * xmlXIncludeCopyNode:
809 * @ctxt: the XInclude context
810 * @target: the document target
811 * @source: the document source
814 * Make a copy of the node while preserving the XInclude semantic
815 * of the Infoset copy
818 xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr target
,
819 xmlDocPtr source
, xmlNodePtr elem
) {
820 xmlNodePtr result
= NULL
;
822 if ((ctxt
== NULL
) || (target
== NULL
) || (source
== NULL
) ||
825 if (elem
->type
== XML_DTD_NODE
)
827 if (elem
->type
== XML_DOCUMENT_NODE
)
828 result
= xmlXIncludeCopyNodeList(ctxt
, target
, source
, elem
->children
);
830 result
= xmlDocCopyNode(elem
, target
, 1);
835 * xmlXIncludeCopyNodeList:
836 * @ctxt: the XInclude context
837 * @target: the document target
838 * @source: the document source
839 * @elem: the element list
841 * Make a copy of the node list while preserving the XInclude semantic
842 * of the Infoset copy
845 xmlXIncludeCopyNodeList(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr target
,
846 xmlDocPtr source
, xmlNodePtr elem
) {
847 xmlNodePtr cur
, res
, result
= NULL
, last
= NULL
;
849 if ((ctxt
== NULL
) || (target
== NULL
) || (source
== NULL
) ||
853 while (cur
!= NULL
) {
854 res
= xmlXIncludeCopyNode(ctxt
, target
, source
, cur
);
856 if (result
== NULL
) {
870 * xmlXIncludeGetNthChild:
872 * @no: the child number
874 * Returns the @n'th element child of @cur or NULL
877 xmlXIncludeGetNthChild(xmlNodePtr cur
, int no
) {
879 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
882 for (i
= 0;i
<= no
;cur
= cur
->next
) {
885 if ((cur
->type
== XML_ELEMENT_NODE
) ||
886 (cur
->type
== XML_DOCUMENT_NODE
) ||
887 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
896 xmlNodePtr
xmlXPtrAdvanceNode(xmlNodePtr cur
, int *level
); /* in xpointer.c */
898 * xmlXIncludeCopyRange:
899 * @ctxt: the XInclude context
900 * @target: the document target
901 * @source: the document source
902 * @obj: the XPointer result from the evaluation.
904 * Build a node list tree copy of the XPointer result.
906 * Returns an xmlNodePtr list or NULL.
907 * The caller has to free the node tree.
910 xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr target
,
911 xmlDocPtr source
, xmlXPathObjectPtr range
) {
912 /* pointers to generated nodes */
913 xmlNodePtr list
= NULL
, last
= NULL
, listParent
= NULL
;
914 xmlNodePtr tmp
, tmp2
;
915 /* pointers to traversal nodes */
916 xmlNodePtr start
, cur
, end
;
918 int level
= 0, lastLevel
= 0, endLevel
= 0, endFlag
= 0;
920 if ((ctxt
== NULL
) || (target
== NULL
) || (source
== NULL
) ||
923 if (range
->type
!= XPATH_RANGE
)
925 start
= (xmlNodePtr
) range
->user
;
927 if ((start
== NULL
) || (start
->type
== XML_NAMESPACE_DECL
))
931 return(xmlDocCopyNode(start
, target
, 1));
932 if (end
->type
== XML_NAMESPACE_DECL
)
936 index1
= range
->index
;
937 index2
= range
->index2
;
939 * level is depth of the current node under consideration
940 * list is the pointer to the root of the output tree
941 * listParent is a pointer to the parent of output tree (within
942 the included file) in case we need to add another level
943 * last is a pointer to the last node added to the output tree
944 * lastLevel is the depth of last (relative to the root)
946 while (cur
!= NULL
) {
948 * Check if our output tree needs a parent
952 /* copy must include namespaces and properties */
953 tmp2
= xmlDocCopyNode(listParent
, target
, 2);
954 xmlAddChild(tmp2
, list
);
956 listParent
= listParent
->parent
;
963 * Check whether we need to change our insertion point
965 while (level
< lastLevel
) {
969 if (cur
== end
) { /* Are we at the end of the range? */
970 if (cur
->type
== XML_TEXT_NODE
) {
971 const xmlChar
*content
= cur
->content
;
974 if (content
== NULL
) {
975 tmp
= xmlNewTextLen(NULL
, 0);
978 if ((cur
== start
) && (index1
> 1)) {
979 content
+= (index1
- 1);
984 tmp
= xmlNewTextLen(content
, len
);
986 /* single sub text node selection */
989 /* prune and return full set */
990 if (level
== lastLevel
)
991 xmlAddNextSibling(last
, tmp
);
993 xmlAddChild(last
, tmp
);
995 } else { /* ending node not a text node */
996 endLevel
= level
; /* remember the level of the end node */
998 /* last node - need to take care of properties + namespaces */
999 tmp
= xmlDocCopyNode(cur
, target
, 2);
1002 listParent
= cur
->parent
;
1004 if (level
== lastLevel
)
1005 xmlAddNextSibling(last
, tmp
);
1007 xmlAddChild(last
, tmp
);
1014 end
= xmlXIncludeGetNthChild(cur
, index2
- 1);
1017 if ((cur
== start
) && (index1
> 1)) {
1018 cur
= xmlXIncludeGetNthChild(cur
, index1
- 1);
1021 cur
= cur
->children
;
1023 level
++; /* increment level to show change */
1025 * Now gather the remaining nodes from cur to end
1027 continue; /* while */
1029 } else if (cur
== start
) { /* Not at the end, are we at start? */
1030 if ((cur
->type
== XML_TEXT_NODE
) ||
1031 (cur
->type
== XML_CDATA_SECTION_NODE
)) {
1032 const xmlChar
*content
= cur
->content
;
1034 if (content
== NULL
) {
1035 tmp
= xmlNewTextLen(NULL
, 0);
1038 content
+= (index1
- 1);
1041 tmp
= xmlNewText(content
);
1044 listParent
= cur
->parent
;
1045 } else { /* Not text node */
1047 * start of the range - need to take care of
1048 * properties and namespaces
1050 tmp
= xmlDocCopyNode(cur
, target
, 2);
1052 listParent
= cur
->parent
;
1053 if (index1
> 1) { /* Do we need to position? */
1054 cur
= xmlXIncludeGetNthChild(cur
, index1
- 1);
1055 level
= lastLevel
= 1;
1058 * Now gather the remaining nodes from cur to end
1060 continue; /* while */
1065 switch (cur
->type
) {
1067 case XML_ELEMENT_DECL
:
1068 case XML_ATTRIBUTE_DECL
:
1069 case XML_ENTITY_NODE
:
1070 /* Do not copy DTD informations */
1072 case XML_ENTITY_DECL
:
1073 /* handle crossing entities -> stack needed */
1075 case XML_XINCLUDE_START
:
1076 case XML_XINCLUDE_END
:
1077 /* don't consider it part of the tree content */
1079 case XML_ATTRIBUTE_NODE
:
1080 /* Humm, should not happen ! */
1084 * Middle of the range - need to take care of
1085 * properties and namespaces
1087 tmp
= xmlDocCopyNode(cur
, target
, 2);
1091 if (level
== lastLevel
)
1092 xmlAddNextSibling(last
, tmp
);
1094 xmlAddChild(last
, tmp
);
1101 * Skip to next node in document order
1103 cur
= xmlXPtrAdvanceNode(cur
, &level
);
1104 if (endFlag
&& (level
>= endLevel
))
1111 * xmlXIncludeBuildNodeList:
1112 * @ctxt: the XInclude context
1113 * @target: the document target
1114 * @source: the document source
1115 * @obj: the XPointer result from the evaluation.
1117 * Build a node list tree copy of the XPointer result.
1118 * This will drop Attributes and Namespace declarations.
1120 * Returns an xmlNodePtr list or NULL.
1121 * the caller has to free the node tree.
1124 xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr target
,
1125 xmlDocPtr source
, xmlXPathObjectPtr obj
) {
1126 xmlNodePtr list
= NULL
, last
= NULL
;
1131 if ((ctxt
== NULL
) || (target
== NULL
) || (source
== NULL
) ||
1134 switch (obj
->type
) {
1135 case XPATH_NODESET
: {
1136 xmlNodeSetPtr set
= obj
->nodesetval
;
1139 for (i
= 0;i
< set
->nodeNr
;i
++) {
1140 if (set
->nodeTab
[i
] == NULL
)
1142 switch (set
->nodeTab
[i
]->type
) {
1144 case XML_CDATA_SECTION_NODE
:
1145 case XML_ELEMENT_NODE
:
1146 case XML_ENTITY_REF_NODE
:
1147 case XML_ENTITY_NODE
:
1149 case XML_COMMENT_NODE
:
1150 case XML_DOCUMENT_NODE
:
1151 case XML_HTML_DOCUMENT_NODE
:
1152 #ifdef LIBXML_DOCB_ENABLED
1153 case XML_DOCB_DOCUMENT_NODE
:
1155 case XML_XINCLUDE_END
:
1157 case XML_XINCLUDE_START
: {
1158 xmlNodePtr tmp
, cur
= set
->nodeTab
[i
];
1161 while (cur
!= NULL
) {
1164 case XML_CDATA_SECTION_NODE
:
1165 case XML_ELEMENT_NODE
:
1166 case XML_ENTITY_REF_NODE
:
1167 case XML_ENTITY_NODE
:
1169 case XML_COMMENT_NODE
:
1170 tmp
= xmlXIncludeCopyNode(ctxt
, target
,
1175 xmlAddNextSibling(last
, tmp
);
1187 case XML_ATTRIBUTE_NODE
:
1188 case XML_NAMESPACE_DECL
:
1189 case XML_DOCUMENT_TYPE_NODE
:
1190 case XML_DOCUMENT_FRAG_NODE
:
1191 case XML_NOTATION_NODE
:
1193 case XML_ELEMENT_DECL
:
1194 case XML_ATTRIBUTE_DECL
:
1195 case XML_ENTITY_DECL
:
1199 list
= last
= xmlXIncludeCopyNode(ctxt
, target
, source
,
1202 xmlAddNextSibling(last
,
1203 xmlXIncludeCopyNode(ctxt
, target
, source
,
1205 if (last
->next
!= NULL
)
1211 #ifdef LIBXML_XPTR_ENABLED
1212 case XPATH_LOCATIONSET
: {
1213 xmlLocationSetPtr set
= (xmlLocationSetPtr
) obj
->user
;
1216 for (i
= 0;i
< set
->locNr
;i
++) {
1218 list
= last
= xmlXIncludeCopyXPointer(ctxt
, target
, source
,
1221 xmlAddNextSibling(last
,
1222 xmlXIncludeCopyXPointer(ctxt
, target
, source
,
1225 while (last
->next
!= NULL
)
1232 return(xmlXIncludeCopyRange(ctxt
, target
, source
, obj
));
1235 /* points are ignored in XInclude */
1242 /************************************************************************
1244 * XInclude I/O handling *
1246 ************************************************************************/
1248 typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData
;
1249 typedef xmlXIncludeMergeData
*xmlXIncludeMergeDataPtr
;
1250 struct _xmlXIncludeMergeData
{
1252 xmlXIncludeCtxtPtr ctxt
;
1256 * xmlXIncludeMergeOneEntity:
1258 * @doc: the including doc
1259 * @nr: the entity name
1261 * Inplements the merge of one entity
1264 xmlXIncludeMergeEntity(void *payload
, void *vdata
,
1265 const xmlChar
*name ATTRIBUTE_UNUSED
) {
1266 xmlEntityPtr ent
= (xmlEntityPtr
) payload
;
1267 xmlXIncludeMergeDataPtr data
= (xmlXIncludeMergeDataPtr
) vdata
;
1268 xmlEntityPtr ret
, prev
;
1270 xmlXIncludeCtxtPtr ctxt
;
1272 if ((ent
== NULL
) || (data
== NULL
))
1276 if ((ctxt
== NULL
) || (doc
== NULL
))
1278 switch (ent
->etype
) {
1279 case XML_INTERNAL_PARAMETER_ENTITY
:
1280 case XML_EXTERNAL_PARAMETER_ENTITY
:
1281 case XML_INTERNAL_PREDEFINED_ENTITY
:
1283 case XML_INTERNAL_GENERAL_ENTITY
:
1284 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
1285 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
1288 ret
= xmlAddDocEntity(doc
, ent
->name
, ent
->etype
, ent
->ExternalID
,
1289 ent
->SystemID
, ent
->content
);
1291 if (ent
->URI
!= NULL
)
1292 ret
->URI
= xmlStrdup(ent
->URI
);
1294 prev
= xmlGetDocEntity(doc
, ent
->name
);
1296 if (ent
->etype
!= prev
->etype
)
1299 if ((ent
->SystemID
!= NULL
) && (prev
->SystemID
!= NULL
)) {
1300 if (!xmlStrEqual(ent
->SystemID
, prev
->SystemID
))
1302 } else if ((ent
->ExternalID
!= NULL
) &&
1303 (prev
->ExternalID
!= NULL
)) {
1304 if (!xmlStrEqual(ent
->ExternalID
, prev
->ExternalID
))
1306 } else if ((ent
->content
!= NULL
) && (prev
->content
!= NULL
)) {
1307 if (!xmlStrEqual(ent
->content
, prev
->content
))
1317 switch (ent
->etype
) {
1318 case XML_INTERNAL_PARAMETER_ENTITY
:
1319 case XML_EXTERNAL_PARAMETER_ENTITY
:
1320 case XML_INTERNAL_PREDEFINED_ENTITY
:
1321 case XML_INTERNAL_GENERAL_ENTITY
:
1322 case XML_EXTERNAL_GENERAL_PARSED_ENTITY
:
1324 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
:
1327 xmlXIncludeErr(ctxt
, (xmlNodePtr
) ent
, XML_XINCLUDE_ENTITY_DEF_MISMATCH
,
1328 "mismatch in redefinition of entity %s\n",
1333 * xmlXIncludeMergeEntities:
1334 * @ctxt: an XInclude context
1335 * @doc: the including doc
1336 * @from: the included doc
1338 * Inplements the entity merge
1340 * Returns 0 if merge succeeded, -1 if some processing failed
1343 xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr doc
,
1346 xmlDtdPtr target
, source
;
1351 if ((from
== NULL
) || (from
->intSubset
== NULL
))
1354 target
= doc
->intSubset
;
1355 if (target
== NULL
) {
1356 cur
= xmlDocGetRootElement(doc
);
1359 target
= xmlCreateIntSubset(doc
, cur
->name
, NULL
, NULL
);
1364 source
= from
->intSubset
;
1365 if ((source
!= NULL
) && (source
->entities
!= NULL
)) {
1366 xmlXIncludeMergeData data
;
1371 xmlHashScan((xmlHashTablePtr
) source
->entities
,
1372 xmlXIncludeMergeEntity
, &data
);
1374 source
= from
->extSubset
;
1375 if ((source
!= NULL
) && (source
->entities
!= NULL
)) {
1376 xmlXIncludeMergeData data
;
1382 * don't duplicate existing stuff when external subsets are the same
1384 if ((!xmlStrEqual(target
->ExternalID
, source
->ExternalID
)) &&
1385 (!xmlStrEqual(target
->SystemID
, source
->SystemID
))) {
1386 xmlHashScan((xmlHashTablePtr
) source
->entities
,
1387 xmlXIncludeMergeEntity
, &data
);
1394 * xmlXIncludeLoadDoc:
1395 * @ctxt: the XInclude context
1396 * @url: the associated URL
1397 * @nr: the xinclude node number
1399 * Load the document, and store the result in the XInclude context
1401 * Returns 0 in case of success, -1 in case of failure
1404 xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt
, const xmlChar
*url
, int nr
) {
1408 xmlChar
*fragment
= NULL
;
1410 #ifdef LIBXML_XPTR_ENABLED
1414 #ifdef DEBUG_XINCLUDE
1415 xmlGenericError(xmlGenericErrorContext
, "Loading doc %s:%d\n", url
, nr
);
1418 * Check the URL and remove any fragment identifier
1420 uri
= xmlParseURI((const char *)url
);
1422 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1423 XML_XINCLUDE_HREF_URI
,
1424 "invalid value URI %s\n", url
);
1427 if (uri
->fragment
!= NULL
) {
1428 fragment
= (xmlChar
*) uri
->fragment
;
1429 uri
->fragment
= NULL
;
1431 if ((ctxt
->incTab
!= NULL
) && (ctxt
->incTab
[nr
] != NULL
) &&
1432 (ctxt
->incTab
[nr
]->fragment
!= NULL
)) {
1433 if (fragment
!= NULL
) xmlFree(fragment
);
1434 fragment
= xmlStrdup(ctxt
->incTab
[nr
]->fragment
);
1436 URL
= xmlSaveUri(uri
);
1439 if (ctxt
->incTab
!= NULL
)
1440 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1441 XML_XINCLUDE_HREF_URI
,
1442 "invalid value URI %s\n", url
);
1444 xmlXIncludeErr(ctxt
, NULL
,
1445 XML_XINCLUDE_HREF_URI
,
1446 "invalid value URI %s\n", url
);
1447 if (fragment
!= NULL
)
1453 * Handling of references to the local document are done
1454 * directly through ctxt->doc.
1456 if ((URL
[0] == 0) || (URL
[0] == '#') ||
1457 ((ctxt
->doc
!= NULL
) && (xmlStrEqual(URL
, ctxt
->doc
->URL
)))) {
1463 * Prevent reloading twice the document.
1465 for (i
= 0; i
< ctxt
->incNr
; i
++) {
1466 if ((xmlStrEqual(URL
, ctxt
->incTab
[i
]->URI
)) &&
1467 (ctxt
->incTab
[i
]->doc
!= NULL
)) {
1468 doc
= ctxt
->incTab
[i
]->doc
;
1469 #ifdef DEBUG_XINCLUDE
1470 printf("Already loaded %s\n", URL
);
1479 #ifdef DEBUG_XINCLUDE
1480 printf("loading %s\n", URL
);
1482 #ifdef LIBXML_XPTR_ENABLED
1484 * If this is an XPointer evaluation, we want to assure that
1485 * all entities have been resolved prior to processing the
1486 * referenced document
1488 saveFlags
= ctxt
->parseFlags
;
1489 if (fragment
!= NULL
) { /* if this is an XPointer eval */
1490 ctxt
->parseFlags
|= XML_PARSE_NOENT
;
1494 doc
= xmlXIncludeParseFile(ctxt
, (const char *)URL
);
1495 #ifdef LIBXML_XPTR_ENABLED
1496 ctxt
->parseFlags
= saveFlags
;
1500 if (fragment
!= NULL
)
1504 ctxt
->incTab
[nr
]->doc
= doc
;
1506 * It's possible that the requested URL has been mapped to a
1507 * completely different location (e.g. through a catalog entry).
1508 * To check for this, we compare the URL with that of the doc
1509 * and change it if they disagree (bug 146988).
1511 if (!xmlStrEqual(URL
, doc
->URL
)) {
1513 URL
= xmlStrdup(doc
->URL
);
1515 for (i
= nr
+ 1; i
< ctxt
->incNr
; i
++) {
1516 if (xmlStrEqual(URL
, ctxt
->incTab
[i
]->URI
)) {
1517 ctxt
->incTab
[nr
]->count
++;
1518 #ifdef DEBUG_XINCLUDE
1519 printf("Increasing %s count since reused\n", URL
);
1526 * Make sure we have all entities fixed up
1528 xmlXIncludeMergeEntities(ctxt
, ctxt
->doc
, doc
);
1531 * We don't need the DTD anymore, free up space
1532 if (doc->intSubset != NULL) {
1533 xmlUnlinkNode((xmlNodePtr) doc->intSubset);
1534 xmlFreeNode((xmlNodePtr) doc->intSubset);
1535 doc->intSubset = NULL;
1537 if (doc->extSubset != NULL) {
1538 xmlUnlinkNode((xmlNodePtr) doc->extSubset);
1539 xmlFreeNode((xmlNodePtr) doc->extSubset);
1540 doc->extSubset = NULL;
1543 xmlXIncludeRecurseDoc(ctxt
, doc
, URL
);
1546 if (fragment
== NULL
) {
1548 * Add the top children list as the replacement copy.
1552 /* Hopefully a DTD declaration won't be copied from
1553 * the same document */
1554 ctxt
->incTab
[nr
]->inc
= xmlCopyNodeList(ctxt
->doc
->children
);
1556 ctxt
->incTab
[nr
]->inc
= xmlXIncludeCopyNodeList(ctxt
, ctxt
->doc
,
1557 doc
, doc
->children
);
1560 #ifdef LIBXML_XPTR_ENABLED
1563 * Computes the XPointer expression and make a copy used
1564 * as the replacement copy.
1566 xmlXPathObjectPtr xptr
;
1567 xmlXPathContextPtr xptrctxt
;
1571 xptrctxt
= xmlXPtrNewContext(ctxt
->doc
, ctxt
->incTab
[nr
]->ref
,
1574 xptrctxt
= xmlXPtrNewContext(doc
, NULL
, NULL
);
1576 if (xptrctxt
== NULL
) {
1577 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1578 XML_XINCLUDE_XPTR_FAILED
,
1579 "could not create XPointer context\n", NULL
);
1584 xptr
= xmlXPtrEval(fragment
, xptrctxt
);
1586 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1587 XML_XINCLUDE_XPTR_FAILED
,
1588 "XPointer evaluation failed: #%s\n",
1590 xmlXPathFreeContext(xptrctxt
);
1595 switch (xptr
->type
) {
1596 case XPATH_UNDEFINED
:
1602 case XPATH_XSLT_TREE
:
1603 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1604 XML_XINCLUDE_XPTR_RESULT
,
1605 "XPointer is not a range: #%s\n",
1607 xmlXPathFreeContext(xptrctxt
);
1612 if ((xptr
->nodesetval
== NULL
) ||
1613 (xptr
->nodesetval
->nodeNr
<= 0)) {
1614 xmlXPathFreeContext(xptrctxt
);
1621 case XPATH_LOCATIONSET
:
1624 set
= xptr
->nodesetval
;
1626 for (i
= 0;i
< set
->nodeNr
;i
++) {
1627 if (set
->nodeTab
[i
] == NULL
)
1629 switch (set
->nodeTab
[i
]->type
) {
1630 case XML_ELEMENT_NODE
:
1632 case XML_CDATA_SECTION_NODE
:
1633 case XML_ENTITY_REF_NODE
:
1634 case XML_ENTITY_NODE
:
1636 case XML_COMMENT_NODE
:
1637 case XML_DOCUMENT_NODE
:
1638 case XML_HTML_DOCUMENT_NODE
:
1639 #ifdef LIBXML_DOCB_ENABLED
1640 case XML_DOCB_DOCUMENT_NODE
:
1644 case XML_ATTRIBUTE_NODE
:
1645 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1646 XML_XINCLUDE_XPTR_RESULT
,
1647 "XPointer selects an attribute: #%s\n",
1649 set
->nodeTab
[i
] = NULL
;
1651 case XML_NAMESPACE_DECL
:
1652 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1653 XML_XINCLUDE_XPTR_RESULT
,
1654 "XPointer selects a namespace: #%s\n",
1656 set
->nodeTab
[i
] = NULL
;
1658 case XML_DOCUMENT_TYPE_NODE
:
1659 case XML_DOCUMENT_FRAG_NODE
:
1660 case XML_NOTATION_NODE
:
1662 case XML_ELEMENT_DECL
:
1663 case XML_ATTRIBUTE_DECL
:
1664 case XML_ENTITY_DECL
:
1665 case XML_XINCLUDE_START
:
1666 case XML_XINCLUDE_END
:
1667 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1668 XML_XINCLUDE_XPTR_RESULT
,
1669 "XPointer selects unexpected nodes: #%s\n",
1671 set
->nodeTab
[i
] = NULL
;
1672 set
->nodeTab
[i
] = NULL
;
1678 ctxt
->incTab
[nr
]->xptr
= xptr
;
1679 ctxt
->incTab
[nr
]->inc
= NULL
;
1681 ctxt
->incTab
[nr
]->inc
=
1682 xmlXIncludeCopyXPointer(ctxt
, ctxt
->doc
, doc
, xptr
);
1683 xmlXPathFreeObject(xptr
);
1685 xmlXPathFreeContext(xptrctxt
);
1691 * Do the xml:base fixup if needed
1693 if ((doc
!= NULL
) && (URL
!= NULL
) &&
1694 (!(ctxt
->parseFlags
& XML_PARSE_NOBASEFIX
)) &&
1695 (!(doc
->parseFlags
& XML_PARSE_NOBASEFIX
))) {
1701 * The base is only adjusted if "necessary", i.e. if the xinclude node
1702 * has a base specified, or the URL is relative
1704 base
= xmlGetNsProp(ctxt
->incTab
[nr
]->ref
, BAD_CAST
"base",
1708 * No xml:base on the xinclude node, so we check whether the
1709 * URI base is different than (relative to) the context base
1711 curBase
= xmlBuildRelativeURI(URL
, ctxt
->base
);
1712 if (curBase
== NULL
) { /* Error return */
1713 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1714 XML_XINCLUDE_HREF_URI
,
1715 "trying to build relative URI from %s\n", URL
);
1717 /* If the URI doesn't contain a slash, it's not relative */
1718 if (!xmlStrchr(curBase
, (xmlChar
) '/'))
1724 if (base
!= NULL
) { /* Adjustment may be needed */
1725 node
= ctxt
->incTab
[nr
]->inc
;
1726 while (node
!= NULL
) {
1727 /* Only work on element nodes */
1728 if (node
->type
== XML_ELEMENT_NODE
) {
1729 curBase
= xmlNodeGetBase(node
->doc
, node
);
1730 /* If no current base, set it */
1731 if (curBase
== NULL
) {
1732 xmlNodeSetBase(node
, base
);
1735 * If the current base is the same as the
1736 * URL of the document, then reset it to be
1737 * the specified xml:base or the relative URI
1739 if (xmlStrEqual(curBase
, node
->doc
->URL
)) {
1740 xmlNodeSetBase(node
, base
);
1743 * If the element already has an xml:base
1744 * set, then relativise it if necessary
1747 xmlBase
= xmlGetNsProp(node
,
1750 if (xmlBase
!= NULL
) {
1752 relBase
= xmlBuildURI(xmlBase
, base
);
1753 if (relBase
== NULL
) { /* error */
1754 xmlXIncludeErr(ctxt
,
1755 ctxt
->incTab
[nr
]->ref
,
1756 XML_XINCLUDE_HREF_URI
,
1757 "trying to rebuild base from %s\n",
1760 xmlNodeSetBase(node
, relBase
);
1774 if ((nr
< ctxt
->incNr
) && (ctxt
->incTab
[nr
]->doc
!= NULL
) &&
1775 (ctxt
->incTab
[nr
]->count
<= 1)) {
1776 #ifdef DEBUG_XINCLUDE
1777 printf("freeing %s\n", ctxt
->incTab
[nr
]->doc
->URL
);
1779 xmlFreeDoc(ctxt
->incTab
[nr
]->doc
);
1780 ctxt
->incTab
[nr
]->doc
= NULL
;
1787 * xmlXIncludeLoadTxt:
1788 * @ctxt: the XInclude context
1789 * @url: the associated URL
1790 * @nr: the xinclude node number
1792 * Load the content, and store the result in the XInclude context
1794 * Returns 0 in case of success, -1 in case of failure
1797 xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt
, const xmlChar
*url
, int nr
) {
1798 xmlParserInputBufferPtr buf
;
1803 xmlChar
*encoding
= NULL
;
1804 xmlCharEncoding enc
= (xmlCharEncoding
) 0;
1805 xmlParserCtxtPtr pctxt
;
1806 xmlParserInputPtr inputStream
;
1807 int xinclude_multibyte_fallback_used
= 0;
1810 * Check the URL and remove any fragment identifier
1812 uri
= xmlParseURI((const char *)url
);
1814 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
, XML_XINCLUDE_HREF_URI
,
1815 "invalid value URI %s\n", url
);
1818 if (uri
->fragment
!= NULL
) {
1819 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
, XML_XINCLUDE_TEXT_FRAGMENT
,
1820 "fragment identifier forbidden for text: %s\n",
1821 (const xmlChar
*) uri
->fragment
);
1825 URL
= xmlSaveUri(uri
);
1828 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
, XML_XINCLUDE_HREF_URI
,
1829 "invalid value URI %s\n", url
);
1834 * Handling of references to the local document are done
1835 * directly through ctxt->doc.
1838 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1839 XML_XINCLUDE_TEXT_DOCUMENT
,
1840 "text serialization of document not available\n", NULL
);
1846 * Prevent reloading twice the document.
1848 for (i
= 0; i
< ctxt
->txtNr
; i
++) {
1849 if (xmlStrEqual(URL
, ctxt
->txturlTab
[i
])) {
1850 node
= xmlCopyNode(ctxt
->txtTab
[i
], 1);
1855 * Try to get the encoding if available
1857 if ((ctxt
->incTab
[nr
] != NULL
) && (ctxt
->incTab
[nr
]->ref
!= NULL
)) {
1858 encoding
= xmlGetProp(ctxt
->incTab
[nr
]->ref
, XINCLUDE_PARSE_ENCODING
);
1860 if (encoding
!= NULL
) {
1862 * TODO: we should not have to remap to the xmlCharEncoding
1863 * predefined set, a better interface than
1864 * xmlParserInputBufferCreateFilename should allow any
1865 * encoding supported by iconv
1867 enc
= xmlParseCharEncoding((const char *) encoding
);
1868 if (enc
== XML_CHAR_ENCODING_ERROR
) {
1869 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1870 XML_XINCLUDE_UNKNOWN_ENCODING
,
1871 "encoding %s not supported\n", encoding
);
1882 pctxt
= xmlNewParserCtxt();
1883 inputStream
= xmlLoadExternalEntity((const char*)URL
, NULL
, pctxt
);
1884 if(inputStream
== NULL
) {
1885 xmlFreeParserCtxt(pctxt
);
1889 buf
= inputStream
->buf
;
1891 xmlFreeInputStream (inputStream
);
1892 xmlFreeParserCtxt(pctxt
);
1897 xmlCharEncCloseFunc(buf
->encoder
);
1898 buf
->encoder
= xmlGetCharEncodingHandler(enc
);
1899 node
= xmlNewText(NULL
);
1902 * Scan all chars from the resource and add the to the node
1904 xinclude_multibyte_fallback
:
1905 while (xmlParserInputBufferRead(buf
, 128) > 0) {
1907 const xmlChar
*content
;
1909 content
= xmlBufContent(buf
->buffer
);
1910 len
= xmlBufLength(buf
->buffer
);
1911 for (i
= 0;i
< len
;) {
1915 cur
= xmlStringCurrentChar(NULL
, &content
[i
], &l
);
1916 if (!IS_CHAR(cur
)) {
1917 /* Handle splitted multibyte char at buffer boundary */
1918 if (((len
- i
) < 4) && (!xinclude_multibyte_fallback_used
)) {
1919 xinclude_multibyte_fallback_used
= 1;
1920 xmlBufShrink(buf
->buffer
, i
);
1921 goto xinclude_multibyte_fallback
;
1923 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
1924 XML_XINCLUDE_INVALID_CHAR
,
1925 "%s contains invalid char\n", URL
);
1926 xmlFreeParserInputBuffer(buf
);
1931 xinclude_multibyte_fallback_used
= 0;
1932 xmlNodeAddContentLen(node
, &content
[i
], l
);
1936 xmlBufShrink(buf
->buffer
, len
);
1938 xmlFreeParserCtxt(pctxt
);
1939 xmlXIncludeAddTxt(ctxt
, node
, URL
);
1940 xmlFreeInputStream(inputStream
);
1944 * Add the element as the replacement copy.
1946 ctxt
->incTab
[nr
]->inc
= node
;
1952 * xmlXIncludeLoadFallback:
1953 * @ctxt: the XInclude context
1954 * @fallback: the fallback node
1955 * @nr: the xinclude node number
1957 * Load the content of the fallback node, and store the result
1958 * in the XInclude context
1960 * Returns 0 in case of success, -1 in case of failure
1963 xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr fallback
, int nr
) {
1964 xmlXIncludeCtxtPtr newctxt
;
1967 if ((fallback
== NULL
) || (fallback
->type
== XML_NAMESPACE_DECL
) ||
1970 if (fallback
->children
!= NULL
) {
1972 * It's possible that the fallback also has 'includes'
1973 * (Bug 129969), so we re-process the fallback just in case
1975 newctxt
= xmlXIncludeNewContext(ctxt
->doc
);
1976 if (newctxt
== NULL
)
1978 newctxt
->_private
= ctxt
->_private
;
1979 newctxt
->base
= xmlStrdup(ctxt
->base
); /* Inherit the base from the existing context */
1980 xmlXIncludeSetFlags(newctxt
, ctxt
->parseFlags
);
1981 ret
= xmlXIncludeDoProcess(newctxt
, ctxt
->doc
, fallback
->children
);
1982 if (ctxt
->nbErrors
> 0)
1985 ret
= 0; /* xmlXIncludeDoProcess can return +ve number */
1986 xmlXIncludeFreeContext(newctxt
);
1988 ctxt
->incTab
[nr
]->inc
= xmlDocCopyNodeList(ctxt
->doc
,
1989 fallback
->children
);
1991 ctxt
->incTab
[nr
]->inc
= NULL
;
1992 ctxt
->incTab
[nr
]->emptyFb
= 1; /* flag empty callback */
1997 /************************************************************************
1999 * XInclude Processing *
2001 ************************************************************************/
2004 * xmlXIncludePreProcessNode:
2005 * @ctxt: an XInclude context
2006 * @node: an XInclude node
2008 * Implement the XInclude preprocessing, currently just adding the element
2009 * for further processing.
2011 * Returns the result list or NULL in case of error
2014 xmlXIncludePreProcessNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
) {
2015 xmlXIncludeAddNode(ctxt
, node
);
2020 * xmlXIncludeLoadNode:
2021 * @ctxt: an XInclude context
2022 * @nr: the node number
2024 * Find and load the infoset replacement for the given node.
2026 * Returns 0 if substitution succeeded, -1 if some processing failed
2029 xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt
, int nr
) {
2036 int xml
= 1; /* default Issue 64 */
2041 if ((nr
< 0) || (nr
>= ctxt
->incNr
))
2043 cur
= ctxt
->incTab
[nr
]->ref
;
2048 * read the attributes
2050 href
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_HREF
);
2052 href
= xmlStrdup(BAD_CAST
""); /* @@@@ href is now optional */
2056 parse
= xmlXIncludeGetProp(ctxt
, cur
, XINCLUDE_PARSE
);
2057 if (parse
!= NULL
) {
2058 if (xmlStrEqual(parse
, XINCLUDE_PARSE_XML
))
2060 else if (xmlStrEqual(parse
, XINCLUDE_PARSE_TEXT
))
2063 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
2064 XML_XINCLUDE_PARSE_VALUE
,
2065 "invalid value %s for 'parse'\n", parse
);
2077 base
= xmlNodeGetBase(ctxt
->doc
, cur
);
2079 URI
= xmlBuildURI(href
, ctxt
->doc
->URL
);
2081 URI
= xmlBuildURI(href
, base
);
2087 * Some escaping may be needed
2089 escbase
= xmlURIEscape(base
);
2090 eschref
= xmlURIEscape(href
);
2091 URI
= xmlBuildURI(eschref
, escbase
);
2092 if (escbase
!= NULL
)
2094 if (eschref
!= NULL
)
2098 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
2099 XML_XINCLUDE_HREF_URI
, "failed build URL\n", NULL
);
2108 #ifdef DEBUG_XINCLUDE
2109 xmlGenericError(xmlGenericErrorContext
, "parse: %s\n",
2110 xml
? "xml": "text");
2111 xmlGenericError(xmlGenericErrorContext
, "URI: %s\n", URI
);
2115 * Save the base for this include (saving the current one)
2117 oldBase
= ctxt
->base
;
2121 ret
= xmlXIncludeLoadDoc(ctxt
, URI
, nr
);
2122 /* xmlXIncludeGetFragment(ctxt, cur, URI); */
2124 ret
= xmlXIncludeLoadTxt(ctxt
, URI
, nr
);
2128 * Restore the original base before checking for fallback
2130 ctxt
->base
= oldBase
;
2133 xmlNodePtr children
;
2136 * Time to try a fallback if availble
2138 #ifdef DEBUG_XINCLUDE
2139 xmlGenericError(xmlGenericErrorContext
, "error looking for fallback\n");
2141 children
= cur
->children
;
2142 while (children
!= NULL
) {
2143 if ((children
->type
== XML_ELEMENT_NODE
) &&
2144 (children
->ns
!= NULL
) &&
2145 (xmlStrEqual(children
->name
, XINCLUDE_FALLBACK
)) &&
2146 ((xmlStrEqual(children
->ns
->href
, XINCLUDE_NS
)) ||
2147 (xmlStrEqual(children
->ns
->href
, XINCLUDE_OLD_NS
)))) {
2148 ret
= xmlXIncludeLoadFallback(ctxt
, children
, nr
);
2152 children
= children
->next
;
2156 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
2157 XML_XINCLUDE_NO_FALLBACK
,
2158 "could not load %s, and no fallback was found\n",
2177 * xmlXIncludeIncludeNode:
2178 * @ctxt: an XInclude context
2179 * @nr: the node number
2181 * Inplement the infoset replacement for the given node
2183 * Returns 0 if substitution succeeded, -1 if some processing failed
2186 xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt
, int nr
) {
2187 xmlNodePtr cur
, end
, list
, tmp
;
2191 if ((nr
< 0) || (nr
>= ctxt
->incNr
))
2193 cur
= ctxt
->incTab
[nr
]->ref
;
2194 if ((cur
== NULL
) || (cur
->type
== XML_NAMESPACE_DECL
))
2198 * If we stored an XPointer a late computation may be needed
2200 if ((ctxt
->incTab
[nr
]->inc
== NULL
) &&
2201 (ctxt
->incTab
[nr
]->xptr
!= NULL
)) {
2202 ctxt
->incTab
[nr
]->inc
=
2203 xmlXIncludeCopyXPointer(ctxt
, ctxt
->doc
, ctxt
->doc
,
2204 ctxt
->incTab
[nr
]->xptr
);
2205 xmlXPathFreeObject(ctxt
->incTab
[nr
]->xptr
);
2206 ctxt
->incTab
[nr
]->xptr
= NULL
;
2208 list
= ctxt
->incTab
[nr
]->inc
;
2209 ctxt
->incTab
[nr
]->inc
= NULL
;
2212 * Check against the risk of generating a multi-rooted document
2214 if ((cur
->parent
!= NULL
) &&
2215 (cur
->parent
->type
!= XML_ELEMENT_NODE
)) {
2219 while (tmp
!= NULL
) {
2220 if (tmp
->type
== XML_ELEMENT_NODE
)
2225 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
2226 XML_XINCLUDE_MULTIPLE_ROOT
,
2227 "XInclude error: would result in multiple root nodes\n",
2233 if (ctxt
->parseFlags
& XML_PARSE_NOXINCNODE
) {
2235 * Add the list of nodes
2237 while (list
!= NULL
) {
2241 xmlAddPrevSibling(cur
, end
);
2247 * Change the current node as an XInclude start one, and add an
2250 cur
->type
= XML_XINCLUDE_START
;
2251 end
= xmlNewDocNode(cur
->doc
, cur
->ns
, cur
->name
, NULL
);
2253 xmlXIncludeErr(ctxt
, ctxt
->incTab
[nr
]->ref
,
2254 XML_XINCLUDE_BUILD_FAILED
,
2255 "failed to build node\n", NULL
);
2258 end
->type
= XML_XINCLUDE_END
;
2259 xmlAddNextSibling(cur
, end
);
2262 * Add the list of nodes
2264 while (list
!= NULL
) {
2268 xmlAddPrevSibling(end
, cur
);
2277 * xmlXIncludeTestNode:
2278 * @ctxt: the XInclude processing context
2279 * @node: an XInclude node
2281 * test if the node is an XInclude node
2283 * Returns 1 true, 0 otherwise
2286 xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
) {
2289 if (node
->type
!= XML_ELEMENT_NODE
)
2291 if (node
->ns
== NULL
)
2293 if ((xmlStrEqual(node
->ns
->href
, XINCLUDE_NS
)) ||
2294 (xmlStrEqual(node
->ns
->href
, XINCLUDE_OLD_NS
))) {
2295 if (xmlStrEqual(node
->ns
->href
, XINCLUDE_OLD_NS
)) {
2296 if (ctxt
->legacy
== 0) {
2297 #if 0 /* wait for the XML Core Working Group to get something stable ! */
2298 xmlXIncludeWarn(ctxt
, node
, XML_XINCLUDE_DEPRECATED_NS
,
2299 "Deprecated XInclude namespace found, use %s",
2305 if (xmlStrEqual(node
->name
, XINCLUDE_NODE
)) {
2306 xmlNodePtr child
= node
->children
;
2307 int nb_fallback
= 0;
2309 while (child
!= NULL
) {
2310 if ((child
->type
== XML_ELEMENT_NODE
) &&
2311 (child
->ns
!= NULL
) &&
2312 ((xmlStrEqual(child
->ns
->href
, XINCLUDE_NS
)) ||
2313 (xmlStrEqual(child
->ns
->href
, XINCLUDE_OLD_NS
)))) {
2314 if (xmlStrEqual(child
->name
, XINCLUDE_NODE
)) {
2315 xmlXIncludeErr(ctxt
, node
,
2316 XML_XINCLUDE_INCLUDE_IN_INCLUDE
,
2317 "%s has an 'include' child\n",
2321 if (xmlStrEqual(child
->name
, XINCLUDE_FALLBACK
)) {
2325 child
= child
->next
;
2327 if (nb_fallback
> 1) {
2328 xmlXIncludeErr(ctxt
, node
, XML_XINCLUDE_FALLBACKS_IN_INCLUDE
,
2329 "%s has multiple fallback children\n",
2335 if (xmlStrEqual(node
->name
, XINCLUDE_FALLBACK
)) {
2336 if ((node
->parent
== NULL
) ||
2337 (node
->parent
->type
!= XML_ELEMENT_NODE
) ||
2338 (node
->parent
->ns
== NULL
) ||
2339 ((!xmlStrEqual(node
->parent
->ns
->href
, XINCLUDE_NS
)) &&
2340 (!xmlStrEqual(node
->parent
->ns
->href
, XINCLUDE_OLD_NS
))) ||
2341 (!xmlStrEqual(node
->parent
->name
, XINCLUDE_NODE
))) {
2342 xmlXIncludeErr(ctxt
, node
,
2343 XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE
,
2344 "%s is not the child of an 'include'\n",
2353 * xmlXIncludeDoProcess:
2354 * @ctxt: the XInclude processing context
2355 * @doc: an XML document
2356 * @tree: the top of the tree to process
2358 * Implement the XInclude substitution on the XML document @doc
2360 * Returns 0 if no substitution were done, -1 if some processing failed
2361 * or the number of substitutions done.
2364 xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt
, xmlDocPtr doc
, xmlNodePtr tree
) {
2369 if ((doc
== NULL
) || (tree
== NULL
) || (tree
->type
== XML_NAMESPACE_DECL
))
2374 if (doc
->URL
!= NULL
) {
2375 ret
= xmlXIncludeURLPush(ctxt
, doc
->URL
);
2379 start
= ctxt
->incNr
;
2382 * First phase: lookup the elements in the document
2385 if (xmlXIncludeTestNode(ctxt
, cur
) == 1)
2386 xmlXIncludePreProcessNode(ctxt
, cur
);
2387 while ((cur
!= NULL
) && (cur
!= tree
->parent
)) {
2388 /* TODO: need to work on entities -> stack */
2389 if ((cur
->children
!= NULL
) &&
2390 (cur
->children
->type
!= XML_ENTITY_DECL
) &&
2391 (cur
->children
->type
!= XML_XINCLUDE_START
) &&
2392 (cur
->children
->type
!= XML_XINCLUDE_END
)) {
2393 cur
= cur
->children
;
2394 if (xmlXIncludeTestNode(ctxt
, cur
))
2395 xmlXIncludePreProcessNode(ctxt
, cur
);
2396 } else if (cur
->next
!= NULL
) {
2398 if (xmlXIncludeTestNode(ctxt
, cur
))
2399 xmlXIncludePreProcessNode(ctxt
, cur
);
2405 if ((cur
== NULL
) || (cur
== tree
->parent
))
2407 if (cur
->next
!= NULL
) {
2409 if (xmlXIncludeTestNode(ctxt
, cur
))
2410 xmlXIncludePreProcessNode(ctxt
, cur
);
2413 } while (cur
!= NULL
);
2418 * Second Phase : collect the infosets fragments
2420 for (i
= start
;i
< ctxt
->incNr
; i
++) {
2421 xmlXIncludeLoadNode(ctxt
, i
);
2426 * Third phase: extend the original document infoset.
2428 * Originally we bypassed the inclusion if there were any errors
2429 * encountered on any of the XIncludes. A bug was raised (bug
2430 * 132588) requesting that we output the XIncludes without error,
2431 * so the check for inc!=NULL || xptr!=NULL was put in. This may
2432 * give some other problems in the future, but for now it seems to
2436 for (i
= ctxt
->incBase
;i
< ctxt
->incNr
; i
++) {
2437 if ((ctxt
->incTab
[i
]->inc
!= NULL
) ||
2438 (ctxt
->incTab
[i
]->xptr
!= NULL
) ||
2439 (ctxt
->incTab
[i
]->emptyFb
!= 0)) /* (empty fallback) */
2440 xmlXIncludeIncludeNode(ctxt
, i
);
2443 if (doc
->URL
!= NULL
)
2444 xmlXIncludeURLPop(ctxt
);
2449 * xmlXIncludeSetFlags:
2450 * @ctxt: an XInclude processing context
2451 * @flags: a set of xmlParserOption used for parsing XML includes
2453 * Set the flags used for further processing of XML resources.
2455 * Returns 0 in case of success and -1 in case of error.
2458 xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt
, int flags
) {
2461 ctxt
->parseFlags
= flags
;
2466 * xmlXIncludeProcessTreeFlagsData:
2467 * @tree: an XML node
2468 * @flags: a set of xmlParserOption used for parsing XML includes
2469 * @data: application data that will be passed to the parser context
2470 * in the _private field of the parser context(s)
2472 * Implement the XInclude substitution on the XML node @tree
2474 * Returns 0 if no substitution were done, -1 if some processing failed
2475 * or the number of substitutions done.
2479 xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree
, int flags
, void *data
) {
2480 xmlXIncludeCtxtPtr ctxt
;
2483 if ((tree
== NULL
) || (tree
->type
== XML_NAMESPACE_DECL
) ||
2484 (tree
->doc
== NULL
))
2487 ctxt
= xmlXIncludeNewContext(tree
->doc
);
2490 ctxt
->_private
= data
;
2491 ctxt
->base
= xmlStrdup((xmlChar
*)tree
->doc
->URL
);
2492 xmlXIncludeSetFlags(ctxt
, flags
);
2493 ret
= xmlXIncludeDoProcess(ctxt
, tree
->doc
, tree
);
2494 if ((ret
>= 0) && (ctxt
->nbErrors
> 0))
2497 xmlXIncludeFreeContext(ctxt
);
2502 * xmlXIncludeProcessFlagsData:
2503 * @doc: an XML document
2504 * @flags: a set of xmlParserOption used for parsing XML includes
2505 * @data: application data that will be passed to the parser context
2506 * in the _private field of the parser context(s)
2508 * Implement the XInclude substitution on the XML document @doc
2510 * Returns 0 if no substitution were done, -1 if some processing failed
2511 * or the number of substitutions done.
2514 xmlXIncludeProcessFlagsData(xmlDocPtr doc
, int flags
, void *data
) {
2519 tree
= xmlDocGetRootElement(doc
);
2522 return(xmlXIncludeProcessTreeFlagsData(tree
, flags
, data
));
2526 * xmlXIncludeProcessFlags:
2527 * @doc: an XML document
2528 * @flags: a set of xmlParserOption used for parsing XML includes
2530 * Implement the XInclude substitution on the XML document @doc
2532 * Returns 0 if no substitution were done, -1 if some processing failed
2533 * or the number of substitutions done.
2536 xmlXIncludeProcessFlags(xmlDocPtr doc
, int flags
) {
2537 return xmlXIncludeProcessFlagsData(doc
, flags
, NULL
);
2541 * xmlXIncludeProcess:
2542 * @doc: an XML document
2544 * Implement the XInclude substitution on the XML document @doc
2546 * Returns 0 if no substitution were done, -1 if some processing failed
2547 * or the number of substitutions done.
2550 xmlXIncludeProcess(xmlDocPtr doc
) {
2551 return(xmlXIncludeProcessFlags(doc
, 0));
2555 * xmlXIncludeProcessTreeFlags:
2556 * @tree: a node in an XML document
2557 * @flags: a set of xmlParserOption used for parsing XML includes
2559 * Implement the XInclude substitution for the given subtree
2561 * Returns 0 if no substitution were done, -1 if some processing failed
2562 * or the number of substitutions done.
2565 xmlXIncludeProcessTreeFlags(xmlNodePtr tree
, int flags
) {
2566 xmlXIncludeCtxtPtr ctxt
;
2569 if ((tree
== NULL
) || (tree
->type
== XML_NAMESPACE_DECL
) ||
2570 (tree
->doc
== NULL
))
2572 ctxt
= xmlXIncludeNewContext(tree
->doc
);
2575 ctxt
->base
= xmlNodeGetBase(tree
->doc
, tree
);
2576 xmlXIncludeSetFlags(ctxt
, flags
);
2577 ret
= xmlXIncludeDoProcess(ctxt
, tree
->doc
, tree
);
2578 if ((ret
>= 0) && (ctxt
->nbErrors
> 0))
2581 xmlXIncludeFreeContext(ctxt
);
2586 * xmlXIncludeProcessTree:
2587 * @tree: a node in an XML document
2589 * Implement the XInclude substitution for the given subtree
2591 * Returns 0 if no substitution were done, -1 if some processing failed
2592 * or the number of substitutions done.
2595 xmlXIncludeProcessTree(xmlNodePtr tree
) {
2596 return(xmlXIncludeProcessTreeFlags(tree
, 0));
2600 * xmlXIncludeProcessNode:
2601 * @ctxt: an existing XInclude context
2602 * @node: a node in an XML document
2604 * Implement the XInclude substitution for the given subtree reusing
2605 * the informations and data coming from the given context.
2607 * Returns 0 if no substitution were done, -1 if some processing failed
2608 * or the number of substitutions done.
2611 xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt
, xmlNodePtr node
) {
2614 if ((node
== NULL
) || (node
->type
== XML_NAMESPACE_DECL
) ||
2615 (node
->doc
== NULL
) || (ctxt
== NULL
))
2617 ret
= xmlXIncludeDoProcess(ctxt
, node
->doc
, node
);
2618 if ((ret
>= 0) && (ctxt
->nbErrors
> 0))
2623 #else /* !LIBXML_XINCLUDE_ENABLED */
2625 #define bottom_xinclude
2626 #include "elfgcchack.h"