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"
49 /************************************************************************
53 ************************************************************************/
56 * xsltParseStylesheetImport:
57 * @style: the XSLT stylesheet
58 * @template: the "strip-space" element
60 * parse an XSLT stylesheet strip-space element and record
61 * elements needing stripping. Returns zero on success and something else
66 xsltParseStylesheetImport(xsltStylesheetPtr style, xmlNodePtr cur) {
68 xmlDocPtr import = NULL;
70 xmlChar *uriRef = NULL;
72 xsltStylesheetPtr res;
74 if ((cur == NULL) || (style == NULL))
77 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
79 xsltPrintErrorContext(NULL, style, cur);
80 xsltGenericError(xsltGenericErrorContext,
81 "xsl:import : missing href attribute\n");
85 base = xmlNodeGetBase(style->doc, cur);
86 URI = xmlBuildURI(uriRef, base);
88 xsltPrintErrorContext(NULL, style, cur);
89 xsltGenericError(xsltGenericErrorContext,
90 "xsl:import : invalid URI reference %s\n", uriRef);
93 import = xmlParseFile((const char *)URI);
95 xsltPrintErrorContext(NULL, style, cur);
96 xsltGenericError(xsltGenericErrorContext,
97 "xsl:import : unable to load %s\n", URI);
101 res = xsltParseStylesheetDoc(import);
104 res->next = style->imports;
105 style->imports = res;
106 style->extrasNr += res->extrasNr;
124 * xsltParseStylesheetInclude:
125 * @style: the XSLT stylesheet
126 * @template: the "strip-space" element
128 * parse an XSLT stylesheet strip-space element and record
129 * elements needing stripping. Returns zero on success, something else
134 xsltParseStylesheetInclude(xsltStylesheetPtr style, xmlNodePtr cur) {
137 xmlChar *base = NULL;
138 xmlChar *uriRef = NULL;
140 xsltDocumentPtr include;
142 if ((cur == NULL) || (style == NULL))
145 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
146 if (uriRef == NULL) {
147 xsltPrintErrorContext(NULL, style, cur);
148 xsltGenericError(xsltGenericErrorContext,
149 "xsl:include : missing href attribute\n");
153 base = xmlNodeGetBase(style->doc, cur);
154 URI = xmlBuildURI(uriRef, base);
156 xsltPrintErrorContext(NULL, style, cur);
157 xsltGenericError(xsltGenericErrorContext,
158 "xsl:include : invalid URI reference %s\n", uriRef);
162 include = xsltLoadStyleDocument(style, URI);
163 if (include == NULL) {
164 xsltPrintErrorContext(NULL, style, cur);
165 xsltGenericError(xsltGenericErrorContext,
166 "xsl:include : unable to load %s\n", URI);
171 style->doc = include->doc;
172 ret = (int)xsltParseStylesheetProcess(style, include->doc);
193 * @cur: the current XSLT stylesheet
195 * Find the next stylesheet in import precedence.
197 * Returns the next stylesheet or NULL if it was the last one
201 xsltNextImport(xsltStylesheetPtr cur) {
204 if (cur->imports != NULL)
205 return(cur->imports);
206 if (cur->next != NULL)
210 if (cur == NULL) return(NULL);
211 if (cur->next != NULL) return(cur->next);
212 } while (cur != NULL);
217 * xsltNeedElemSpaceHandling:
218 * @ctxt: an XSLT transformation context
220 * Returns whether that stylesheet requires white-space stripping
222 * Returns 1 if space should be stripped, 0 if not
226 xsltNeedElemSpaceHandling(xsltTransformContextPtr ctxt) {
227 xsltStylesheetPtr style;
232 while (style != NULL) {
233 if (style->stripSpaces != NULL)
235 style = xsltNextImport(style);
241 * xsltFindElemSpaceHandling:
242 * @ctxt: an XSLT transformation context
245 * Find strip-space or preserve-space informations for an element
246 * respect the import precedence or the wildcards
248 * Returns 1 if space should be stripped, 0 if not, and 2 if everything
249 * should be CDTATA wrapped.
253 xsltFindElemSpaceHandling(xsltTransformContextPtr ctxt, xmlNodePtr node) {
254 xsltStylesheetPtr style;
257 if ((ctxt == NULL) || (node == NULL))
260 while (style != NULL) {
261 if (node->ns != NULL) {
262 val = (const xmlChar *)
263 xmlHashLookup2(style->stripSpaces, node->name, node->ns->href);
265 val = (const xmlChar *)
266 xmlHashLookup2(style->stripSpaces, node->name, NULL);
269 if (xmlStrEqual(val, (xmlChar *) "strip"))
271 if (xmlStrEqual(val, (xmlChar *) "preserve"))
274 if (ctxt->style->stripAll == 1)
276 if (ctxt->style->stripAll == -1)
279 style = xsltNextImport(style);
286 * @ctxt: an XSLT transformation context
287 * @name: the template name
288 * @nameURI: the template name URI
290 * Finds the named template, apply import precedence rule.
292 * Returns the xsltTemplatePtr or NULL if not found
295 xsltFindTemplate(xsltTransformContextPtr ctxt, const xmlChar *name,
296 const xmlChar *nameURI) {
298 xsltStylesheetPtr style;
300 if ((ctxt == NULL) || (name == NULL))
303 while (style != NULL) {
304 cur = style->templates;
305 while (cur != NULL) {
306 if (xmlStrEqual(name, cur->name)) {
307 if (((nameURI == NULL) && (cur->nameURI == NULL)) ||
308 ((nameURI != NULL) && (cur->nameURI != NULL) &&
309 (xmlStrEqual(nameURI, cur->nameURI)))) {
316 style = xsltNextImport(style);