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 xmlHashScan(res->templatesHash,
123 (xmlHashScanner) xsltNormalizeCompSteps, style);
124 style->extrasNr += res->extrasNr;
142 * xsltParseStylesheetInclude:
143 * @style: the XSLT stylesheet
144 * @cur: the include node
146 * parse an XSLT stylesheet include element
148 * Returns 0 in case of success -1 in case of failure
152 xsltParseStylesheetInclude(xsltStylesheetPtr style, xmlNodePtr cur) {
155 xmlChar *base = NULL;
156 xmlChar *uriRef = NULL;
158 xsltDocumentPtr include;
160 if ((cur == NULL) || (style == NULL))
163 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
164 if (uriRef == NULL) {
165 xsltTransformError(NULL, style, cur,
166 "xsl:include : missing href attribute\n");
170 base = xmlNodeGetBase(style->doc, cur);
171 URI = xmlBuildURI(uriRef, base);
173 xsltTransformError(NULL, style, cur,
174 "xsl:include : invalid URI reference %s\n", uriRef);
178 include = xsltLoadStyleDocument(style, URI);
179 if (include == NULL) {
180 xsltTransformError(NULL, style, cur,
181 "xsl:include : unable to load %s\n", URI);
186 style->doc = include->doc;
187 ret = (int)xsltParseStylesheetProcess(style, include->doc);
208 * @cur: the current XSLT stylesheet
210 * Find the next stylesheet in import precedence.
212 * Returns the next stylesheet or NULL if it was the last one
216 xsltNextImport(xsltStylesheetPtr cur) {
219 if (cur->imports != NULL)
220 return(cur->imports);
221 if (cur->next != NULL)
225 if (cur == NULL) return(NULL);
226 if (cur->next != NULL) return(cur->next);
227 } while (cur != NULL);
232 * xsltNeedElemSpaceHandling:
233 * @ctxt: an XSLT transformation context
235 * Checks whether that stylesheet requires white-space stripping
237 * Returns 1 if space should be stripped, 0 if not
241 xsltNeedElemSpaceHandling(xsltTransformContextPtr ctxt) {
242 xsltStylesheetPtr style;
247 while (style != NULL) {
248 if (style->stripSpaces != NULL)
250 style = xsltNextImport(style);
256 * xsltFindElemSpaceHandling:
257 * @ctxt: an XSLT transformation context
260 * Find strip-space or preserve-space informations for an element
261 * respect the import precedence or the wildcards
263 * Returns 1 if space should be stripped, 0 if not, and 2 if everything
264 * should be CDTATA wrapped.
268 xsltFindElemSpaceHandling(xsltTransformContextPtr ctxt, xmlNodePtr node) {
269 xsltStylesheetPtr style;
272 if ((ctxt == NULL) || (node == NULL))
275 while (style != NULL) {
276 if (node->ns != NULL) {
277 val = (const xmlChar *)
278 xmlHashLookup2(style->stripSpaces, node->name, node->ns->href);
280 val = (const xmlChar *)
281 xmlHashLookup2(style->stripSpaces, node->name, NULL);
284 if (xmlStrEqual(val, (xmlChar *) "strip"))
286 if (xmlStrEqual(val, (xmlChar *) "preserve"))
289 if (style->stripAll == 1)
291 if (style->stripAll == -1)
294 style = xsltNextImport(style);
301 * @ctxt: an XSLT transformation context
302 * @name: the template name
303 * @nameURI: the template name URI
305 * Finds the named template, apply import precedence rule.
307 * Returns the xsltTemplatePtr or NULL if not found
310 xsltFindTemplate(xsltTransformContextPtr ctxt, const xmlChar *name,
311 const xmlChar *nameURI) {
313 xsltStylesheetPtr style;
315 if ((ctxt == NULL) || (name == NULL))
318 while (style != NULL) {
319 cur = style->templates;
320 while (cur != NULL) {
321 if (xmlStrEqual(name, cur->name)) {
322 if (((nameURI == NULL) && (cur->nameURI == NULL)) ||
323 ((nameURI != NULL) && (cur->nameURI != NULL) &&
324 (xmlStrEqual(nameURI, cur->nameURI)))) {
331 style = xsltNextImport(style);