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
65 xsltParseStylesheetImport(xsltStylesheetPtr style, xmlNodePtr cur) {
66 xmlDocPtr import = NULL;
68 xmlChar *uriRef = NULL;
70 xsltStylesheetPtr res;
72 if ((cur == NULL) || (style == NULL))
75 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
77 xsltPrintErrorContext(NULL, style, cur);
78 xsltGenericError(xsltGenericErrorContext,
79 "xsl:import : missing href attribute\n");
83 base = xmlNodeGetBase(style->doc, cur);
84 URI = xmlBuildURI(uriRef, base);
86 xsltPrintErrorContext(NULL, style, cur);
87 xsltGenericError(xsltGenericErrorContext,
88 "xsl:import : invalid URI reference %s\n", uriRef);
91 import = xmlParseFile((const char *)URI);
93 xsltPrintErrorContext(NULL, style, cur);
94 xsltGenericError(xsltGenericErrorContext,
95 "xsl:import : unable to load %s\n", URI);
99 res = xsltParseStylesheetDoc(import);
102 res->next = style->imports;
103 style->imports = res;
104 style->extrasNr += res->extrasNr;
117 * xsltParseStylesheetInclude:
118 * @style: the XSLT stylesheet
119 * @template: the "strip-space" element
121 * parse an XSLT stylesheet strip-space element and record
122 * elements needing stripping
126 xsltParseStylesheetInclude(xsltStylesheetPtr style, xmlNodePtr cur) {
128 xmlChar *base = NULL;
129 xmlChar *uriRef = NULL;
131 xsltDocumentPtr include;
133 if ((cur == NULL) || (style == NULL))
136 uriRef = xsltGetNsProp(cur, (const xmlChar *)"href", XSLT_NAMESPACE);
137 if (uriRef == NULL) {
138 xsltPrintErrorContext(NULL, style, cur);
139 xsltGenericError(xsltGenericErrorContext,
140 "xsl:include : missing href attribute\n");
144 base = xmlNodeGetBase(style->doc, cur);
145 URI = xmlBuildURI(uriRef, base);
147 xsltPrintErrorContext(NULL, style, cur);
148 xsltGenericError(xsltGenericErrorContext,
149 "xsl:include : invalid URI reference %s\n", uriRef);
153 include = xsltLoadStyleDocument(style, URI);
154 if (include == NULL) {
155 xsltPrintErrorContext(NULL, style, cur);
156 xsltGenericError(xsltGenericErrorContext,
157 "xsl:include : unable to load %s\n", URI);
162 style->doc = include->doc;
163 xsltParseStylesheetProcess(style, include->doc);
177 * @cur: the current XSLT stylesheet
179 * Find the next stylesheet in import precedence.
181 * Returns the next stylesheet or NULL if it was the last one
185 xsltNextImport(xsltStylesheetPtr cur) {
188 if (cur->imports != NULL)
189 return(cur->imports);
190 if (cur->next != NULL)
194 if (cur == NULL) return(NULL);
195 if (cur->next != NULL) return(cur->next);
196 } while (cur != NULL);
201 * xsltNeedElemSpaceHandling:
202 * @ctxt: an XSLT transformation context
204 * Returns whether that stylesheet requires white-space stripping
206 * Returns 1 if space should be stripped, 0 if not
210 xsltNeedElemSpaceHandling(xsltTransformContextPtr ctxt) {
211 xsltStylesheetPtr style;
216 while (style != NULL) {
217 if (style->stripSpaces != NULL)
219 style = xsltNextImport(style);
225 * xsltFindElemSpaceHandling:
226 * @ctxt: an XSLT transformation context
229 * Find strip-space or preserve-space informations for an element
230 * respect the import precedence or the wildcards
232 * Returns 1 if space should be stripped, 0 if not, and 2 if everything
233 * should be CDTATA wrapped.
237 xsltFindElemSpaceHandling(xsltTransformContextPtr ctxt, xmlNodePtr node) {
238 xsltStylesheetPtr style;
241 if ((ctxt == NULL) || (node == NULL))
244 while (style != NULL) {
245 /* TODO: add namespaces support */
246 val = (const xmlChar *)
247 xmlHashLookup(style->stripSpaces, node->name);
249 if (xmlStrEqual(val, (xmlChar *) "strip"))
251 if (xmlStrEqual(val, (xmlChar *) "preserve"))
254 if (ctxt->style->stripAll == 1)
256 if (ctxt->style->stripAll == -1)
259 style = xsltNextImport(style);
266 * @ctxt: an XSLT transformation context
267 * @name: the template name
268 * @nameURI: the template name URI
270 * Finds the named template, apply import precedence rule.
272 * Returns the xsltTemplatePtr or NULL if not found
275 xsltFindTemplate(xsltTransformContextPtr ctxt, const xmlChar *name,
276 const xmlChar *nameURI) {
278 xsltStylesheetPtr style;
280 if ((ctxt == NULL) || (name == NULL))
283 while (style != NULL) {
284 cur = style->templates;
285 while (cur != NULL) {
286 if (xmlStrEqual(name, cur->name)) {
287 if (((nameURI == NULL) && (cur->nameURI == NULL)) ||
288 ((nameURI != NULL) && (cur->nameURI != NULL) &&
289 (xmlStrEqual(nameURI, cur->nameURI)))) {
296 style = xsltNextImport(style);