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 * @cur: the import element
61 * parse an XSLT stylesheet import element
63 * Returns 0 in case of success -1 in case of failure.
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 xsltTransformError(NULL, style, cur,
82 "xsl:import : missing href attribute\n");
86 base = xmlNodeGetBase(style->doc, cur);
87 URI = xmlBuildURI(uriRef, base);
89 xsltTransformError(NULL, style, cur,
90 "xsl:import : invalid URI reference %s\n", uriRef);
95 * Security framework check
97 sec = xsltGetDefaultSecurityPrefs();
101 secres = xsltCheckRead(sec, NULL, URI);
103 xsltTransformError(NULL, NULL, NULL,
104 "xsl:import: read rights for %s denied\n",
110 import = xmlParseFile((const char *)URI);
111 if (import == NULL) {
112 xsltTransformError(NULL, style, cur,
113 "xsl:import : unable to load %s\n", URI);
117 res = xsltParseStylesheetImportedDoc(import);
120 res->next = style->imports;
121 style->imports = res;
122 style->extrasNr += res->extrasNr;
140 * xsltParseStylesheetInclude:
141 * @style: the XSLT stylesheet
142 * @cur: the include node
144 * parse an XSLT stylesheet include element
146 * Returns 0 in case of success -1 in case of failure
150 xsltParseStylesheetInclude(xsltStylesheetPtr style, xmlNodePtr cur) {
153 xmlChar *base = NULL;
154 xmlChar *uriRef = NULL;
156 xsltDocumentPtr include;
158 if ((cur == NULL) || (style == NULL))
161 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
162 if (uriRef == NULL) {
163 xsltTransformError(NULL, style, cur,
164 "xsl:include : missing href attribute\n");
168 base = xmlNodeGetBase(style->doc, cur);
169 URI = xmlBuildURI(uriRef, base);
171 xsltTransformError(NULL, style, cur,
172 "xsl:include : invalid URI reference %s\n", uriRef);
176 include = xsltLoadStyleDocument(style, URI);
177 if (include == NULL) {
178 xsltTransformError(NULL, style, cur,
179 "xsl:include : unable to load %s\n", URI);
184 style->doc = include->doc;
185 ret = (int)xsltParseStylesheetProcess(style, include->doc);
206 * @cur: the current XSLT stylesheet
208 * Find the next stylesheet in import precedence.
210 * Returns the next stylesheet or NULL if it was the last one
214 xsltNextImport(xsltStylesheetPtr cur) {
217 if (cur->imports != NULL)
218 return(cur->imports);
219 if (cur->next != NULL)
223 if (cur == NULL) return(NULL);
224 if (cur->next != NULL) return(cur->next);
225 } while (cur != NULL);
230 * xsltNeedElemSpaceHandling:
231 * @ctxt: an XSLT transformation context
233 * Checks whether that stylesheet requires white-space stripping
235 * Returns 1 if space should be stripped, 0 if not
239 xsltNeedElemSpaceHandling(xsltTransformContextPtr ctxt) {
240 xsltStylesheetPtr style;
245 while (style != NULL) {
246 if (style->stripSpaces != NULL)
248 style = xsltNextImport(style);
254 * xsltFindElemSpaceHandling:
255 * @ctxt: an XSLT transformation context
258 * Find strip-space or preserve-space informations for an element
259 * respect the import precedence or the wildcards
261 * Returns 1 if space should be stripped, 0 if not, and 2 if everything
262 * should be CDTATA wrapped.
266 xsltFindElemSpaceHandling(xsltTransformContextPtr ctxt, xmlNodePtr node) {
267 xsltStylesheetPtr style;
270 if ((ctxt == NULL) || (node == NULL))
273 while (style != NULL) {
274 if (node->ns != NULL) {
275 val = (const xmlChar *)
276 xmlHashLookup2(style->stripSpaces, node->name, node->ns->href);
278 val = (const xmlChar *)
279 xmlHashLookup2(style->stripSpaces, node->name, NULL);
282 if (xmlStrEqual(val, (xmlChar *) "strip"))
284 if (xmlStrEqual(val, (xmlChar *) "preserve"))
287 if (style->stripAll == 1)
289 if (style->stripAll == -1)
292 style = xsltNextImport(style);
299 * @ctxt: an XSLT transformation context
300 * @name: the template name
301 * @nameURI: the template name URI
303 * Finds the named template, apply import precedence rule.
305 * Returns the xsltTemplatePtr or NULL if not found
308 xsltFindTemplate(xsltTransformContextPtr ctxt, const xmlChar *name,
309 const xmlChar *nameURI) {
311 xsltStylesheetPtr style;
313 if ((ctxt == NULL) || (name == NULL))
316 while (style != NULL) {
317 cur = style->templates;
318 while (cur != NULL) {
319 if (xmlStrEqual(name, cur->name)) {
320 if (((nameURI == NULL) && (cur->nameURI == NULL)) ||
321 ((nameURI != NULL) && (cur->nameURI != NULL) &&
322 (xmlStrEqual(nameURI, cur->nameURI)))) {
329 style = xsltNextImport(style);