[LIBXSLT] Update to version 1.1.32. CORE-14291
[reactos.git] / dll / 3rdparty / libxslt / preproc.c
index b47d809..44d4983 100644 (file)
  * daniel@veillard.com
  */
 
-#define IN_LIBXSLT
-#include "libxslt.h"
-
-#include <string.h>
-
-#include <libxml/xmlmemory.h>
-#include <libxml/parser.h>
-#include <libxml/tree.h>
-#include <libxml/valid.h>
-#include <libxml/hash.h>
-#include <libxml/uri.h>
-#include <libxml/encoding.h>
-#include <libxml/xmlerror.h>
-#include "xslt.h"
-#include "xsltutils.h"
-#include "xsltInternals.h"
-#include "transform.h"
-#include "templates.h"
-#include "variables.h"
-#include "numbersInternals.h"
-#include "preproc.h"
-#include "extra.h"
-#include "imports.h"
-#include "extensions.h"
+#include "precomp.h"
 
 #ifdef WITH_XSLT_DEBUG
 #define WITH_XSLT_DEBUG_PREPROC
@@ -72,7 +49,7 @@ xsltCheckTopLevelElement(xsltStylesheetPtr style, xmlNodePtr inst, int err) {
     xmlNodePtr parent;
     if ((style == NULL) || (inst == NULL) || (inst->ns == NULL))
         return(-1);
-    
+
     parent = inst->parent;
     if (parent == NULL) {
         if (err) {
@@ -115,7 +92,7 @@ xsltCheckInstructionElement(xsltStylesheetPtr style, xmlNodePtr inst) {
         return;
 
     has_ext = (style->extInfos != NULL);
-    
+
     parent = inst->parent;
     if (parent == NULL) {
        xsltTransformError(NULL, style, inst,
@@ -141,7 +118,7 @@ xsltCheckInstructionElement(xsltStylesheetPtr style, xmlNodePtr inst) {
        if ((has_ext) && (parent->ns != NULL) &&
            (xmlHashLookup(style->extInfos, parent->ns->href) != NULL))
            return;
-       
+
         parent = parent->parent;
     }
     xsltTransformError(NULL, style, inst,
@@ -193,7 +170,7 @@ xsltCheckParentElement(xsltStylesheetPtr style, xmlNodePtr inst,
            if ((parent->ns != NULL) &&
                (xmlHashLookup(style->extInfos, parent->ns->href) != NULL))
                return;
-           
+
            parent = parent->parent;
        }
     }
@@ -229,7 +206,7 @@ xsltNewStylePreComp(xsltStylesheetPtr style, xsltStyleType type) {
 
     if (style == NULL)
         return(NULL);
-   
+
 #ifdef XSLT_REFACTORED
     /*
     * URGENT TODO: Use specialized factory functions in order
@@ -280,7 +257,7 @@ xsltNewStylePreComp(xsltStylesheetPtr style, xsltStyleType type) {
            size = sizeof(xsltStyleItemWhen); break;
        case XSLT_FUNC_OTHERWISE:
            size = sizeof(xsltStyleItemOtherwise); break;
-       default:        
+       default:
            xsltTransformError(NULL, style, NULL,
                    "xsltNewStylePreComp : invalid type %d\n", type);
            style->errors++;
@@ -353,8 +330,8 @@ xsltNewStylePreComp(xsltStylesheetPtr style, xsltStyleType type) {
         case XSLT_FUNC_DOCUMENT:
             cur->func = (xsltTransformFunction) xsltDocumentElem;break;
        case XSLT_FUNC_WITHPARAM:
-       case XSLT_FUNC_PARAM:       
-       case XSLT_FUNC_VARIABLE:            
+       case XSLT_FUNC_PARAM:
+       case XSLT_FUNC_VARIABLE:
        case XSLT_FUNC_WHEN:
            break;
        default:
@@ -419,7 +396,13 @@ xsltFreeStylePreComp(xsltStylePreCompPtr comp) {
                    xmlXPathFreeCompExpr(item->comp);
            }
             break;
-        case XSLT_FUNC_NUMBER:
+        case XSLT_FUNC_NUMBER: {
+                xsltStyleItemNumberPtr item = (xsltStyleItemNumberPtr) comp;
+                if (item->numdata.countPat != NULL)
+                    xsltFreeCompMatchList(item->numdata.countPat);
+                if (item->numdata.fromPat != NULL)
+                    xsltFreeCompMatchList(item->numdata.fromPat);
+            }
             break;
         case XSLT_FUNC_APPLYIMPORTS:
             break;
@@ -477,22 +460,26 @@ xsltFreeStylePreComp(xsltStylePreCompPtr comp) {
                    xmlXPathFreeCompExpr(item->comp);
            }
            break;
-       case XSLT_FUNC_OTHERWISE:           
+       case XSLT_FUNC_OTHERWISE:
        case XSLT_FUNC_FALLBACK:
        case XSLT_FUNC_MESSAGE:
        case XSLT_FUNC_INCLUDE:
        case XSLT_FUNC_ATTRSET:
-       
+
            break;
        default:
            /* TODO: Raise error. */
            break;
     }
-#else    
+#else
     if (comp->locale != (xsltLocale)0)
        xsltFreeLocale(comp->locale);
     if (comp->comp != NULL)
        xmlXPathFreeCompExpr(comp->comp);
+    if (comp->numdata.countPat != NULL)
+        xsltFreeCompMatchList(comp->numdata.countPat);
+    if (comp->numdata.fromPat != NULL)
+        xsltFreeCompMatchList(comp->numdata.fromPat);
     if (comp->nsList != NULL)
        xmlFree(comp->nsList);
 #endif
@@ -545,7 +532,7 @@ xsltDocumentComp(xsltStylesheetPtr style, xmlNodePtr inst,
 #else
     comp = xsltNewStylePreComp(style, XSLT_FUNC_DOCUMENT);
 #endif
-    
+
     if (comp == NULL)
        return (NULL);
     comp->inst = inst;
@@ -592,8 +579,8 @@ xsltDocumentComp(xsltStylesheetPtr style, xmlNodePtr inst,
 #ifdef WITH_XSLT_DEBUG_EXTRA
                xsltGenericDebug(xsltGenericDebugContext,
                    "Found xslt11:document construct\n");
-#endif                 
-           } else {            
+#endif
+           } else {
                if (xmlStrEqual(inst->ns->href,
                    (const xmlChar *)"http://exslt.org/common")) {
                    /* EXSLT. */
@@ -634,12 +621,12 @@ xsltDocumentComp(xsltStylesheetPtr style, xmlNodePtr inst,
        * 3) XSLT_XT_NAMESPACE (http://www.jclark.com/xt)
        *     Example: <xt:document method="xml" href="myFile.xml">
        *    TODO: is @href is an AVT?
-       *               
+       *
        * In all cases @href is in no namespace.
        */
        filename = xsltEvalStaticAttrValueTemplate(style, inst,
            (const xmlChar *)"href", NULL, &comp->has_filename);
-    }          
+    }
     if (!comp->has_filename) {
        goto error;
     }
@@ -669,7 +656,7 @@ xsltSortComp(xsltStylesheetPtr style, xmlNodePtr inst) {
 #else
     xsltStylePreCompPtr comp;
 #endif
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -677,7 +664,7 @@ xsltSortComp(xsltStylesheetPtr style, xmlNodePtr inst) {
 #else
     comp = xsltNewStylePreComp(style, XSLT_FUNC_SORT);
 #endif
-    
+
     if (comp == NULL)
        return;
     inst->psvi = comp;
@@ -777,14 +764,14 @@ xsltCopyComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 #ifdef XSLT_REFACTORED
     comp = (xsltStyleItemCopyPtr) xsltNewStylePreComp(style, XSLT_FUNC_COPY);
 #else
     comp = xsltNewStylePreComp(style, XSLT_FUNC_COPY);
 #endif
-    
+
     if (comp == NULL)
        return;
     inst->psvi = comp;
@@ -821,14 +808,14 @@ xsltTextComp(xsltStylesheetPtr style, xmlNodePtr inst) {
 #endif
     const xmlChar *prop;
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
     comp = (xsltStyleItemTextPtr) xsltNewStylePreComp(style, XSLT_FUNC_TEXT);
 #else
     comp = xsltNewStylePreComp(style, XSLT_FUNC_TEXT);
-#endif    
+#endif
     if (comp == NULL)
        return;
     inst->psvi = comp;
@@ -874,7 +861,7 @@ xsltElementComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     *   <!-- Content: template -->
     * </xsl:element>
     */
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -910,8 +897,8 @@ xsltElementComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     */
     comp->ns = xsltEvalStaticAttrValueTemplate(style, inst,
        (const xmlChar *)"namespace", NULL, &comp->has_ns);
-    
-    if (comp->name != NULL) {  
+
+    if (comp->name != NULL) {
        if (xmlValidateQName(comp->name, 0)) {
            xsltTransformError(NULL, style, inst,
                "xsl:element: The value '%s' of the attribute 'name' is "
@@ -921,7 +908,7 @@ xsltElementComp(xsltStylesheetPtr style, xmlNodePtr inst) {
            const xmlChar *prefix = NULL, *name;
 
            name = xsltSplitQName(style->dict, comp->name, &prefix);
-           if (comp->has_ns == 0) {        
+           if (comp->has_ns == 0) {
                xmlNsPtr ns;
 
                /*
@@ -930,7 +917,7 @@ xsltElementComp(xsltStylesheetPtr style, xmlNodePtr inst) {
                *  expanded into an expanded-name using the namespace declarations
                *  in effect for the xsl:element element, including any default
                *  namespace declaration.
-               */              
+               */
                ns = xmlSearchNs(inst->doc, inst, prefix);
                if (ns != NULL) {
                    comp->ns = xmlDictLookup(style->dict, ns->href, -1);
@@ -938,6 +925,8 @@ xsltElementComp(xsltStylesheetPtr style, xmlNodePtr inst) {
 #ifdef XSLT_REFACTORED
                    comp->nsPrefix = prefix;
                    comp->name = name;
+#else
+                    (void)name; /* Suppress unused variable warning. */
 #endif
                } else if (prefix != NULL) {
                    xsltTransformError(NULL, style, inst,
@@ -947,17 +936,17 @@ xsltElementComp(xsltStylesheetPtr style, xmlNodePtr inst) {
                        "not specified by the instruction itself.\n", comp->name);
                    style->errors++;
                }
-           }       
+           }
            if ((prefix != NULL) &&
                (!xmlStrncasecmp(prefix, (xmlChar *)"xml", 3)))
            {
                /*
                * Mark is to be skipped.
                */
-               comp->has_name = 0;             
+               comp->has_name = 0;
            }
        }
-    }    
+    }
     /*
     * Attribute "use-attribute-sets",
     */
@@ -965,7 +954,7 @@ xsltElementComp(xsltStylesheetPtr style, xmlNodePtr inst) {
                       (const xmlChar *)"use-attribute-sets",
                       NULL, &comp->has_use);
 
-error:    
+error:
     return;
 }
 
@@ -991,7 +980,7 @@ xsltAttributeComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     *   <!-- Content: template -->
     * </xsl:attribute>
     */
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1000,7 +989,7 @@ xsltAttributeComp(xsltStylesheetPtr style, xmlNodePtr inst) {
 #else
     comp = xsltNewStylePreComp(style, XSLT_FUNC_ATTRIBUTE);
 #endif
-    
+
     if (comp == NULL)
        return;
     inst->psvi = comp;
@@ -1020,7 +1009,7 @@ xsltAttributeComp(xsltStylesheetPtr style, xmlNodePtr inst) {
            "XSLT-attribute: The attribute 'name' is missing.\n");
        style->errors++;
        return;
-    }    
+    }
     /*
     * Attribute "namespace".
     */
@@ -1037,6 +1026,10 @@ xsltAttributeComp(xsltStylesheetPtr style, xmlNodePtr inst) {
                "xsl:attribute: The value '%s' of the attribute 'name' is "
                "not a valid QName.\n", comp->name);
            style->errors++;
+        } else if (xmlStrEqual(comp->name, BAD_CAST "xmlns")) {
+           xsltTransformError(NULL, style, inst,
+                "xsl:attribute: The attribute name 'xmlns' is not allowed.\n");
+           style->errors++;
        } else {
            const xmlChar *prefix = NULL, *name;
 
@@ -1051,7 +1044,7 @@ xsltAttributeComp(xsltStylesheetPtr style, xmlNodePtr inst) {
                    *  QName is expanded into an expanded-name using the
                    *  namespace declarations in effect for the xsl:element
                    *  element, including any default namespace declaration.
-                   */              
+                   */
                    ns = xmlSearchNs(inst->doc, inst, prefix);
                    if (ns != NULL) {
                        comp->ns = xmlDictLookup(style->dict, ns->href, -1);
@@ -1059,6 +1052,8 @@ xsltAttributeComp(xsltStylesheetPtr style, xmlNodePtr inst) {
 #ifdef XSLT_REFACTORED
                        comp->nsPrefix = prefix;
                        comp->name = name;
+#else
+                        (void)name; /* Suppress unused variable warning. */
 #endif
                    } else {
                        xsltTransformError(NULL, style, inst,
@@ -1070,29 +1065,8 @@ xsltAttributeComp(xsltStylesheetPtr style, xmlNodePtr inst) {
                        style->errors++;
                    }
                }
-               if (!xmlStrncasecmp(prefix, (xmlChar *) "xmlns", 5)) {
-                   /*
-                   * SPEC XSLT 1.0:
-                   *  "It is an error if the string that results from
-                   *  instantiating the attribute value template is not a
-                   *  QName or is the string xmlns. An XSLT processor may
-                   *  signal the error; if it does not signal the error,
-                   *  it must recover by not adding the attribute to the
-                   *  result tree."
-                   *
-                   * Reject a prefix of "xmlns". Mark to be skipped.
-                   */
-                   comp->has_name = 0;
-                   
-#ifdef WITH_XSLT_DEBUG_PARSING
-                   xsltGenericDebug(xsltGenericDebugContext,
-                       "xsltAttribute: xmlns prefix forbidden\n");
-#endif             
-                   return;
-               }
-               
            }
-       }       
+       }
     }
 }
 
@@ -1111,7 +1085,7 @@ xsltCommentComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1141,7 +1115,7 @@ xsltProcessingInstructionComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1175,7 +1149,7 @@ xsltCopyOfComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1222,7 +1196,7 @@ xsltValueOfComp(xsltStylesheetPtr style, xmlNodePtr inst) {
 #endif
     const xmlChar *prop;
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1307,7 +1281,8 @@ xsltGetQNameProperty(xsltStylesheetPtr style, xmlNodePtr inst,
            if (prop == NULL) {
                style->errors++;
            } else {
-               *localName = prop;
+               if (localName)
+                   *localName = prop;
                if (hasProp)
                    *hasProp = 1;
                if (URI != NULL) {
@@ -1347,7 +1322,7 @@ xsltWithParamComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1406,7 +1381,7 @@ xsltNumberComp(xsltStylesheetPtr style, xmlNodePtr cur) {
 #endif
     const xmlChar *prop;
 
-    if ((style == NULL) || (cur == NULL))
+    if ((style == NULL) || (cur == NULL) || (cur->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1419,14 +1394,11 @@ xsltNumberComp(xsltStylesheetPtr style, xmlNodePtr cur) {
        return;
     cur->psvi = comp;
 
-    if ((style == NULL) || (cur == NULL))
-       return;
-
     comp->numdata.doc = cur->doc;
     comp->numdata.node = cur;
     comp->numdata.value = xsltGetCNsProp(style, cur, (const xmlChar *)"value",
                                        XSLT_NAMESPACE);
-    
+
     prop = xsltEvalStaticAttrValueTemplate(style, cur,
                         (const xmlChar *)"format",
                         XSLT_NAMESPACE, &comp->numdata.has_format);
@@ -1437,10 +1409,22 @@ xsltNumberComp(xsltStylesheetPtr style, xmlNodePtr cur) {
     }
 
     comp->numdata.count = xsltGetCNsProp(style, cur, (const xmlChar *)"count",
-                                       XSLT_NAMESPACE);
+                                         XSLT_NAMESPACE);
     comp->numdata.from = xsltGetCNsProp(style, cur, (const xmlChar *)"from",
-                                       XSLT_NAMESPACE);
-    
+                                        XSLT_NAMESPACE);
+
+    prop = xsltGetCNsProp(style, cur, (const xmlChar *)"count", XSLT_NAMESPACE);
+    if (prop != NULL) {
+       comp->numdata.countPat = xsltCompilePattern(prop, cur->doc, cur, style,
+                                                    NULL);
+    }
+
+    prop = xsltGetCNsProp(style, cur, (const xmlChar *)"from", XSLT_NAMESPACE);
+    if (prop != NULL) {
+       comp->numdata.fromPat = xsltCompilePattern(prop, cur->doc, cur, style,
+                                                   NULL);
+    }
+
     prop = xsltGetCNsProp(style, cur, (const xmlChar *)"level", XSLT_NAMESPACE);
     if (prop != NULL) {
        if (xmlStrEqual(prop, BAD_CAST("single")) ||
@@ -1453,14 +1437,14 @@ xsltNumberComp(xsltStylesheetPtr style, xmlNodePtr cur) {
            if (style != NULL) style->warnings++;
        }
     }
-    
+
     prop = xsltGetCNsProp(style, cur, (const xmlChar *)"lang", XSLT_NAMESPACE);
     if (prop != NULL) {
            xsltTransformError(NULL, style, cur,
                 "xsl:number : lang attribute not implemented\n");
        XSLT_TODO; /* xsl:number lang attribute */
     }
-    
+
     prop = xsltGetCNsProp(style, cur, (const xmlChar *)"letter-value", XSLT_NAMESPACE);
     if (prop != NULL) {
        if (xmlStrEqual(prop, BAD_CAST("alphabetic"))) {
@@ -1479,7 +1463,7 @@ xsltNumberComp(xsltStylesheetPtr style, xmlNodePtr cur) {
            if (style != NULL) style->warnings++;
        }
     }
-    
+
     prop = xsltGetCNsProp(style, cur, (const xmlChar *)"grouping-separator",
                        XSLT_NAMESPACE);
     if (prop != NULL) {
@@ -1487,7 +1471,7 @@ xsltNumberComp(xsltStylesheetPtr style, xmlNodePtr cur) {
        comp->numdata.groupingCharacter =
            xsltGetUTF8Char(prop, &(comp->numdata.groupingCharacterLen));
     }
-    
+
     prop = xsltGetCNsProp(style, cur, (const xmlChar *)"grouping-size", XSLT_NAMESPACE);
     if (prop != NULL) {
        sscanf((char *)prop, "%d", &comp->numdata.digitsPerGroup);
@@ -1502,7 +1486,7 @@ xsltNumberComp(xsltStylesheetPtr style, xmlNodePtr cur) {
                                                BAD_CAST"single", 6);
        }
     }
-    
+
 }
 
 /**
@@ -1520,7 +1504,7 @@ xsltApplyImportsComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1550,7 +1534,7 @@ xsltCallTemplateComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1589,7 +1573,7 @@ xsltApplyTemplatesComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1641,7 +1625,7 @@ xsltChooseComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1672,7 +1656,7 @@ xsltIfComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1718,7 +1702,7 @@ xsltWhenComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1764,7 +1748,7 @@ xsltForEachComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1812,7 +1796,7 @@ xsltVariableComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1837,13 +1821,16 @@ xsltVariableComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltGetQNameProperty(style, inst, BAD_CAST "name",
        1, &(comp->has_name), &(comp->ns), &(comp->name));
     if (comp->ns)
-       comp->has_ns = 1;    
+       comp->has_ns = 1;
     /*
     * Attribute "select".
     */
     comp->select = xsltGetCNsProp(style, inst, (const xmlChar *)"select",
                                XSLT_NAMESPACE);
     if (comp->select != NULL) {
+#ifndef XSLT_REFACTORED
+        xmlNodePtr cur;
+#endif
        comp->comp = xsltXPathCompile(style, comp->select);
        if (comp->comp == NULL) {
            xsltTransformError(NULL, style, inst,
@@ -1851,12 +1838,25 @@ xsltVariableComp(xsltStylesheetPtr style, xmlNodePtr inst) {
                comp->select);
            style->errors++;
        }
+#ifdef XSLT_REFACTORED
        if (inst->children != NULL) {
            xsltTransformError(NULL, style, inst,
-               "XSLT-variable: The must be no child nodes, since the "
+               "XSLT-variable: There must be no child nodes, since the "
                "attribute 'select' was specified.\n");
            style->errors++;
        }
+#else
+        for (cur = inst->children; cur != NULL; cur = cur->next) {
+            if (cur->type != XML_COMMENT_NODE &&
+                (cur->type != XML_TEXT_NODE || !xsltIsBlank(cur->content)))
+            {
+                xsltTransformError(NULL, style, inst,
+                    "XSLT-variable: There must be no child nodes, since the "
+                    "attribute 'select' was specified.\n");
+                style->errors++;
+            }
+        }
+#endif
     }
 }
 
@@ -1875,7 +1875,7 @@ xsltParamComp(xsltStylesheetPtr style, xmlNodePtr inst) {
     xsltStylePreCompPtr comp;
 #endif
 
-    if ((style == NULL) || (inst == NULL))
+    if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE))
        return;
 
 #ifdef XSLT_REFACTORED
@@ -1936,11 +1936,11 @@ xsltFreeStylePreComps(xsltStylesheetPtr style) {
     xsltElemPreCompPtr cur, next;
 
     if (style == NULL)
-       return;        
-    
+       return;
+
     cur = style->preComps;
     while (cur != NULL) {
-       next = cur->next;               
+       next = cur->next;
        if (cur->type == XSLT_FUNC_EXTENSION)
            cur->free(cur);
        else
@@ -1962,12 +1962,12 @@ xsltFreeStylePreComps(xsltStylesheetPtr style) {
  */
 void
 xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr node) {
-    /*    
+    /*
     * The xsltXSLTElemMarker marker was set beforehand by
     *  the parsing mechanism for all elements in the XSLT namespace.
     */
     if (style == NULL) {
-       if (node != NULL)
+       if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
            node->psvi = NULL;
        return;
     }
@@ -1982,67 +1982,67 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr node) {
            case XSLT_FUNC_APPLYTEMPLATES:
                xsltApplyTemplatesComp(style, node);
                break;
-           case XSLT_FUNC_WITHPARAM:                      
+           case XSLT_FUNC_WITHPARAM:
                xsltWithParamComp(style, node);
                break;
-           case XSLT_FUNC_VALUEOF:         
+           case XSLT_FUNC_VALUEOF:
                xsltValueOfComp(style, node);
                break;
-           case XSLT_FUNC_COPY:            
+           case XSLT_FUNC_COPY:
                xsltCopyComp(style, node);
                break;
            case XSLT_FUNC_COPYOF:
                xsltCopyOfComp(style, node);
                break;
-           case XSLT_FUNC_IF:      
+           case XSLT_FUNC_IF:
                xsltIfComp(style, node);
                break;
-           case XSLT_FUNC_CHOOSE:          
+           case XSLT_FUNC_CHOOSE:
                xsltChooseComp(style, node);
                break;
-           case XSLT_FUNC_WHEN:            
+           case XSLT_FUNC_WHEN:
                xsltWhenComp(style, node);
                break;
-           case XSLT_FUNC_OTHERWISE:       
+           case XSLT_FUNC_OTHERWISE:
                /* NOP yet */
                return;
-           case XSLT_FUNC_FOREACH:         
+           case XSLT_FUNC_FOREACH:
                xsltForEachComp(style, node);
                break;
-           case XSLT_FUNC_APPLYIMPORTS:            
+           case XSLT_FUNC_APPLYIMPORTS:
                xsltApplyImportsComp(style, node);
                break;
-           case XSLT_FUNC_ATTRIBUTE:       
+           case XSLT_FUNC_ATTRIBUTE:
                xsltAttributeComp(style, node);
                break;
-           case XSLT_FUNC_ELEMENT:         
+           case XSLT_FUNC_ELEMENT:
                xsltElementComp(style, node);
                break;
-           case XSLT_FUNC_SORT:            
+           case XSLT_FUNC_SORT:
                xsltSortComp(style, node);
                break;
-           case XSLT_FUNC_COMMENT:         
+           case XSLT_FUNC_COMMENT:
                xsltCommentComp(style, node);
                break;
-           case XSLT_FUNC_NUMBER:          
+           case XSLT_FUNC_NUMBER:
                xsltNumberComp(style, node);
                break;
-           case XSLT_FUNC_PI:      
+           case XSLT_FUNC_PI:
                xsltProcessingInstructionComp(style, node);
                break;
-           case XSLT_FUNC_CALLTEMPLATE:            
+           case XSLT_FUNC_CALLTEMPLATE:
                xsltCallTemplateComp(style, node);
                break;
-           case XSLT_FUNC_PARAM:           
+           case XSLT_FUNC_PARAM:
                xsltParamComp(style, node);
                break;
-           case XSLT_FUNC_VARIABLE:        
+           case XSLT_FUNC_VARIABLE:
                xsltVariableComp(style, node);
                break;
-           case XSLT_FUNC_FALLBACK:        
+           case XSLT_FUNC_FALLBACK:
                /* NOP yet */
                return;
-           case XSLT_FUNC_DOCUMENT:        
+           case XSLT_FUNC_DOCUMENT:
                /* The extra one */
                node->psvi = (void *) xsltDocumentComp(style, node,
                    (xsltTransformFunction) xsltDocumentElem);
@@ -2065,7 +2065,7 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr node) {
     } else {
        /*
        * Fallback to string comparison.
-       */      
+       */
        if (IS_XSLT_NAME(node, "apply-templates")) {
            xsltApplyTemplatesComp(style, node);
        } else if (IS_XSLT_NAME(node, "with-param")) {
@@ -2081,7 +2081,7 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr node) {
        } else if (IS_XSLT_NAME(node, "choose")) {
            xsltChooseComp(style, node);
        } else if (IS_XSLT_NAME(node, "when")) {
-           xsltWhenComp(style, node);  
+           xsltWhenComp(style, node);
        } else if (IS_XSLT_NAME(node, "otherwise")) {
            /* NOP yet */
            return;
@@ -2113,7 +2113,7 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr node) {
        } else if (IS_XSLT_NAME(node, "document")) {
            /* The extra one */
            node->psvi = (void *) xsltDocumentComp(style, node,
-               (xsltTransformFunction) xsltDocumentElem);      
+               (xsltTransformFunction) xsltDocumentElem);
        } else if (IS_XSLT_NAME(node, "output")) {
            /* Top-level */
            return;
@@ -2122,7 +2122,7 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr node) {
            return;
        } else if (IS_XSLT_NAME(node, "strip-space")) {
            /* Top-level */
-           return;     
+           return;
        } else if (IS_XSLT_NAME(node, "key")) {
            /* Top-level */
            return;
@@ -2138,7 +2138,7 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr node) {
            /* Top-level */
            return;
        } else if (IS_XSLT_NAME(node, "include")) {
-           /* Top-level */                 
+           /* Top-level */
        } else {
            /*
            * NOTE that xsl:text, xsl:template, xsl:stylesheet,
@@ -2150,7 +2150,7 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr node) {
                "the XSLT element '%s'.\n", node->name);
                style->errors++;
            return;
-       }       
+       }
     }
     /*
     * Assign the current list of in-scope namespaces to the
@@ -2182,7 +2182,8 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr inst) {
     *   namespace- and local-name of the node, but can evaluate this
     *   using cctxt->style->inode->category;
     */
-    if (inst->psvi != NULL)
+    if ((inst == NULL) || (inst->type != XML_ELEMENT_NODE) ||
+        (inst->psvi != NULL))
        return;
 
     if (IS_XSLT_ELEM(inst)) {
@@ -2222,7 +2223,8 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr inst) {
        } else if (IS_XSLT_NAME(inst, "attribute")) {
            xmlNodePtr parent = inst->parent;
 
-           if ((parent == NULL) || (parent->ns == NULL) ||
+           if ((parent == NULL) ||
+               (parent->type != XML_ELEMENT_NODE) || (parent->ns == NULL) ||
                ((parent->ns != inst->ns) &&
                 (!xmlStrEqual(parent->ns->href, inst->ns->href))) ||
                (!xmlStrEqual(parent->name, BAD_CAST "attribute-set"))) {
@@ -2251,7 +2253,7 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr inst) {
        } else if (IS_XSLT_NAME(inst, "call-template")) {
            xsltCheckInstructionElement(style, inst);
            xsltCallTemplateComp(style, inst);
-       } else if (IS_XSLT_NAME(inst, "param")) {          
+       } else if (IS_XSLT_NAME(inst, "param")) {
            if (xsltCheckTopLevelElement(style, inst, 0) == 0)
                xsltCheckInstructionElement(style, inst);
            xsltParamComp(style, inst);
@@ -2314,12 +2316,12 @@ xsltStylePreCompute(xsltStylesheetPtr style, xmlNodePtr inst) {
            xsltCheckInstructionElement(style, inst);
            inst->psvi = (void *) xsltDocumentComp(style, inst,
                                (xsltTransformFunction) xsltDocumentElem);
-       } else {
+       } else if ((style == NULL) || (style->forwards_compatible == 0)) {
            xsltTransformError(NULL, style, inst,
                 "xsltStylePreCompute: unknown xsl:%s\n", inst->name);
            if (style != NULL) style->warnings++;
        }
-       
+
        cur = (xsltStylePreCompPtr) inst->psvi;
        /*
        * A ns-list is build for every XSLT item in the