2 * imports.c: Implementation of the XSLT imports
5 * http://www.w3.org/TR/1999/REC-xslt-19991116
7 * See Copyright for the status of this software.
16 #ifdef HAVE_SYS_TYPES_H
17 #include <sys/types.h>
35 #include <libxml/xmlmemory.h>
36 #include <libxml/tree.h>
37 #include <libxml/hash.h>
38 #include <libxml/xmlerror.h>
39 #include <libxml/uri.h>
41 #include "xsltInternals.h"
42 #include "xsltutils.h"
44 #include "documents.h"
48 /************************************************************************
52 ************************************************************************/
55 * xsltParseStylesheetImport:
56 * @style: the XSLT stylesheet
57 * @template: the "strip-space" element
59 * parse an XSLT stylesheet strip-space element and record
60 * elements needing stripping
64 xsltParseStylesheetImport(xsltStylesheetPtr style, xmlNodePtr cur) {
65 xmlDocPtr import = NULL;
67 xmlChar *uriRef = NULL;
69 xsltStylesheetPtr res;
71 if ((cur == NULL) || (style == NULL))
74 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
76 xsltPrintErrorContext(NULL, style, cur);
77 xsltGenericError(xsltGenericErrorContext,
78 "xsl:import : missing href attribute\n");
82 base = xmlNodeGetBase(style->doc, cur);
83 URI = xmlBuildURI(uriRef, base);
85 xsltPrintErrorContext(NULL, style, cur);
86 xsltGenericError(xsltGenericErrorContext,
87 "xsl:import : invalid URI reference %s\n", uriRef);
90 import = xmlParseFile((const char *)URI);
92 xsltPrintErrorContext(NULL, style, cur);
93 xsltGenericError(xsltGenericErrorContext,
94 "xsl:import : unable to load %s\n", URI);
98 res = xsltParseStylesheetDoc(import);
101 res->next = style->imports;
102 style->imports = res;
103 style->extrasNr += res->extrasNr;
116 * xsltParseStylesheetInclude:
117 * @style: the XSLT stylesheet
118 * @template: the "strip-space" element
120 * parse an XSLT stylesheet strip-space element and record
121 * elements needing stripping
125 xsltParseStylesheetInclude(xsltStylesheetPtr style, xmlNodePtr cur) {
127 xmlChar *base = NULL;
128 xmlChar *uriRef = NULL;
130 xsltDocumentPtr include;
132 if ((cur == NULL) || (style == NULL))
135 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
136 if (uriRef == NULL) {
137 xsltPrintErrorContext(NULL, style, cur);
138 xsltGenericError(xsltGenericErrorContext,
139 "xsl:include : missing href attribute\n");
143 base = xmlNodeGetBase(style->doc, cur);
144 URI = xmlBuildURI(uriRef, base);
146 xsltPrintErrorContext(NULL, style, cur);
147 xsltGenericError(xsltGenericErrorContext,
148 "xsl:include : invalid URI reference %s\n", uriRef);
152 include = xsltLoadStyleDocument(style, URI);
153 if (include == NULL) {
154 xsltPrintErrorContext(NULL, style, cur);
155 xsltGenericError(xsltGenericErrorContext,
156 "xsl:include : unable to load %s\n", URI);
161 style->doc = include->doc;
162 xsltParseStylesheetProcess(style, include->doc);
176 * @cur: the current XSLT stylesheet
178 * Find the next stylesheet in import precedence.
180 * Returns the next stylesheet or NULL if it was the last one
184 xsltNextImport(xsltStylesheetPtr cur) {
187 if (cur->imports != NULL)
188 return(cur->imports);
189 if (cur->next != NULL)
193 if (cur == NULL) return(NULL);
194 if (cur->next != NULL) return(cur->next);
195 } while (cur != NULL);
200 * xsltNeedElemSpaceHandling:
201 * @ctxt: an XSLT transformation context
203 * Returns whether that stylesheet requires white-space stripping
205 * Returns 1 if space should be stripped, 0 if not
209 xsltNeedElemSpaceHandling(xsltTransformContextPtr ctxt) {
210 xsltStylesheetPtr style;
215 while (style != NULL) {
216 if (style->stripSpaces != NULL)
218 style = xsltNextImport(style);
224 * xsltFindElemSpaceHandling:
225 * @ctxt: an XSLT transformation context
228 * Find strip-space or preserve-space informations for an element
229 * respect the import precedence or the wildcards
231 * Returns 1 if space should be stripped, 0 if not, and 2 if everything
232 * should be CDTATA wrapped.
236 xsltFindElemSpaceHandling(xsltTransformContextPtr ctxt, xmlNodePtr node) {
237 xsltStylesheetPtr style;
240 if ((ctxt == NULL) || (node == NULL))
243 while (style != NULL) {
244 /* TODO: add namespaces support */
245 val = (const xmlChar *)
246 xmlHashLookup(style->stripSpaces, node->name);
248 if (xmlStrEqual(val, (xmlChar *) "strip"))
250 if (xmlStrEqual(val, (xmlChar *) "preserve"))
253 if (ctxt->style->stripAll == 1)
255 if (ctxt->style->stripAll == -1)
258 style = xsltNextImport(style);
265 * @ctxt: an XSLT transformation context
266 * @name: the template name
267 * @nameURI: the template name URI
269 * Finds the named template, apply import precedence rule.
271 * Returns the xsltTemplatePtr or NULL if not found
274 xsltFindTemplate(xsltTransformContextPtr ctxt, const xmlChar *name,
275 const xmlChar *nameURI) {
277 xsltStylesheetPtr style;
279 if ((ctxt == NULL) || (name == NULL))
282 while (style != NULL) {
283 cur = style->templates;
284 while (cur != NULL) {
285 if (xmlStrEqual(name, cur->name)) {
286 if (((nameURI == NULL) && (cur->nameURI == NULL)) ||
287 ((nameURI != NULL) && (cur->nameURI != NULL) &&
288 (xmlStrEqual(nameURI, cur->nameURI)))) {
295 style = xsltNextImport(style);