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.
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
36 #include <libxml/xmlmemory.h>
37 #include <libxml/tree.h>
38 #include <libxml/hash.h>
39 #include <libxml/xmlerror.h>
40 #include <libxml/uri.h>
42 #include "xsltInternals.h"
43 #include "xsltutils.h"
45 #include "documents.h"
50 /************************************************************************
54 ************************************************************************/
57 * xsltParseStylesheetImport:
58 * @style: the XSLT stylesheet
59 * @template: the "strip-space" element
61 * parse an XSLT stylesheet strip-space element and record
62 * elements needing stripping. Returns zero on success and something else
67 xsltParseStylesheetImport(xsltStylesheetPtr style, xmlNodePtr cur) {
69 xmlDocPtr import = NULL;
71 xmlChar *uriRef = NULL;
73 xsltStylesheetPtr res;
74 xsltSecurityPrefsPtr sec;
76 if ((cur == NULL) || (style == NULL))
79 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
81 xsltPrintErrorContext(NULL, style, cur);
82 xsltGenericError(xsltGenericErrorContext,
83 "xsl:import : missing href attribute\n");
87 base = xmlNodeGetBase(style->doc, cur);
88 URI = xmlBuildURI(uriRef, base);
90 xsltPrintErrorContext(NULL, style, cur);
91 xsltGenericError(xsltGenericErrorContext,
92 "xsl:import : invalid URI reference %s\n", uriRef);
97 * Security framework check
99 sec = xsltGetDefaultSecurityPrefs();
103 res = xsltCheckRead(sec, NULL, URI);
105 xsltPrintErrorContext(NULL, NULL, NULL);
106 xsltGenericError(xsltGenericErrorContext,
107 "xsl:import: read rights for %s denied\n",
113 import = xmlParseFile((const char *)URI);
114 if (import == NULL) {
115 xsltPrintErrorContext(NULL, style, cur);
116 xsltGenericError(xsltGenericErrorContext,
117 "xsl:import : unable to load %s\n", URI);
121 res = xsltParseStylesheetDoc(import);
124 res->next = style->imports;
125 style->imports = res;
126 style->extrasNr += res->extrasNr;
144 * xsltParseStylesheetInclude:
145 * @style: the XSLT stylesheet
146 * @template: the "strip-space" element
148 * parse an XSLT stylesheet strip-space element and record
149 * elements needing stripping. Returns zero on success, something else
154 xsltParseStylesheetInclude(xsltStylesheetPtr style, xmlNodePtr cur) {
157 xmlChar *base = NULL;
158 xmlChar *uriRef = NULL;
160 xsltDocumentPtr include;
162 if ((cur == NULL) || (style == NULL))
165 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
166 if (uriRef == NULL) {
167 xsltPrintErrorContext(NULL, style, cur);
168 xsltGenericError(xsltGenericErrorContext,
169 "xsl:include : missing href attribute\n");
173 base = xmlNodeGetBase(style->doc, cur);
174 URI = xmlBuildURI(uriRef, base);
176 xsltPrintErrorContext(NULL, style, cur);
177 xsltGenericError(xsltGenericErrorContext,
178 "xsl:include : invalid URI reference %s\n", uriRef);
182 include = xsltLoadStyleDocument(style, URI);
183 if (include == NULL) {
184 xsltPrintErrorContext(NULL, style, cur);
185 xsltGenericError(xsltGenericErrorContext,
186 "xsl:include : unable to load %s\n", URI);
191 style->doc = include->doc;
192 ret = (int)xsltParseStylesheetProcess(style, include->doc);
213 * @cur: the current XSLT stylesheet
215 * Find the next stylesheet in import precedence.
217 * Returns the next stylesheet or NULL if it was the last one
221 xsltNextImport(xsltStylesheetPtr cur) {
224 if (cur->imports != NULL)
225 return(cur->imports);
226 if (cur->next != NULL)
230 if (cur == NULL) return(NULL);
231 if (cur->next != NULL) return(cur->next);
232 } while (cur != NULL);
237 * xsltNeedElemSpaceHandling:
238 * @ctxt: an XSLT transformation context
240 * Returns whether that stylesheet requires white-space stripping
242 * Returns 1 if space should be stripped, 0 if not
246 xsltNeedElemSpaceHandling(xsltTransformContextPtr ctxt) {
247 xsltStylesheetPtr style;
252 while (style != NULL) {
253 if (style->stripSpaces != NULL)
255 style = xsltNextImport(style);
261 * xsltFindElemSpaceHandling:
262 * @ctxt: an XSLT transformation context
265 * Find strip-space or preserve-space informations for an element
266 * respect the import precedence or the wildcards
268 * Returns 1 if space should be stripped, 0 if not, and 2 if everything
269 * should be CDTATA wrapped.
273 xsltFindElemSpaceHandling(xsltTransformContextPtr ctxt, xmlNodePtr node) {
274 xsltStylesheetPtr style;
277 if ((ctxt == NULL) || (node == NULL))
280 while (style != NULL) {
281 if (node->ns != NULL) {
282 val = (const xmlChar *)
283 xmlHashLookup2(style->stripSpaces, node->name, node->ns->href);
285 val = (const xmlChar *)
286 xmlHashLookup2(style->stripSpaces, node->name, NULL);
289 if (xmlStrEqual(val, (xmlChar *) "strip"))
291 if (xmlStrEqual(val, (xmlChar *) "preserve"))
294 if (ctxt->style->stripAll == 1)
296 if (ctxt->style->stripAll == -1)
299 style = xsltNextImport(style);
306 * @ctxt: an XSLT transformation context
307 * @name: the template name
308 * @nameURI: the template name URI
310 * Finds the named template, apply import precedence rule.
312 * Returns the xsltTemplatePtr or NULL if not found
315 xsltFindTemplate(xsltTransformContextPtr ctxt, const xmlChar *name,
316 const xmlChar *nameURI) {
318 xsltStylesheetPtr style;
320 if ((ctxt == NULL) || (name == NULL))
323 while (style != NULL) {
324 cur = style->templates;
325 while (cur != NULL) {
326 if (xmlStrEqual(name, cur->name)) {
327 if (((nameURI == NULL) && (cur->nameURI == NULL)) ||
328 ((nameURI != NULL) && (cur->nameURI != NULL) &&
329 (xmlStrEqual(nameURI, cur->nameURI)))) {
336 style = xsltNextImport(style);