Logo Search packages:      
Sourcecode: chromium-browser version File versions  Download package

xmlschemas.c

/*
 * schemas.c : implementation of the XML Schema handling and
 *             schema validity checking
 *
 * See Copyright for the status of this software.
 *
 * Daniel Veillard <veillard@redhat.com>
 */

/*
 * TODO:
 *   - when types are redefined in includes, check that all
 *     types in the redef list are equal
 *     -> need a type equality operation.
 *   - if we don't intend to use the schema for schemas, we
 *     need to validate all schema attributes (ref, type, name)
 *     against their types.
 *   - Eliminate item creation for: ??
 *
 * URGENT TODO:
 *   - For xsi-driven schema acquisition, augment the IDCs after every
 *     acquisition episode (xmlSchemaAugmentIDC).
 *
 * NOTES:
 *   - Elimated item creation for: <restriction>, <extension>,
 *     <simpleContent>, <complexContent>, <list>, <union>
 *
 * PROBLEMS:
 *   - http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0337.html
 *     IDC XPath expression and chameleon includes: the targetNamespace is changed, so
 *     XPath will have trouble to resolve to this namespace, since not known.
 *
 *
 * CONSTRAINTS:
 *
 * Schema Component Constraint:
 *   All Group Limited (cos-all-limited)
 *   Status: complete
 *   (1.2)
 *     In xmlSchemaGroupDefReferenceTermFixup() and
 *   (2)
 *     In xmlSchemaParseModelGroup()
 *     TODO: Actually this should go to component-level checks,
 *     but is done here due to performance. Move it to an other layer
 *     is schema construction via an API is implemented.
 */
#define IN_LIBXML
#include "libxml.h"

#ifdef LIBXML_SCHEMAS_ENABLED

#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/hash.h>
#include <libxml/uri.h>
#include <libxml/xmlschemas.h>
#include <libxml/schemasInternals.h>
#include <libxml/xmlschemastypes.h>
#include <libxml/xmlautomata.h>
#include <libxml/xmlregexp.h>
#include <libxml/dict.h>
#include <libxml/encoding.h>
#include <libxml/xmlIO.h>
#ifdef LIBXML_PATTERN_ENABLED
#include <libxml/pattern.h>
#endif
#ifdef LIBXML_READER_ENABLED
#include <libxml/xmlreader.h>
#endif

/* #define DEBUG 1 */

/* #define DEBUG_CONTENT 1 */

/* #define DEBUG_TYPE 1 */

/* #define DEBUG_CONTENT_REGEXP 1 */

/* #define DEBUG_AUTOMATA 1 */

/* #define DEBUG_IDC */

/* #define DEBUG_IDC_NODE_TABLE */

/* #define WXS_ELEM_DECL_CONS_ENABLED */

#ifdef DEBUG_IDC
 #ifndef DEBUG_IDC_NODE_TABLE
  #define DEBUG_IDC_NODE_TABLE
 #endif
#endif   

/* #define ENABLE_PARTICLE_RESTRICTION 1 */

#define ENABLE_REDEFINE

/* #define ENABLE_NAMED_LOCALS */

/* #define ENABLE_IDC_NODE_TABLES_TEST */

#define DUMP_CONTENT_MODEL

#ifdef LIBXML_READER_ENABLED
/* #define XML_SCHEMA_READER_ENABLED */
#endif

#define UNBOUNDED (1 << 30)
#define TODO                                                \
    xmlGenericError(xmlGenericErrorContext,                       \
          "Unimplemented block at %s:%d\n",                       \
            __FILE__, __LINE__);

#define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"

/*
 * The XML Schemas namespaces
 */
static const xmlChar *xmlSchemaNs = (const xmlChar *)
    "http://www.w3.org/2001/XMLSchema";

static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *)
    "http://www.w3.org/2001/XMLSchema-instance";

static const xmlChar *xmlNamespaceNs = (const xmlChar *)
    "http://www.w3.org/2000/xmlns/";

/*
* Come casting macros.
*/
#define ACTXT_CAST (xmlSchemaAbstractCtxtPtr)
#define PCTXT_CAST (xmlSchemaParserCtxtPtr)
#define VCTXT_CAST (xmlSchemaValidCtxtPtr)
#define WXS_BASIC_CAST (xmlSchemaBasicItemPtr)
#define WXS_TREE_CAST (xmlSchemaTreeItemPtr)
#define WXS_PTC_CAST (xmlSchemaParticlePtr)
#define WXS_TYPE_CAST (xmlSchemaTypePtr)
#define WXS_ELEM_CAST (xmlSchemaElementPtr)
#define WXS_ATTR_GROUP_CAST (xmlSchemaAttributeGroupPtr)
#define WXS_ATTR_CAST (xmlSchemaAttributePtr)
#define WXS_ATTR_USE_CAST (xmlSchemaAttributeUsePtr)
#define WXS_ATTR_PROHIB_CAST (xmlSchemaAttributeUseProhibPtr)
#define WXS_MODEL_GROUPDEF_CAST (xmlSchemaModelGroupDefPtr)
#define WXS_MODEL_GROUP_CAST (xmlSchemaModelGroupPtr)
#define WXS_IDC_CAST (xmlSchemaIDCPtr)
#define WXS_QNAME_CAST (xmlSchemaQNameRefPtr)
#define WXS_LIST_CAST (xmlSchemaItemListPtr)

/*
* Macros to query common properties of components.
*/
#define WXS_ITEM_NODE(i) xmlSchemaGetComponentNode(WXS_BASIC_CAST (i))

#define WXS_ITEM_TYPE_NAME(i) xmlSchemaGetComponentTypeStr(WXS_BASIC_CAST (i))
/*
* Macros for element declarations.
*/
#define WXS_ELEM_TYPEDEF(e) (e)->subtypes

#define WXS_SUBST_HEAD(item) (item)->refDecl
/*
* Macros for attribute declarations.
*/
#define WXS_ATTR_TYPEDEF(a) (a)->subtypes
/*
* Macros for attribute uses.
*/
#define WXS_ATTRUSE_DECL(au) WXS_ATTR_CAST (WXS_ATTR_USE_CAST (au))->attrDecl

#define WXS_ATTRUSE_TYPEDEF(au) WXS_ATTR_TYPEDEF(WXS_ATTRUSE_DECL( WXS_ATTR_USE_CAST au))

#define WXS_ATTRUSE_DECL_NAME(au) (WXS_ATTRUSE_DECL(au))->name

#define WXS_ATTRUSE_DECL_TNS(au) (WXS_ATTRUSE_DECL(au))->targetNamespace
/*
* Macros for attribute groups.
*/
#define WXS_ATTR_GROUP_HAS_REFS(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS)
#define WXS_ATTR_GROUP_EXPANDED(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED)
/*
* Macros for particles.
*/
#define WXS_PARTICLE(p) WXS_PTC_CAST (p)

#define WXS_PARTICLE_TERM(p) (WXS_PARTICLE(p))->children

#define WXS_PARTICLE_TERM_AS_ELEM(p) (WXS_ELEM_CAST WXS_PARTICLE_TERM(p))

#define WXS_PARTICLE_MODEL(p) WXS_MODEL_GROUP_CAST WXS_PARTICLE(p)->children
/*
* Macros for model groups definitions.
*/
#define WXS_MODELGROUPDEF_MODEL(mgd) (WXS_MODEL_GROUP_CAST (mgd))->children
/*
* Macros for model groups.
*/
#define WXS_IS_MODEL_GROUP(i) \
    (((i)->type == XML_SCHEMA_TYPE_SEQUENCE) || \
     ((i)->type == XML_SCHEMA_TYPE_CHOICE) || \
     ((i)->type == XML_SCHEMA_TYPE_ALL))

#define WXS_MODELGROUP_PARTICLE(mg) WXS_PTC_CAST (mg)->children
/*
* Macros for schema buckets.
*/
#define WXS_IS_BUCKET_INCREDEF(t) (((t) == XML_SCHEMA_SCHEMA_INCLUDE) || \
    ((t) == XML_SCHEMA_SCHEMA_REDEFINE))

#define WXS_IS_BUCKET_IMPMAIN(t) (((t) == XML_SCHEMA_SCHEMA_MAIN) || \
    ((t) == XML_SCHEMA_SCHEMA_IMPORT))

#define WXS_IMPBUCKET(b) ((xmlSchemaImportPtr) (b))

#define WXS_INCBUCKET(b) ((xmlSchemaIncludePtr) (b))
/*
* Macros for complex/simple types.
*/
#define WXS_IS_ANYTYPE(i) \
     (( (i)->type == XML_SCHEMA_TYPE_BASIC) && \
      ( (WXS_TYPE_CAST (i))->builtInType == XML_SCHEMAS_ANYTYPE))

#define WXS_IS_COMPLEX(i) \
    (((i)->type == XML_SCHEMA_TYPE_COMPLEX) || \
     ((i)->builtInType == XML_SCHEMAS_ANYTYPE))

#define WXS_IS_SIMPLE(item) \
    ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \
     ((item->type == XML_SCHEMA_TYPE_BASIC) && \
      (item->builtInType != XML_SCHEMAS_ANYTYPE)))

#define WXS_IS_ANY_SIMPLE_TYPE(i) \
    (((i)->type == XML_SCHEMA_TYPE_BASIC) && \
      ((i)->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))

#define WXS_IS_RESTRICTION(t) \
    ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)

#define WXS_IS_EXTENSION(t) \
    ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)

#define WXS_IS_TYPE_NOT_FIXED(i) \
    (((i)->type != XML_SCHEMA_TYPE_BASIC) && \
     (((i)->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))

#define WXS_IS_TYPE_NOT_FIXED_1(item) \
    (((item)->type != XML_SCHEMA_TYPE_BASIC) && \
     (((item)->flags & XML_SCHEMAS_TYPE_FIXUP_1) == 0))

#define WXS_TYPE_IS_GLOBAL(t) ((t)->flags & XML_SCHEMAS_TYPE_GLOBAL)

#define WXS_TYPE_IS_LOCAL(t) (((t)->flags & XML_SCHEMAS_TYPE_GLOBAL) == 0)
/*
* Macros for exclusively for complex types.
*/
#define WXS_HAS_COMPLEX_CONTENT(item) \
    ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \
     (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \
     (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))

#define WXS_HAS_SIMPLE_CONTENT(item) \
    ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
     (item->contentType == XML_SCHEMA_CONTENT_BASIC))

#define WXS_HAS_MIXED_CONTENT(item) \
    (item->contentType == XML_SCHEMA_CONTENT_MIXED)

#define WXS_EMPTIABLE(t) \
    (xmlSchemaIsParticleEmptiable(WXS_PTC_CAST (t)->subtypes))

#define WXS_TYPE_CONTENTTYPE(t) (t)->subtypes

#define WXS_TYPE_PARTICLE(t) WXS_PTC_CAST (t)->subtypes

#define WXS_TYPE_PARTICLE_TERM(t) WXS_PARTICLE_TERM(WXS_TYPE_PARTICLE(t))
/*
* Macros for exclusively for simple types.
*/
#define WXS_LIST_ITEMTYPE(t) (t)->subtypes

#define WXS_IS_ATOMIC(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)

#define WXS_IS_LIST(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)

#define WXS_IS_UNION(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
/*
* Misc parser context macros.
*/
#define WXS_CONSTRUCTOR(ctx) (ctx)->constructor

#define WXS_HAS_BUCKETS(ctx) \
( (WXS_CONSTRUCTOR((ctx))->buckets != NULL) && \
(WXS_CONSTRUCTOR((ctx))->buckets->nbItems > 0) )

#define WXS_SUBST_GROUPS(ctx) WXS_CONSTRUCTOR((ctx))->substGroups

#define WXS_BUCKET(ctx) WXS_CONSTRUCTOR((ctx))->bucket

#define WXS_SCHEMA(ctx) (ctx)->schema

#define WXS_ADD_LOCAL(ctx, item) \
    xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->locals), 10, item)

#define WXS_ADD_GLOBAL(ctx, item) \
    xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->globals), 5, item)

#define WXS_ADD_PENDING(ctx, item) \
    xmlSchemaAddItemSize(&((ctx)->constructor->pending), 10, item)
/*
* xmlSchemaItemList macros.
*/
#define WXS_ILIST_IS_EMPTY(l) ((l == NULL) || ((l)->nbItems == 0))
/*
* Misc macros.
*/
#define IS_SCHEMA(node, type) \
   ((node != NULL) && (node->ns != NULL) && \
    (xmlStrEqual(node->name, (const xmlChar *) type)) && \
    (xmlStrEqual(node->ns->href, xmlSchemaNs)))

#define FREE_AND_NULL(str) if ((str) != NULL) { xmlFree((xmlChar *) (str)); str = NULL; }

/*
* Since we put the default/fixed values into the dict, we can
* use pointer comparison for those values.
* REMOVED: (xmlStrEqual((v1), (v2)))
*/
#define WXS_ARE_DEFAULT_STR_EQUAL(v1, v2) ((v1) == (v2))

#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)

#define CAN_PARSE_SCHEMA(b) (((b)->doc != NULL) && ((b)->parsed == 0))

#define HFAILURE if (res == -1) goto exit_failure;

#define HERROR if (res != 0) goto exit_error;

#define HSTOP(ctx) if ((ctx)->stop) goto exit;
/*
* Some flags used for various schema constraints.
*/
#define SUBSET_RESTRICTION  1<<0
#define SUBSET_EXTENSION    1<<1
#define SUBSET_SUBSTITUTION 1<<2
#define SUBSET_LIST         1<<3
#define SUBSET_UNION        1<<4

typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;

typedef struct _xmlSchemaItemList xmlSchemaItemList;
typedef xmlSchemaItemList *xmlSchemaItemListPtr;
struct _xmlSchemaItemList {
    void **items;  /* used for dynamic addition of schemata */
    int nbItems; /* used for dynamic addition of schemata */
    int sizeItems; /* used for dynamic addition of schemata */
};

#define XML_SCHEMA_CTXT_PARSER 1
#define XML_SCHEMA_CTXT_VALIDATOR 2

typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
struct _xmlSchemaAbstractCtxt {
    int type; /* E.g. XML_SCHEMA_CTXT_VALIDATOR */
};

typedef struct _xmlSchemaBucket xmlSchemaBucket;
typedef xmlSchemaBucket *xmlSchemaBucketPtr;

#define XML_SCHEMA_SCHEMA_MAIN 0
#define XML_SCHEMA_SCHEMA_IMPORT 1
#define XML_SCHEMA_SCHEMA_INCLUDE 2
#define XML_SCHEMA_SCHEMA_REDEFINE 3

/**
 * xmlSchemaSchemaRelation: 
 *
 * Used to create a graph of schema relationships.
 */
typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
struct _xmlSchemaSchemaRelation {
    xmlSchemaSchemaRelationPtr next;
    int type; /* E.g. XML_SCHEMA_SCHEMA_IMPORT */
    const xmlChar *importNamespace;
    xmlSchemaBucketPtr bucket;
};

#define XML_SCHEMA_BUCKET_MARKED 1<<0
#define XML_SCHEMA_BUCKET_COMPS_ADDED 1<<1

struct _xmlSchemaBucket {
    int type;
    int flags;
    const xmlChar *schemaLocation;
    const xmlChar *origTargetNamespace;
    const xmlChar *targetNamespace;
    xmlDocPtr doc;
    xmlSchemaSchemaRelationPtr relations;
    int located;
    int parsed;
    int imported;
    int preserveDoc;
    xmlSchemaItemListPtr globals; /* Global components. */ 
    xmlSchemaItemListPtr locals; /* Local components. */
};

/**
 * xmlSchemaImport: 
 * (extends xmlSchemaBucket)
 *
 * Reflects a schema. Holds some information
 * about the schema and its toplevel components. Duplicate
 * toplevel components are not checked at this level.
 */
typedef struct _xmlSchemaImport xmlSchemaImport;
typedef xmlSchemaImport *xmlSchemaImportPtr;
struct _xmlSchemaImport {
    int type; /* Main OR import OR include. */
    int flags;
    const xmlChar *schemaLocation; /* The URI of the schema document. */
    /* For chameleon includes, @origTargetNamespace will be NULL */
    const xmlChar *origTargetNamespace;
    /* 
    * For chameleon includes, @targetNamespace will be the
    * targetNamespace of the including schema. 
    */
    const xmlChar *targetNamespace;
    xmlDocPtr doc; /* The schema node-tree. */
    /* @relations will hold any included/imported/redefined schemas. */
    xmlSchemaSchemaRelationPtr relations;
    int located;
    int parsed;
    int imported;
    int preserveDoc;
    xmlSchemaItemListPtr globals;
    xmlSchemaItemListPtr locals;
    /* The imported schema. */
    xmlSchemaPtr schema;
};

/*
* (extends xmlSchemaBucket)
*/
typedef struct _xmlSchemaInclude xmlSchemaInclude;
typedef xmlSchemaInclude *xmlSchemaIncludePtr;
struct _xmlSchemaInclude {
    int type;
    int flags;
    const xmlChar *schemaLocation;
    const xmlChar *origTargetNamespace;
    const xmlChar *targetNamespace;
    xmlDocPtr doc;
    xmlSchemaSchemaRelationPtr relations;
    int located;
    int parsed;
    int imported;
    int preserveDoc;
    xmlSchemaItemListPtr globals; /* Global components. */ 
    xmlSchemaItemListPtr locals; /* Local components. */

    /* The owning main or import schema bucket. */
    xmlSchemaImportPtr ownerImport;
};

/**
 * xmlSchemaBasicItem:
 *
 * The abstract base type for schema components.
 */
typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;
typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;
struct _xmlSchemaBasicItem {
    xmlSchemaTypeType type;
};

/**
 * xmlSchemaAnnotItem:
 *
 * The abstract base type for annotated schema components.
 * (Extends xmlSchemaBasicItem)
 */
typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;
typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;
struct _xmlSchemaAnnotItem {
    xmlSchemaTypeType type;
    xmlSchemaAnnotPtr annot;
};

/**
 * xmlSchemaTreeItem:
 *
 * The abstract base type for tree-like structured schema components.
 * (Extends xmlSchemaAnnotItem)
 */
typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
struct _xmlSchemaTreeItem {
    xmlSchemaTypeType type;
    xmlSchemaAnnotPtr annot;
    xmlSchemaTreeItemPtr next;
    xmlSchemaTreeItemPtr children;
};


#define XML_SCHEMA_ATTR_USE_FIXED 1<<0
/**
 * xmlSchemaAttributeUsePtr:
 *
 * The abstract base type for tree-like structured schema components.
 * (Extends xmlSchemaTreeItem)
 */
typedef struct _xmlSchemaAttributeUse xmlSchemaAttributeUse;
typedef xmlSchemaAttributeUse *xmlSchemaAttributeUsePtr;
struct _xmlSchemaAttributeUse {
    xmlSchemaTypeType type;
    xmlSchemaAnnotPtr annot;
    xmlSchemaAttributeUsePtr next; /* The next attr. use. */
    /* 
    * The attr. decl. OR a QName-ref. to an attr. decl. OR
    * a QName-ref. to an attribute group definition.
    */
    xmlSchemaAttributePtr attrDecl;

    int flags;
    xmlNodePtr node;
    int occurs; /* required, optional */
    const xmlChar * defValue;
    xmlSchemaValPtr defVal;
};

/**
 * xmlSchemaAttributeUseProhibPtr:
 *
 * A helper component to reflect attribute prohibitions.
 * (Extends xmlSchemaBasicItem)
 */
typedef struct _xmlSchemaAttributeUseProhib xmlSchemaAttributeUseProhib;
typedef xmlSchemaAttributeUseProhib *xmlSchemaAttributeUseProhibPtr;
struct _xmlSchemaAttributeUseProhib {
    xmlSchemaTypeType type; /* == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB */
    xmlNodePtr node;
    const xmlChar *name;
    const xmlChar *targetNamespace;
    int isRef;
};

/**
 * xmlSchemaRedef:
 */
typedef struct _xmlSchemaRedef xmlSchemaRedef;
typedef xmlSchemaRedef *xmlSchemaRedefPtr;
struct _xmlSchemaRedef {
    xmlSchemaRedefPtr next;
    xmlSchemaBasicItemPtr item; /* The redefining component. */
    xmlSchemaBasicItemPtr reference; /* The referencing component. */
    xmlSchemaBasicItemPtr target; /* The to-be-redefined component. */
    const xmlChar *refName; /* The name of the to-be-redefined component. */
    const xmlChar *refTargetNs; /* The target namespace of the
                                   to-be-redefined comp. */
    xmlSchemaBucketPtr targetBucket; /* The redefined schema. */
};

/**
 * xmlSchemaConstructionCtxt:
 */
typedef struct _xmlSchemaConstructionCtxt xmlSchemaConstructionCtxt;
typedef xmlSchemaConstructionCtxt *xmlSchemaConstructionCtxtPtr;
struct _xmlSchemaConstructionCtxt {
    xmlSchemaPtr mainSchema; /* The main schema. */
    xmlSchemaBucketPtr mainBucket; /* The main schema bucket */
    xmlDictPtr dict;
    xmlSchemaItemListPtr buckets; /* List of schema buckets. */
    /* xmlSchemaItemListPtr relations; */ /* List of schema relations. */
    xmlSchemaBucketPtr bucket; /* The current schema bucket */    
    xmlSchemaItemListPtr pending; /* All Components of all schemas that
                                     need to be fixed. */
    xmlHashTablePtr substGroups;
    xmlSchemaRedefPtr redefs;
    xmlSchemaRedefPtr lastRedef;
};

#define XML_SCHEMAS_PARSE_ERROR           1
#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT

struct _xmlSchemaParserCtxt {
    int type;
    void *errCtxt;             /* user specific error context */
    xmlSchemaValidityErrorFunc error;   /* the callback in case of errors */
    xmlSchemaValidityWarningFunc warning;       /* the callback in case of warning */
    int err;
    int nberrors;
    xmlStructuredErrorFunc serror;

    xmlSchemaConstructionCtxtPtr constructor;
    int ownsConstructor; /* TODO: Move this to parser *flags*. */

    /* xmlSchemaPtr topschema;      */
    /* xmlHashTablePtr namespaces;  */

    xmlSchemaPtr schema;        /* The main schema in use */
    int counter;

    const xmlChar *URL;
    xmlDocPtr doc;
    int preserve;       /* Whether the doc should be freed  */

    const char *buffer;
    int size;

    /*
     * Used to build complex element content models
     */
    xmlAutomataPtr am;
    xmlAutomataStatePtr start;
    xmlAutomataStatePtr end;
    xmlAutomataStatePtr state;

    xmlDictPtr dict;          /* dictionnary for interned string names */
    xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */
    int options;
    xmlSchemaValidCtxtPtr vctxt;
    int isS4S;
    int isRedefine;
    int xsiAssemble;
    int stop; /* If the parser should stop; i.e. a critical error. */
    const xmlChar *targetNamespace;
    xmlSchemaBucketPtr redefined; /* The schema to be redefined. */

    xmlSchemaRedefPtr redef; /* Used for redefinitions. */
    int redefCounter; /* Used for redefinitions. */ 
    xmlSchemaItemListPtr attrProhibs;
};

/**
 * xmlSchemaQNameRef:
 *
 * A component reference item (not a schema component)
 * (Extends xmlSchemaBasicItem)
 */
typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;
typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;
struct _xmlSchemaQNameRef {
    xmlSchemaTypeType type;
    xmlSchemaBasicItemPtr item; /* The resolved referenced item. */
    xmlSchemaTypeType itemType;
    const xmlChar *name;
    const xmlChar *targetNamespace;
    xmlNodePtr node;
};

/**
 * xmlSchemaParticle:
 *
 * A particle component.
 * (Extends xmlSchemaTreeItem)
 */
typedef struct _xmlSchemaParticle xmlSchemaParticle;
typedef xmlSchemaParticle *xmlSchemaParticlePtr;
struct _xmlSchemaParticle {
    xmlSchemaTypeType type;
    xmlSchemaAnnotPtr annot;
    xmlSchemaTreeItemPtr next; /* next particle */
    xmlSchemaTreeItemPtr children; /* the "term" (e.g. a model group,
      a group definition, a XML_SCHEMA_EXTRA_QNAMEREF (if a reference),
        etc.) */
    int minOccurs;
    int maxOccurs;
    xmlNodePtr node;
};

/**
 * xmlSchemaModelGroup:
 *
 * A model group component.
 * (Extends xmlSchemaTreeItem)
 */
typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
struct _xmlSchemaModelGroup {
    xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */
    xmlSchemaAnnotPtr annot;
    xmlSchemaTreeItemPtr next; /* not used */
    xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */
    xmlNodePtr node;
};

#define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
#define XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED 1<<1
/**
 * xmlSchemaModelGroupDef:
 *
 * A model group definition component.
 * (Extends xmlSchemaTreeItem)
 */
typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;
typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;
struct _xmlSchemaModelGroupDef {
    xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */
    xmlSchemaAnnotPtr annot;
    xmlSchemaTreeItemPtr next; /* not used */
    xmlSchemaTreeItemPtr children; /* the "model group" */
    const xmlChar *name;
    const xmlChar *targetNamespace;
    xmlNodePtr node;
    int flags;
};

typedef struct _xmlSchemaIDC xmlSchemaIDC;
typedef xmlSchemaIDC *xmlSchemaIDCPtr;

/**
 * xmlSchemaIDCSelect:
 *
 * The identity-constraint "field" and "selector" item, holding the
 * XPath expression.
 */
typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;
typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;
struct _xmlSchemaIDCSelect {
    xmlSchemaIDCSelectPtr next;
    xmlSchemaIDCPtr idc;
    int index; /* an index position if significant for IDC key-sequences */
    const xmlChar *xpath; /* the XPath expression */
    void *xpathComp; /* the compiled XPath expression */
};

/**
 * xmlSchemaIDC:
 *
 * The identity-constraint definition component.
 * (Extends xmlSchemaAnnotItem)
 */

struct _xmlSchemaIDC {
    xmlSchemaTypeType type;
    xmlSchemaAnnotPtr annot;
    xmlSchemaIDCPtr next;
    xmlNodePtr node;
    const xmlChar *name;
    const xmlChar *targetNamespace;
    xmlSchemaIDCSelectPtr selector;
    xmlSchemaIDCSelectPtr fields;
    int nbFields;
    xmlSchemaQNameRefPtr ref;
};

/**
 * xmlSchemaIDCAug:
 *
 * The augmented IDC information used for validation.
 */
typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;
typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;
struct _xmlSchemaIDCAug {
    xmlSchemaIDCAugPtr next; /* next in a list */
    xmlSchemaIDCPtr def; /* the IDC definition */
    int keyrefDepth; /* the lowest tree level to which IDC
                        tables need to be bubbled upwards */
};

/**
 * xmlSchemaPSVIIDCKeySequence:
 *
 * The key sequence of a node table item.
 */
typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;
typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;
struct _xmlSchemaPSVIIDCKey {
    xmlSchemaTypePtr type;
    xmlSchemaValPtr val;
};

/**
 * xmlSchemaPSVIIDCNode:
 *
 * The node table item of a node table.
 */
typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;
typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;
struct _xmlSchemaPSVIIDCNode {
    xmlNodePtr node;
    xmlSchemaPSVIIDCKeyPtr *keys;
    int nodeLine;
    int nodeQNameID;    

};

/**
 * xmlSchemaPSVIIDCBinding:
 *
 * The identity-constraint binding item of the [identity-constraint table].
 */
typedef struct _xmlSchemaPSVIIDCBinding xmlSchemaPSVIIDCBinding;
typedef xmlSchemaPSVIIDCBinding *xmlSchemaPSVIIDCBindingPtr;
struct _xmlSchemaPSVIIDCBinding {
    xmlSchemaPSVIIDCBindingPtr next; /* next binding of a specific node */
    xmlSchemaIDCPtr definition; /* the IDC definition */
    xmlSchemaPSVIIDCNodePtr *nodeTable; /* array of key-sequences */
    int nbNodes; /* number of entries in the node table */
    int sizeNodes; /* size of the node table */
    xmlSchemaItemListPtr dupls;
};


#define XPATH_STATE_OBJ_TYPE_IDC_SELECTOR 1
#define XPATH_STATE_OBJ_TYPE_IDC_FIELD 2

#define XPATH_STATE_OBJ_MATCHES -2
#define XPATH_STATE_OBJ_BLOCKED -3

typedef struct _xmlSchemaIDCMatcher xmlSchemaIDCMatcher;
typedef xmlSchemaIDCMatcher *xmlSchemaIDCMatcherPtr;

/**
 * xmlSchemaIDCStateObj:
 *
 * The state object used to evaluate XPath expressions.
 */
typedef struct _xmlSchemaIDCStateObj xmlSchemaIDCStateObj;
typedef xmlSchemaIDCStateObj *xmlSchemaIDCStateObjPtr;
struct _xmlSchemaIDCStateObj {
    int type;
    xmlSchemaIDCStateObjPtr next; /* next if in a list */
    int depth; /* depth of creation */
    int *history; /* list of (depth, state-id) tuples */
    int nbHistory;
    int sizeHistory;
    xmlSchemaIDCMatcherPtr matcher; /* the correspondent field/selector
                                       matcher */
    xmlSchemaIDCSelectPtr sel;
    void *xpathCtxt;
};

#define IDC_MATCHER 0

/**
 * xmlSchemaIDCMatcher:
 *
 * Used to evaluate IDC selectors (and fields).
 */
struct _xmlSchemaIDCMatcher {
    int type;
    int depth; /* the tree depth at creation time */
    xmlSchemaIDCMatcherPtr next; /* next in the list */
    xmlSchemaIDCMatcherPtr nextCached; /* next in the cache list */
    xmlSchemaIDCAugPtr aidc; /* the augmented IDC item */
    int idcType;
    xmlSchemaPSVIIDCKeyPtr **keySeqs; /* the key-sequences of the target
                                         elements */
    int sizeKeySeqs;
    xmlSchemaItemListPtr targets; /* list of target-node
                                     (xmlSchemaPSVIIDCNodePtr) entries */
};

/*
* Element info flags.
*/
#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES  1<<0
#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES 1<<1
#define XML_SCHEMA_ELEM_INFO_NILLED        1<<2
#define XML_SCHEMA_ELEM_INFO_LOCAL_TYPE          1<<3

#define XML_SCHEMA_NODE_INFO_VALUE_NEEDED      1<<4
#define XML_SCHEMA_ELEM_INFO_EMPTY             1<<5
#define XML_SCHEMA_ELEM_INFO_HAS_CONTENT       1<<6

#define XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT  1<<7
#define XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT  1<<8
#define XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED  1<<9
#define XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE  1<<10

/**
 * xmlSchemaNodeInfo:
 *
 * Holds information of an element node.
 */
struct _xmlSchemaNodeInfo {
    int nodeType;
    xmlNodePtr node;
    int nodeLine;    
    const xmlChar *localName;
    const xmlChar *nsName;
    const xmlChar *value;
    xmlSchemaValPtr val; /* the pre-computed value if any */
    xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */

    int flags; /* combination of node info flags */

    int valNeeded;
    int normVal;

    xmlSchemaElementPtr decl; /* the element/attribute declaration */
    int depth;
    xmlSchemaPSVIIDCBindingPtr idcTable; /* the table of PSVI IDC bindings
                                            for the scope element*/
    xmlSchemaIDCMatcherPtr idcMatchers; /* the IDC matchers for the scope
                                           element */
    xmlRegExecCtxtPtr regexCtxt;

    const xmlChar **nsBindings; /* Namespace bindings on this element */
    int nbNsBindings;
    int sizeNsBindings;    

    int hasKeyrefs;
    int appliedXPath; /* Indicates that an XPath has been applied. */
};

#define XML_SCHEMAS_ATTR_UNKNOWN 1
#define XML_SCHEMAS_ATTR_ASSESSED 2
#define XML_SCHEMAS_ATTR_PROHIBITED 3
#define XML_SCHEMAS_ATTR_ERR_MISSING 4
#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
#define XML_SCHEMAS_ATTR_DEFAULT 8
#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
#define XML_SCHEMAS_ATTR_WILD_SKIP 13
#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
#define XML_SCHEMAS_ATTR_META 17
/*
* @metaType values of xmlSchemaAttrInfo.
*/
#define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
#define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
#define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
#define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
#define XML_SCHEMA_ATTR_INFO_META_XMLNS 5

typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
struct _xmlSchemaAttrInfo {
    int nodeType;
    xmlNodePtr node;
    int nodeLine;    
    const xmlChar *localName;
    const xmlChar *nsName;
    const xmlChar *value;
    xmlSchemaValPtr val; /* the pre-computed value if any */
    xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
    int flags; /* combination of node info flags */

    xmlSchemaAttributePtr decl; /* the attribute declaration */
    xmlSchemaAttributeUsePtr use;  /* the attribute use */
    int state;
    int metaType;
    const xmlChar *vcValue; /* the value constraint value */
    xmlSchemaNodeInfoPtr parent;
};


#define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
/**
 * xmlSchemaValidCtxt:
 *
 * A Schemas validation context
 */
struct _xmlSchemaValidCtxt {
    int type;
    void *errCtxt;             /* user specific data block */
    xmlSchemaValidityErrorFunc error;   /* the callback in case of errors */
    xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
    xmlStructuredErrorFunc serror;

    xmlSchemaPtr schema;        /* The schema in use */
    xmlDocPtr doc;
    xmlParserInputBufferPtr input;
    xmlCharEncoding enc;
    xmlSAXHandlerPtr sax;
    xmlParserCtxtPtr parserCtxt;
    void *user_data; /* TODO: What is this for? */

    int err;
    int nberrors;

    xmlNodePtr node;
    xmlNodePtr cur;
    /* xmlSchemaTypePtr type; */

    xmlRegExecCtxtPtr regexp;
    xmlSchemaValPtr value;

    int valueWS;
    int options;
    xmlNodePtr validationRoot;
    xmlSchemaParserCtxtPtr pctxt;
    int xsiAssemble;

    int depth;
    xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
    int sizeElemInfos;
    xmlSchemaNodeInfoPtr inode; /* the current element information */

    xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */

    xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
    xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
    xmlSchemaIDCMatcherPtr idcMatcherCache; /* Cache for IDC matcher objects. */

    xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
    int nbIdcNodes;
    int sizeIdcNodes;

    xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
    int nbIdcKeys;
    int sizeIdcKeys;

    int flags;

    xmlDictPtr dict;

#ifdef LIBXML_READER_ENABLED
    xmlTextReaderPtr reader;
#endif

    xmlSchemaAttrInfoPtr *attrInfos;
    int nbAttrInfos;
    int sizeAttrInfos;

    int skipDepth;
    xmlSchemaItemListPtr nodeQNames;
    int hasKeyrefs;
    int createIDCNodeTables;
    int psviExposeIDCNodeTables;
};

/**
 * xmlSchemaSubstGroup:
 *
 *
 */
typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
struct _xmlSchemaSubstGroup {
    xmlSchemaElementPtr head;
    xmlSchemaItemListPtr members;
};

/************************************************************************
 *                                                    *
 *                Some predeclarations                      *
 *                                                    *
 ************************************************************************/

static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
                                 xmlSchemaPtr schema,
                                 xmlNodePtr node);
static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
                                 xmlSchemaPtr schema,
                                 xmlNodePtr node);
static int
xmlSchemaTypeFixup(xmlSchemaTypePtr type,
                   xmlSchemaAbstractCtxtPtr ctxt);
static const xmlChar *
xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
static int
xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                     xmlNodePtr node);
static int
xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
                       xmlSchemaParserCtxtPtr ctxt);
static void
xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
static xmlSchemaWhitespaceValueType
xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
static xmlSchemaTreeItemPtr
xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                   xmlNodePtr node, xmlSchemaTypeType type,
                   int withParticle);
static const xmlChar *
xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
static xmlSchemaTypeLinkPtr
xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
static void
xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
                 const char *funcName,
                 const char *message);
static int
xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr ctxt,
                       xmlSchemaTypePtr type,
                       xmlSchemaTypePtr baseType,
                       int subset);
static void
xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
                           xmlSchemaParserCtxtPtr ctxt);
static void
xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
static xmlSchemaQNameRefPtr
xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
                        xmlSchemaPtr schema,
                        xmlNodePtr node);

/************************************************************************
 *                                                    *
 *                Helper functions                      *
 *                                                    *
 ************************************************************************/

/**
 * xmlSchemaItemTypeToStr:
 * @type: the type of the schema item
 *
 * Returns the component name of a schema item.
 */
static const xmlChar *
xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
{
    switch (type) {
      case XML_SCHEMA_TYPE_BASIC:
          return(BAD_CAST "simple type definition");
      case XML_SCHEMA_TYPE_SIMPLE:
          return(BAD_CAST "simple type definition");
      case XML_SCHEMA_TYPE_COMPLEX:
          return(BAD_CAST "complex type definition");
      case XML_SCHEMA_TYPE_ELEMENT:
          return(BAD_CAST "element declaration");
      case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
          return(BAD_CAST "attribute use");
      case XML_SCHEMA_TYPE_ATTRIBUTE:
          return(BAD_CAST "attribute declaration");
      case XML_SCHEMA_TYPE_GROUP:
          return(BAD_CAST "model group definition");
      case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
          return(BAD_CAST "attribute group definition");
      case XML_SCHEMA_TYPE_NOTATION:
          return(BAD_CAST "notation declaration");
      case XML_SCHEMA_TYPE_SEQUENCE:
          return(BAD_CAST "model group (sequence)");
      case XML_SCHEMA_TYPE_CHOICE:
          return(BAD_CAST "model group (choice)");
      case XML_SCHEMA_TYPE_ALL:
          return(BAD_CAST "model group (all)");
      case XML_SCHEMA_TYPE_PARTICLE:
          return(BAD_CAST "particle");
      case XML_SCHEMA_TYPE_IDC_UNIQUE:
          return(BAD_CAST "unique identity-constraint");
          /* return(BAD_CAST "IDC (unique)"); */
      case XML_SCHEMA_TYPE_IDC_KEY:
          return(BAD_CAST "key identity-constraint");
          /* return(BAD_CAST "IDC (key)"); */
      case XML_SCHEMA_TYPE_IDC_KEYREF:
          return(BAD_CAST "keyref identity-constraint");
          /* return(BAD_CAST "IDC (keyref)"); */
      case XML_SCHEMA_TYPE_ANY:
          return(BAD_CAST "wildcard (any)");
      case XML_SCHEMA_EXTRA_QNAMEREF:
          return(BAD_CAST "[helper component] QName reference");
      case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
          return(BAD_CAST "[helper component] attribute use prohibition");
      default:
          return(BAD_CAST "Not a schema component");
    }
}

/**
 * xmlSchemaGetComponentTypeStr:
 * @type: the type of the schema item
 *
 * Returns the component name of a schema item.
 */
static const xmlChar *
xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
{
    switch (item->type) {
      case XML_SCHEMA_TYPE_BASIC:
          if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
            return(BAD_CAST "complex type definition");
          else
            return(BAD_CAST "simple type definition");      
      default:
          return(xmlSchemaItemTypeToStr(item->type));
    }
}

/**
 * xmlSchemaGetComponentNode:
 * @item: a schema component
 *
 * Returns node associated with the schema component.
 * NOTE that such a node need not be available; plus, a component's
 * node need not to reflect the component directly, since there is no
 * one-to-one relationship between the XML Schema representation and
 * the component representation.
 */
static xmlNodePtr
xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
{
    switch (item->type) {
      case XML_SCHEMA_TYPE_ELEMENT:
          return (((xmlSchemaElementPtr) item)->node);
      case XML_SCHEMA_TYPE_ATTRIBUTE:
          return (((xmlSchemaAttributePtr) item)->node);
      case XML_SCHEMA_TYPE_COMPLEX:
      case XML_SCHEMA_TYPE_SIMPLE:
          return (((xmlSchemaTypePtr) item)->node);
      case XML_SCHEMA_TYPE_ANY:
      case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
          return (((xmlSchemaWildcardPtr) item)->node);
      case XML_SCHEMA_TYPE_PARTICLE:
          return (((xmlSchemaParticlePtr) item)->node);
      case XML_SCHEMA_TYPE_SEQUENCE:
      case XML_SCHEMA_TYPE_CHOICE:
      case XML_SCHEMA_TYPE_ALL:
          return (((xmlSchemaModelGroupPtr) item)->node);
      case XML_SCHEMA_TYPE_GROUP:
          return (((xmlSchemaModelGroupDefPtr) item)->node);
      case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
          return (((xmlSchemaAttributeGroupPtr) item)->node);
      case XML_SCHEMA_TYPE_IDC_UNIQUE:
      case XML_SCHEMA_TYPE_IDC_KEY:
      case XML_SCHEMA_TYPE_IDC_KEYREF:
          return (((xmlSchemaIDCPtr) item)->node);
      case XML_SCHEMA_EXTRA_QNAMEREF:
          return(((xmlSchemaQNameRefPtr) item)->node);
      /* TODO: What to do with NOTATIONs?
      case XML_SCHEMA_TYPE_NOTATION:
          return (((xmlSchemaNotationPtr) item)->node);
      */
      case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
          return (((xmlSchemaAttributeUsePtr) item)->node);
      default:
          return (NULL);
    }
}

#if 0
/**
 * xmlSchemaGetNextComponent:
 * @item: a schema component
 *
 * Returns the next sibling of the schema component.
 */
static xmlSchemaBasicItemPtr
xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
{
    switch (item->type) {
      case XML_SCHEMA_TYPE_ELEMENT:
          return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
      case XML_SCHEMA_TYPE_ATTRIBUTE:
          return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
      case XML_SCHEMA_TYPE_COMPLEX:
      case XML_SCHEMA_TYPE_SIMPLE:
          return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
      case XML_SCHEMA_TYPE_ANY:
      case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
          return (NULL);
      case XML_SCHEMA_TYPE_PARTICLE:
          return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
      case XML_SCHEMA_TYPE_SEQUENCE:
      case XML_SCHEMA_TYPE_CHOICE:
      case XML_SCHEMA_TYPE_ALL:
          return (NULL);
      case XML_SCHEMA_TYPE_GROUP:
          return (NULL);
      case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
          return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
      case XML_SCHEMA_TYPE_IDC_UNIQUE:
      case XML_SCHEMA_TYPE_IDC_KEY:
      case XML_SCHEMA_TYPE_IDC_KEYREF:
          return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
      default:
          return (NULL);
    }
}
#endif


/**
 * xmlSchemaFormatQName:
 * @buf: the string buffer
 * @namespaceName:  the namespace name
 * @localName: the local name
 *
 * Returns the given QName in the format "{namespaceName}localName" or
 * just "localName" if @namespaceName is NULL.
 *
 * Returns the localName if @namespaceName is NULL, a formatted
 * string otherwise.
 */
static const xmlChar*
xmlSchemaFormatQName(xmlChar **buf,
                 const xmlChar *namespaceName,
                 const xmlChar *localName)
{
    FREE_AND_NULL(*buf)
    if (namespaceName != NULL) {
      *buf = xmlStrdup(BAD_CAST "{");
      *buf = xmlStrcat(*buf, namespaceName);
      *buf = xmlStrcat(*buf, BAD_CAST "}");
    }
    if (localName != NULL) {
      if (namespaceName == NULL)
          return(localName);
      *buf = xmlStrcat(*buf, localName);
    } else {
      *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
    }    
    return ((const xmlChar *) *buf);
}

static const xmlChar*   
xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
{
    if (ns != NULL)
      return (xmlSchemaFormatQName(buf, ns->href, localName));
    else
      return (xmlSchemaFormatQName(buf, NULL, localName));
}

static const xmlChar *
xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
{
    switch (item->type) {
      case XML_SCHEMA_TYPE_ELEMENT:
          return (((xmlSchemaElementPtr) item)->name);
      case XML_SCHEMA_TYPE_ATTRIBUTE:
          return (((xmlSchemaAttributePtr) item)->name);
      case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
          return (((xmlSchemaAttributeGroupPtr) item)->name);
      case XML_SCHEMA_TYPE_BASIC:
      case XML_SCHEMA_TYPE_SIMPLE:
      case XML_SCHEMA_TYPE_COMPLEX:
          return (((xmlSchemaTypePtr) item)->name);
      case XML_SCHEMA_TYPE_GROUP:
          return (((xmlSchemaModelGroupDefPtr) item)->name);
      case XML_SCHEMA_TYPE_IDC_KEY:
      case XML_SCHEMA_TYPE_IDC_UNIQUE:
      case XML_SCHEMA_TYPE_IDC_KEYREF:
          return (((xmlSchemaIDCPtr) item)->name);
      case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
          if (WXS_ATTRUSE_DECL(item) != NULL) {
            return(xmlSchemaGetComponentName(
                WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
          } else
            return(NULL);
      case XML_SCHEMA_EXTRA_QNAMEREF:
          return (((xmlSchemaQNameRefPtr) item)->name);
      case XML_SCHEMA_TYPE_NOTATION:
          return (((xmlSchemaNotationPtr) item)->name);
      default:
          /*
          * Other components cannot have names.
          */
          break;
    }
    return (NULL);
}

#define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
#define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
/*
static const xmlChar *
xmlSchemaGetQNameRefName(void *ref)
{
    return(((xmlSchemaQNameRefPtr) ref)->name);
}

static const xmlChar *
xmlSchemaGetQNameRefTargetNs(void *ref)
{
    return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
}
*/

static const xmlChar *
xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
{
    switch (item->type) {
      case XML_SCHEMA_TYPE_ELEMENT:
          return (((xmlSchemaElementPtr) item)->targetNamespace);
      case XML_SCHEMA_TYPE_ATTRIBUTE:
          return (((xmlSchemaAttributePtr) item)->targetNamespace);
      case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
          return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
      case XML_SCHEMA_TYPE_BASIC:
          return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
      case XML_SCHEMA_TYPE_SIMPLE:
      case XML_SCHEMA_TYPE_COMPLEX:
          return (((xmlSchemaTypePtr) item)->targetNamespace);
      case XML_SCHEMA_TYPE_GROUP:
          return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
      case XML_SCHEMA_TYPE_IDC_KEY:
      case XML_SCHEMA_TYPE_IDC_UNIQUE:
      case XML_SCHEMA_TYPE_IDC_KEYREF:
          return (((xmlSchemaIDCPtr) item)->targetNamespace);
      case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
          if (WXS_ATTRUSE_DECL(item) != NULL) {
            return(xmlSchemaGetComponentTargetNs(
                WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
          }
          /* TODO: Will returning NULL break something? */
          break;
      case XML_SCHEMA_EXTRA_QNAMEREF:
          return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
      case XML_SCHEMA_TYPE_NOTATION:
          return (((xmlSchemaNotationPtr) item)->targetNamespace);
      default:
          /*
          * Other components cannot have names.
          */
          break;
    }
    return (NULL);
}

static const xmlChar*
xmlSchemaGetComponentQName(xmlChar **buf,
                     void *item)
{
    return (xmlSchemaFormatQName(buf,
      xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
      xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
}

static const xmlChar*
xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
{
    xmlChar *str = NULL;

    *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
    *buf = xmlStrcat(*buf, BAD_CAST " '");
    *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
      (xmlSchemaBasicItemPtr) item));
    *buf = xmlStrcat(*buf, BAD_CAST "'");
    FREE_AND_NULL(str);
    return(*buf);
}

static const xmlChar*
xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
{
    return(xmlSchemaGetComponentDesignation(buf, idc));
}

/**
 * xmlSchemaWildcardPCToString:
 * @pc: the type of processContents
 *
 * Returns a string representation of the type of
 * processContents.
 */
static const xmlChar *
xmlSchemaWildcardPCToString(int pc)
{
    switch (pc) {
      case XML_SCHEMAS_ANY_SKIP:
          return (BAD_CAST "skip");
      case XML_SCHEMAS_ANY_LAX:
          return (BAD_CAST "lax");
      case XML_SCHEMAS_ANY_STRICT:
          return (BAD_CAST "strict");
      default:
          return (BAD_CAST "invalid process contents");
    }
}

/**
 * xmlSchemaGetCanonValueWhtspExt:
 * @val: the precomputed value
 * @retValue: the returned value
 * @ws: the whitespace type of the value
 *
 * Get a the cononical representation of the value.
 * The caller has to free the returned retValue.
 *
 * Returns 0 if the value could be built and -1 in case of
 *         API errors or if the value type is not supported yet.
 */
static int
xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
                         xmlSchemaWhitespaceValueType ws,
                         xmlChar **retValue)
{
    int list;
    xmlSchemaValType valType;
    const xmlChar *value, *value2 = NULL;
    

    if ((retValue == NULL) || (val == NULL))
      return (-1);
    list = xmlSchemaValueGetNext(val) ? 1 : 0;
    *retValue = NULL;
    do {
      value = NULL;     
      valType = xmlSchemaGetValType(val);    
      switch (valType) {          
          case XML_SCHEMAS_STRING:
          case XML_SCHEMAS_NORMSTRING:
          case XML_SCHEMAS_ANYSIMPLETYPE:
            value = xmlSchemaValueGetAsString(val);
            if (value != NULL) {
                if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
                  value2 = xmlSchemaCollapseString(value);
                else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
                  value2 = xmlSchemaWhiteSpaceReplace(value);
                if (value2 != NULL)
                  value = value2;
            }
            break;         
          default:
            if (xmlSchemaGetCanonValue(val, &value2) == -1) {
                if (value2 != NULL)
                  xmlFree((xmlChar *) value2);
                goto internal_error;
            }
            value = value2;
      }
      if (*retValue == NULL)
          if (value == NULL) {
            if (! list)
                *retValue = xmlStrdup(BAD_CAST "");
          } else
            *retValue = xmlStrdup(value);
      else if (value != NULL) {
          /* List. */
          *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
          *retValue = xmlStrcat((xmlChar *) *retValue, value);
      }
      FREE_AND_NULL(value2)
      val = xmlSchemaValueGetNext(val);
    } while (val != NULL);

    return (0);
internal_error:
    if (*retValue != NULL)
      xmlFree((xmlChar *) (*retValue));
    if (value2 != NULL)
      xmlFree((xmlChar *) value2);
    return (-1);
}

/**
 * xmlSchemaFormatItemForReport:
 * @buf: the string buffer
 * @itemDes: the designation of the item
 * @itemName: the name of the item
 * @item: the item as an object 
 * @itemNode: the node of the item
 * @local: the local name
 * @parsing: if the function is used during the parse
 *
 * Returns a representation of the given item used
 * for error reports. 
 *
 * The following order is used to build the resulting 
 * designation if the arguments are not NULL:
 * 1a. If itemDes not NULL -> itemDes
 * 1b. If (itemDes not NULL) and (itemName not NULL)
 *     -> itemDes + itemName
 * 2. If the preceding was NULL and (item not NULL) -> item
 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
 * 
 * If the itemNode is an attribute node, the name of the attribute
 * will be appended to the result.
 *
 * Returns the formatted string and sets @buf to the resulting value.
 */  
static xmlChar*   
xmlSchemaFormatItemForReport(xmlChar **buf,                
                 const xmlChar *itemDes,
                 xmlSchemaBasicItemPtr item,
                 xmlNodePtr itemNode)
{
    xmlChar *str = NULL;
    int named = 1;

    if (*buf != NULL) {
      xmlFree(*buf);
      *buf = NULL;
    }
            
    if (itemDes != NULL) {
      *buf = xmlStrdup(itemDes);    
    } else if (item != NULL) {
      switch (item->type) {
      case XML_SCHEMA_TYPE_BASIC: {
          xmlSchemaTypePtr type = WXS_TYPE_CAST item;

          if (WXS_IS_ATOMIC(type))
            *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
          else if (WXS_IS_LIST(type))
            *buf = xmlStrdup(BAD_CAST "list type 'xs:");
          else if (WXS_IS_UNION(type))
            *buf = xmlStrdup(BAD_CAST "union type 'xs:");
          else
            *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
          *buf = xmlStrcat(*buf, type->name);
          *buf = xmlStrcat(*buf, BAD_CAST "'");
          }
          break;
      case XML_SCHEMA_TYPE_SIMPLE: {
          xmlSchemaTypePtr type = WXS_TYPE_CAST item;

          if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
            *buf = xmlStrdup(BAD_CAST"");
          } else {
            *buf = xmlStrdup(BAD_CAST "local ");
          }
          if (WXS_IS_ATOMIC(type))
            *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
          else if (WXS_IS_LIST(type))
            *buf = xmlStrcat(*buf, BAD_CAST "list type");
          else if (WXS_IS_UNION(type))
            *buf = xmlStrcat(*buf, BAD_CAST "union type");
          else
            *buf = xmlStrcat(*buf, BAD_CAST "simple type");
          if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
            *buf = xmlStrcat(*buf, BAD_CAST " '");
            *buf = xmlStrcat(*buf, type->name);
            *buf = xmlStrcat(*buf, BAD_CAST "'");
          }
          }
          break;
      case XML_SCHEMA_TYPE_COMPLEX: {
          xmlSchemaTypePtr type = WXS_TYPE_CAST item;

          if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
            *buf = xmlStrdup(BAD_CAST "");
          else
            *buf = xmlStrdup(BAD_CAST "local ");
          *buf = xmlStrcat(*buf, BAD_CAST "complex type");
          if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
            *buf = xmlStrcat(*buf, BAD_CAST " '");
            *buf = xmlStrcat(*buf, type->name);
            *buf = xmlStrcat(*buf, BAD_CAST "'");
          }
          }
          break;
      case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
            xmlSchemaAttributeUsePtr ause;
          
            ause = WXS_ATTR_USE_CAST item;
            *buf = xmlStrdup(BAD_CAST "attribute use ");
            if (WXS_ATTRUSE_DECL(ause) != NULL) {
                *buf = xmlStrcat(*buf, BAD_CAST "'");
                *buf = xmlStrcat(*buf,
                  xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
                FREE_AND_NULL(str)
                  *buf = xmlStrcat(*buf, BAD_CAST "'");
            } else {
                *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
            }
          }
          break;
      case XML_SCHEMA_TYPE_ATTRIBUTE: {
            xmlSchemaAttributePtr attr;
          
            attr = (xmlSchemaAttributePtr) item;
            *buf = xmlStrdup(BAD_CAST "attribute decl.");
            *buf = xmlStrcat(*buf, BAD_CAST " '");
            *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
                attr->targetNamespace, attr->name));
            FREE_AND_NULL(str)
                *buf = xmlStrcat(*buf, BAD_CAST "'");
          }
          break;
      case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
          xmlSchemaGetComponentDesignation(buf, item);
          break;
      case XML_SCHEMA_TYPE_ELEMENT: {
            xmlSchemaElementPtr elem;

            elem = (xmlSchemaElementPtr) item;      
            *buf = xmlStrdup(BAD_CAST "element decl.");
            *buf = xmlStrcat(*buf, BAD_CAST " '");
            *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
                elem->targetNamespace, elem->name));
            *buf = xmlStrcat(*buf, BAD_CAST "'");
          }
          break;
      case XML_SCHEMA_TYPE_IDC_UNIQUE:
      case XML_SCHEMA_TYPE_IDC_KEY:
      case XML_SCHEMA_TYPE_IDC_KEYREF:          
          if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
            *buf = xmlStrdup(BAD_CAST "unique '");
          else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
            *buf = xmlStrdup(BAD_CAST "key '");
          else
            *buf = xmlStrdup(BAD_CAST "keyRef '");
          *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
          *buf = xmlStrcat(*buf, BAD_CAST "'");
          break;
      case XML_SCHEMA_TYPE_ANY:
      case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
          *buf = xmlStrdup(xmlSchemaWildcardPCToString(
                ((xmlSchemaWildcardPtr) item)->processContents));
          *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
          break;
      case XML_SCHEMA_FACET_MININCLUSIVE:
      case XML_SCHEMA_FACET_MINEXCLUSIVE:
      case XML_SCHEMA_FACET_MAXINCLUSIVE:
      case XML_SCHEMA_FACET_MAXEXCLUSIVE:
      case XML_SCHEMA_FACET_TOTALDIGITS:
      case XML_SCHEMA_FACET_FRACTIONDIGITS:
      case XML_SCHEMA_FACET_PATTERN:
      case XML_SCHEMA_FACET_ENUMERATION:
      case XML_SCHEMA_FACET_WHITESPACE:
      case XML_SCHEMA_FACET_LENGTH:
      case XML_SCHEMA_FACET_MAXLENGTH:
      case XML_SCHEMA_FACET_MINLENGTH:
          *buf = xmlStrdup(BAD_CAST "facet '");
          *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
          *buf = xmlStrcat(*buf, BAD_CAST "'");
          break;
      case XML_SCHEMA_TYPE_GROUP: {
            *buf = xmlStrdup(BAD_CAST "model group def.");
            *buf = xmlStrcat(*buf, BAD_CAST " '");
            *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
            *buf = xmlStrcat(*buf, BAD_CAST "'");
            FREE_AND_NULL(str)
          }
          break;
      case XML_SCHEMA_TYPE_SEQUENCE:
      case XML_SCHEMA_TYPE_CHOICE:
      case XML_SCHEMA_TYPE_ALL:
      case XML_SCHEMA_TYPE_PARTICLE:
          *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
          break;
      case XML_SCHEMA_TYPE_NOTATION: {
            *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
            *buf = xmlStrcat(*buf, BAD_CAST " '");
            *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
            *buf = xmlStrcat(*buf, BAD_CAST "'");
            FREE_AND_NULL(str);
          }
      default:
          named = 0;
      }
    } else 
      named = 0;

    if ((named == 0) && (itemNode != NULL)) {
      xmlNodePtr elem;

      if (itemNode->type == XML_ATTRIBUTE_NODE)
          elem = itemNode->parent;
      else 
          elem = itemNode;
      *buf = xmlStrdup(BAD_CAST "Element '");
      if (elem->ns != NULL) {
          *buf = xmlStrcat(*buf,
            xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
          FREE_AND_NULL(str)
      } else
          *buf = xmlStrcat(*buf, elem->name);
      *buf = xmlStrcat(*buf, BAD_CAST "'");
      
    }
    if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
      *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
      if (itemNode->ns != NULL) {
          *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
            itemNode->ns->href, itemNode->name));
          FREE_AND_NULL(str)
      } else
          *buf = xmlStrcat(*buf, itemNode->name);
      *buf = xmlStrcat(*buf, BAD_CAST "'");
    }
    FREE_AND_NULL(str)
    
    return (*buf);
}

/**
 * xmlSchemaFormatFacetEnumSet:
 * @buf: the string buffer
 * @type: the type holding the enumeration facets
 *
 * Builds a string consisting of all enumeration elements.
 *
 * Returns a string of all enumeration elements.
 */
static const xmlChar *
xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
                      xmlChar **buf, xmlSchemaTypePtr type)
{
    xmlSchemaFacetPtr facet;
    xmlSchemaWhitespaceValueType ws;
    xmlChar *value = NULL;
    int res, found = 0;

    if (*buf != NULL)
      xmlFree(*buf);    
    *buf = NULL;

    do {
      /*
      * Use the whitespace type of the base type.
      */    
      ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
      for (facet = type->facets; facet != NULL; facet = facet->next) {
          if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
            continue;
          found = 1;
          res = xmlSchemaGetCanonValueWhtspExt(facet->val,
            ws, &value);
          if (res == -1) {
            xmlSchemaInternalErr(actxt,
                "xmlSchemaFormatFacetEnumSet",
                "compute the canonical lexical representation");
            if (*buf != NULL)
                xmlFree(*buf);
            *buf = NULL;
            return (NULL);
          }
          if (*buf == NULL)
            *buf = xmlStrdup(BAD_CAST "'");
          else
            *buf = xmlStrcat(*buf, BAD_CAST ", '");
          *buf = xmlStrcat(*buf, BAD_CAST value);
          *buf = xmlStrcat(*buf, BAD_CAST "'");
          if (value != NULL) {
            xmlFree((xmlChar *)value);
            value = NULL;
          }
      }
      /*
      * The enumeration facet of a type restricts the enumeration
      * facet of the ancestor type; i.e., such restricted enumerations
      * do not belong to the set of the given type. Thus we break
      * on the first found enumeration.
      */
      if (found)
          break;
      type = type->baseType;
    } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));

    return ((const xmlChar *) *buf);
}

/************************************************************************
 *                                                    *
 *                Error functions                             *
 *                                                    *
 ************************************************************************/

#if 0
static void
xmlSchemaErrMemory(const char *msg)
{
    __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
                     msg);
}
#endif

static void
xmlSchemaPSimpleErr(const char *msg)
{
    __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
                     msg);
}

/**
 * xmlSchemaPErrMemory:
 * @node: a context node
 * @extra:  extra informations
 *
 * Handle an out of memory condition
 */
static void
xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
                    const char *extra, xmlNodePtr node)
{
    if (ctxt != NULL)
        ctxt->nberrors++;
    __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
                     extra);
}

/**
 * xmlSchemaPErr:
 * @ctxt: the parsing context
 * @node: the context node
 * @error: the error code
 * @msg: the error message
 * @str1: extra data
 * @str2: extra data
 * 
 * Handle a parser error
 */
static void
xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
              const char *msg, const xmlChar * str1, const xmlChar * str2)
{
    xmlGenericErrorFunc channel = NULL;
    xmlStructuredErrorFunc schannel = NULL;
    void *data = NULL;

    if (ctxt != NULL) {
        ctxt->nberrors++;
      ctxt->err = error;
        channel = ctxt->error;
        data = ctxt->errCtxt;
      schannel = ctxt->serror;
    }
    __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
                    error, XML_ERR_ERROR, NULL, 0,
                    (const char *) str1, (const char *) str2, NULL, 0, 0,
                    msg, str1, str2);
}

/**
 * xmlSchemaPErr2:
 * @ctxt: the parsing context
 * @node: the context node
 * @node: the current child
 * @error: the error code
 * @msg: the error message
 * @str1: extra data
 * @str2: extra data
 * 
 * Handle a parser error
 */
static void
xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
               xmlNodePtr child, int error,
               const char *msg, const xmlChar * str1, const xmlChar * str2)
{
    if (child != NULL)
        xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
    else
        xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
}


/**
 * xmlSchemaPErrExt:
 * @ctxt: the parsing context
 * @node: the context node
 * @error: the error code 
 * @strData1: extra data
 * @strData2: extra data
 * @strData3: extra data
 * @msg: the message
 * @str1:  extra parameter for the message display
 * @str2:  extra parameter for the message display
 * @str3:  extra parameter for the message display
 * @str4:  extra parameter for the message display
 * @str5:  extra parameter for the message display
 * 
 * Handle a parser error
 */
static void
xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
            const xmlChar * strData1, const xmlChar * strData2, 
            const xmlChar * strData3, const char *msg, const xmlChar * str1, 
            const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
            const xmlChar * str5)
{

    xmlGenericErrorFunc channel = NULL;
    xmlStructuredErrorFunc schannel = NULL;
    void *data = NULL;

    if (ctxt != NULL) {
        ctxt->nberrors++;
      ctxt->err = error;
        channel = ctxt->error;
        data = ctxt->errCtxt;
      schannel = ctxt->serror;
    }
    __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
                    error, XML_ERR_ERROR, NULL, 0,
                    (const char *) strData1, (const char *) strData2, 
                (const char *) strData3, 0, 0, msg, str1, str2, 
                str3, str4, str5);
}

/************************************************************************
 *                                                    *
 *                Allround error functions                  *
 *                                                    *
 ************************************************************************/

/**
 * xmlSchemaVTypeErrMemory:
 * @node: a context node
 * @extra:  extra informations
 *
 * Handle an out of memory condition
 */
static void
xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
                    const char *extra, xmlNodePtr node)
{
    if (ctxt != NULL) {
        ctxt->nberrors++;
        ctxt->err = XML_SCHEMAV_INTERNAL;
    }
    __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
                     extra);
}

static void
xmlSchemaPSimpleInternalErr(xmlNodePtr node,
                      const char *msg, const xmlChar *str)
{
     __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
       msg, (const char *) str);
}

#define WXS_ERROR_TYPE_ERROR 1
#define WXS_ERROR_TYPE_WARNING 2
/**
 * xmlSchemaErr3:
 * @ctxt: the validation context
 * @node: the context node
 * @error: the error code
 * @msg: the error message
 * @str1: extra data
 * @str2: extra data
 * @str3: extra data
 * 
 * Handle a validation error
 */
static void
xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
              xmlErrorLevel errorLevel,
              int error, xmlNodePtr node, int line, const char *msg,
              const xmlChar *str1, const xmlChar *str2,
              const xmlChar *str3, const xmlChar *str4)
{
    xmlStructuredErrorFunc schannel = NULL;
    xmlGenericErrorFunc channel = NULL;
    void *data = NULL;    
    
    if (ctxt != NULL) {
      if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
          xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
          const char *file = NULL;
          if (errorLevel != XML_ERR_WARNING) {
            vctxt->nberrors++;
            vctxt->err = error;
            channel = vctxt->error;       
          } else {
            channel = vctxt->warning;
          }
          schannel = vctxt->serror;
          data = vctxt->errCtxt;

          /*
          * Error node. If we specify a line number, then
          * do not channel any node to the error function.
          */
          if (line == 0) {
            if ((node == NULL) &&
                (vctxt->depth >= 0) &&
                (vctxt->inode != NULL)) {
                node = vctxt->inode->node;
            }
            /*
            * Get filename and line if no node-tree.
            */
            if ((node == NULL) &&
                (vctxt->parserCtxt != NULL) &&
                (vctxt->parserCtxt->input != NULL)) {
                file = vctxt->parserCtxt->input->filename;
                line = vctxt->parserCtxt->input->line;
            }
          } else {
            /*
            * Override the given node's (if any) position
            * and channel only the given line number.
            */
            node = NULL;
            /*
            * Get filename.
            */
            if (vctxt->doc != NULL)
                file = (const char *) vctxt->doc->URL;
            else if ((vctxt->parserCtxt != NULL) &&
                (vctxt->parserCtxt->input != NULL))
                file = vctxt->parserCtxt->input->filename;
          }        
          __xmlRaiseError(schannel, channel, data, ctxt,
            node, XML_FROM_SCHEMASV,
            error, errorLevel, file, line,
            (const char *) str1, (const char *) str2,
            (const char *) str3, 0, 0, msg, str1, str2, str3, str4);

      } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
          xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
          if (errorLevel != XML_ERR_WARNING) {
            pctxt->nberrors++;
            pctxt->err = error;
            channel = pctxt->error;       
          } else {
            channel = pctxt->warning;
          }
          schannel = pctxt->serror;
          data = pctxt->errCtxt;
          __xmlRaiseError(schannel, channel, data, ctxt,
            node, XML_FROM_SCHEMASP, error,
            errorLevel, NULL, 0,
            (const char *) str1, (const char *) str2,
            (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
      } else {
          TODO
      }
    }
}

/**
 * xmlSchemaErr3:
 * @ctxt: the validation context
 * @node: the context node
 * @error: the error code
 * @msg: the error message
 * @str1: extra data
 * @str2: extra data
 * @str3: extra data
 * 
 * Handle a validation error
 */
static void
xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,  
            int error, xmlNodePtr node, const char *msg,
            const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
{
    xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
      msg, str1, str2, str3, NULL);
}

static void
xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,  
            int error, xmlNodePtr node, const char *msg,
            const xmlChar *str1, const xmlChar *str2,
            const xmlChar *str3, const xmlChar *str4)
{
    xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
      msg, str1, str2, str3, str4);
}

static void
xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
           int error, xmlNodePtr node, const char *msg,
           const xmlChar *str1, const xmlChar *str2)
{
    xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
}

static xmlChar *
xmlSchemaFormatNodeForError(xmlChar ** msg,
                      xmlSchemaAbstractCtxtPtr actxt,
                      xmlNodePtr node)
{
    xmlChar *str = NULL;

    *msg = NULL;
    if ((node != NULL) &&
      (node->type != XML_ELEMENT_NODE) &&
      (node->type != XML_ATTRIBUTE_NODE))
    {
      /* 
      * Don't try to format other nodes than element and
      * attribute nodes.
      * Play save and return an empty string.
      */
      *msg = xmlStrdup(BAD_CAST "");
      return(*msg);
    }
    if (node != NULL) {
      /*
      * Work on tree nodes.
      */
      if (node->type == XML_ATTRIBUTE_NODE) {
          xmlNodePtr elem = node->parent;
          
          *msg = xmlStrdup(BAD_CAST "Element '");
          if (elem->ns != NULL)
            *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
                elem->ns->href, elem->name));
          else
            *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
                NULL, elem->name));
          FREE_AND_NULL(str);
          *msg = xmlStrcat(*msg, BAD_CAST "', ");
          *msg = xmlStrcat(*msg, BAD_CAST "attribute '");       
      } else {
          *msg = xmlStrdup(BAD_CAST "Element '");
      }
      if (node->ns != NULL)
          *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
          node->ns->href, node->name));
      else
          *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
          NULL, node->name));
      FREE_AND_NULL(str);
      *msg = xmlStrcat(*msg, BAD_CAST "': ");
    } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
      xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
      /*
      * Work on node infos.
      */    
      if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
          xmlSchemaNodeInfoPtr ielem =
            vctxt->elemInfos[vctxt->depth];

          *msg = xmlStrdup(BAD_CAST "Element '");
          *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
            ielem->nsName, ielem->localName));
          FREE_AND_NULL(str);
          *msg = xmlStrcat(*msg, BAD_CAST "', ");
          *msg = xmlStrcat(*msg, BAD_CAST "attribute '");       
      } else {
          *msg = xmlStrdup(BAD_CAST "Element '");
      }
      *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
          vctxt->inode->nsName, vctxt->inode->localName));
      FREE_AND_NULL(str);
      *msg = xmlStrcat(*msg, BAD_CAST "': ");
    } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
      /* 
      * Hmm, no node while parsing?
      * Return an empty string, in case NULL will break something.
      */
      *msg = xmlStrdup(BAD_CAST "");
    } else {
      TODO
      return (NULL);
    }
    /*
    * VAL TODO: The output of the given schema component is currently
    * disabled.
    */
#if 0
    if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
      *msg = xmlStrcat(*msg, BAD_CAST " [");
      *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
          NULL, type, NULL, 0));
      FREE_AND_NULL(str)
      *msg = xmlStrcat(*msg, BAD_CAST "]");
    }
#endif
    return (*msg);
}

static void
xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
                 const char *funcName,
                 const char *message,
                 const xmlChar *str1,
                 const xmlChar *str2)
{
    xmlChar *msg = NULL;

    if (actxt == NULL)
        return;
    msg = xmlStrdup(BAD_CAST "Internal error: ");
    msg = xmlStrcat(msg, BAD_CAST funcName);
    msg = xmlStrcat(msg, BAD_CAST ", ");    
    msg = xmlStrcat(msg, BAD_CAST message);
    msg = xmlStrcat(msg, BAD_CAST ".\n");

    if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
      xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
          (const char *) msg, str1, str2);

    else if (actxt->type == XML_SCHEMA_CTXT_PARSER)   
      xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
          (const char *) msg, str1, str2);

    FREE_AND_NULL(msg)
}

static void
xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
                 const char *funcName,
                 const char *message)
{
    xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
}

#if 0
static void
xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
                 const char *funcName,
                 const char *message,
                 const xmlChar *str1,
                 const xmlChar *str2)
{
    xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
      str1, str2);
}
#endif

static void
xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
               xmlParserErrors error,
               xmlNodePtr node,
               xmlSchemaBasicItemPtr item,
               const char *message,
               const xmlChar *str1, const xmlChar *str2,
               const xmlChar *str3, const xmlChar *str4)
{
    xmlChar *msg = NULL;

    if ((node == NULL) && (item != NULL) &&
      (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
      node = WXS_ITEM_NODE(item);
      xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
      msg = xmlStrcat(msg, BAD_CAST ": ");
    } else
      xmlSchemaFormatNodeForError(&msg, actxt, node);
    msg = xmlStrcat(msg, (const xmlChar *) message);
    msg = xmlStrcat(msg, BAD_CAST ".\n");   
    xmlSchemaErr4(actxt, error, node,
      (const char *) msg, str1, str2, str3, str4);
    FREE_AND_NULL(msg)
}

static void
xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
               xmlParserErrors error,
               xmlNodePtr node,
               xmlSchemaBasicItemPtr item,
               const char *message,
               const xmlChar *str1,
               const xmlChar *str2)
{
    xmlSchemaCustomErr4(actxt, error, node, item,
      message, str1, str2, NULL, NULL);    
}



static void
xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
               xmlParserErrors error,
               xmlNodePtr node,
               xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
               const char *message,
               const xmlChar *str1,
               const xmlChar *str2,
               const xmlChar *str3)
{
    xmlChar *msg = NULL;

    xmlSchemaFormatNodeForError(&msg, actxt, node);
    msg = xmlStrcat(msg, (const xmlChar *) message);
    msg = xmlStrcat(msg, BAD_CAST ".\n"); 
    
    /* URGENT TODO: Set the error code to something sane. */
    xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
      (const char *) msg, str1, str2, str3, NULL);

    FREE_AND_NULL(msg)
}



static void
xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
               xmlParserErrors error,
               xmlSchemaPSVIIDCNodePtr idcNode,
               xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
               const char *message,
               const xmlChar *str1,
               const xmlChar *str2)
{
    xmlChar *msg = NULL, *qname = NULL;
    
    msg = xmlStrdup(BAD_CAST "Element '%s': ");        
    msg = xmlStrcat(msg, (const xmlChar *) message);
    msg = xmlStrcat(msg, BAD_CAST ".\n");
    xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
      error, NULL, idcNode->nodeLine, (const char *) msg,
      xmlSchemaFormatQName(&qname,
          vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
          vctxt->nodeQNames->items[idcNode->nodeQNameID]), 
      str1, str2, NULL);
    FREE_AND_NULL(qname);
    FREE_AND_NULL(msg);
}

static int
xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
                     xmlNodePtr node)
{
    if (node != NULL)
      return (node->type);
    if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
      (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
      return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
    return (-1);
}

static int
xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
{
    switch (item->type) {
      case XML_SCHEMA_TYPE_COMPLEX:
      case XML_SCHEMA_TYPE_SIMPLE:
          if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
            return(1);
          break;
      case XML_SCHEMA_TYPE_GROUP:
          return (1);
      case XML_SCHEMA_TYPE_ELEMENT:
          if ( ((xmlSchemaElementPtr) item)->flags &
            XML_SCHEMAS_ELEM_GLOBAL)
            return(1);
          break;
      case XML_SCHEMA_TYPE_ATTRIBUTE:
          if ( ((xmlSchemaAttributePtr) item)->flags &
            XML_SCHEMAS_ATTR_GLOBAL)
            return(1);
          break;
      /* Note that attribute groups are always global. */
      default:
          return(1);
    }
    return (0);
}

static void
xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
                   xmlParserErrors error,
                   xmlNodePtr node,
                   const xmlChar *value,
                   xmlSchemaTypePtr type,
                   int displayValue)
{
    xmlChar *msg = NULL;

    xmlSchemaFormatNodeForError(&msg, actxt, node);

    if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
          XML_ATTRIBUTE_NODE))
      msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
    else
      msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
          "value of ");

    if (! xmlSchemaIsGlobalItem(type))
      msg = xmlStrcat(msg, BAD_CAST "the local ");
    else
      msg = xmlStrcat(msg, BAD_CAST "the ");

    if (WXS_IS_ATOMIC(type))
      msg = xmlStrcat(msg, BAD_CAST "atomic type");
    else if (WXS_IS_LIST(type))
      msg = xmlStrcat(msg, BAD_CAST "list type");
    else if (WXS_IS_UNION(type))
      msg = xmlStrcat(msg, BAD_CAST "union type");

    if (xmlSchemaIsGlobalItem(type)) {
      xmlChar *str = NULL;
      msg = xmlStrcat(msg, BAD_CAST " '");
      if (type->builtInType != 0) {
          msg = xmlStrcat(msg, BAD_CAST "xs:");
          msg = xmlStrcat(msg, type->name);
      } else 
          msg = xmlStrcat(msg,
            xmlSchemaFormatQName(&str,
                type->targetNamespace, type->name));
      msg = xmlStrcat(msg, BAD_CAST "'");
      FREE_AND_NULL(str);
    }
    msg = xmlStrcat(msg, BAD_CAST ".\n");
    if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
          XML_ATTRIBUTE_NODE))
      xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
    else
      xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
    FREE_AND_NULL(msg)
}

static const xmlChar *
xmlSchemaFormatErrorNodeQName(xmlChar ** str,
                        xmlSchemaNodeInfoPtr ni,
                        xmlNodePtr node)
{
    if (node != NULL) {
      if (node->ns != NULL)
          return (xmlSchemaFormatQName(str, node->ns->href, node->name));
      else
          return (xmlSchemaFormatQName(str, NULL, node->name));
    } else if (ni != NULL)
      return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
    return (NULL);
}

static void
xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
                  xmlParserErrors error,
                  xmlSchemaAttrInfoPtr ni,
                  xmlNodePtr node)
{
    xmlChar *msg = NULL, *str = NULL;
    
    xmlSchemaFormatNodeForError(&msg, actxt, node);
    msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
    xmlSchemaErr(actxt, error, node, (const char *) msg,
      xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
      NULL);        
    FREE_AND_NULL(str)
    FREE_AND_NULL(msg)
}

static void
xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
                    xmlParserErrors error,
                    xmlNodePtr node,
                  xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
                  const char *message,
                  int nbval,
                  int nbneg,
                  xmlChar **values)
{
    xmlChar *str = NULL, *msg = NULL;
    xmlChar *localName, *nsName;
    const xmlChar *cur, *end;
    int i;
    
    xmlSchemaFormatNodeForError(&msg, actxt, node);
    msg = xmlStrcat(msg, (const xmlChar *) message);
    msg = xmlStrcat(msg, BAD_CAST ".");
    /*
    * Note that is does not make sense to report that we have a
    * wildcard here, since the wildcard might be unfolded into
    * multiple transitions.
    */
    if (nbval + nbneg > 0) {
      if (nbval + nbneg > 1) {
          str = xmlStrdup(BAD_CAST " Expected is one of ( ");
      } else
          str = xmlStrdup(BAD_CAST " Expected is ( ");
      nsName = NULL;
          
      for (i = 0; i < nbval + nbneg; i++) {
          cur = values[i];
          if (cur == NULL)
              continue;
          if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
              (cur[3] == ' ')) {
            cur += 4;
            str = xmlStrcat(str, BAD_CAST "##other");
          }
          /*
          * Get the local name.
          */
          localName = NULL;
          
          end = cur;
          if (*end == '*') {
            localName = xmlStrdup(BAD_CAST "*");
            end++;
          } else {
            while ((*end != 0) && (*end != '|'))
                end++;
            localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
          }       
          if (*end != 0) {              
            end++;
            /*
            * Skip "*|*" if they come with negated expressions, since
            * they represent the same negated wildcard.
            */
            if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
                /*
                * Get the namespace name.
                */
                cur = end;
                if (*end == '*') {
                  nsName = xmlStrdup(BAD_CAST "{*}");
                } else {
                  while (*end != 0)
                      end++;
                  
                  if (i >= nbval)
                      nsName = xmlStrdup(BAD_CAST "{##other:");
                  else
                      nsName = xmlStrdup(BAD_CAST "{");
                  
                  nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
                  nsName = xmlStrcat(nsName, BAD_CAST "}");
                }
                str = xmlStrcat(str, BAD_CAST nsName);
                FREE_AND_NULL(nsName)
            } else {
                FREE_AND_NULL(localName);
                continue;
            }
          }         
          str = xmlStrcat(str, BAD_CAST localName);
          FREE_AND_NULL(localName);
            
          if (i < nbval + nbneg -1)
            str = xmlStrcat(str, BAD_CAST ", ");
      }     
      str = xmlStrcat(str, BAD_CAST " ).\n");
      msg = xmlStrcat(msg, BAD_CAST str);
      FREE_AND_NULL(str)
    } else
      msg = xmlStrcat(msg, BAD_CAST "\n");
    xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
    xmlFree(msg);
}

static void
xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
              xmlParserErrors error,
              xmlNodePtr node,
              const xmlChar *value,
              unsigned long length,
              xmlSchemaTypePtr type,
              xmlSchemaFacetPtr facet,
              const char *message,
              const xmlChar *str1,
              const xmlChar *str2)
{
    xmlChar *str = NULL, *msg = NULL;
    xmlSchemaTypeType facetType;
    int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);

    xmlSchemaFormatNodeForError(&msg, actxt, node);
    if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
      facetType = XML_SCHEMA_FACET_ENUMERATION;
      /*
      * If enumerations are validated, one must not expect the
      * facet to be given.
      */    
    } else  
      facetType = facet->type;
    msg = xmlStrcat(msg, BAD_CAST "[");
    msg = xmlStrcat(msg, BAD_CAST "facet '");
    msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
    msg = xmlStrcat(msg, BAD_CAST "'] ");
    if (message == NULL) {
      /*
      * Use a default message.
      */
      if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
          (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
          (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {

          char len[25], actLen[25];

          /* FIXME, TODO: What is the max expected string length of the
          * this value?
          */
          if (nodeType == XML_ATTRIBUTE_NODE)
            msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
          else
            msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");

          snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
          snprintf(actLen, 24, "%lu", length);

          if (facetType == XML_SCHEMA_FACET_LENGTH)
            msg = xmlStrcat(msg, 
            BAD_CAST "this differs from the allowed length of '%s'.\n");     
          else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
            msg = xmlStrcat(msg, 
            BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
          else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
            msg = xmlStrcat(msg, 
            BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
          
          if (nodeType == XML_ATTRIBUTE_NODE)
            xmlSchemaErr3(actxt, error, node, (const char *) msg,
                value, (const xmlChar *) actLen, (const xmlChar *) len);
          else 
            xmlSchemaErr(actxt, error, node, (const char *) msg,
                (const xmlChar *) actLen, (const xmlChar *) len);
      
      } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
            "of the set {%s}.\n");
          xmlSchemaErr(actxt, error, node, (const char *) msg, value, 
            xmlSchemaFormatFacetEnumSet(actxt, &str, type));
      } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
            "by the pattern '%s'.\n");
          xmlSchemaErr(actxt, error, node, (const char *) msg, value, 
            facet->value);
      } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
            "minimum value allowed ('%s').\n");
          xmlSchemaErr(actxt, error, node, (const char *) msg, value,
            facet->value);
      } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
            "maximum value allowed ('%s').\n");
          xmlSchemaErr(actxt, error, node, (const char *) msg, value,
            facet->value);
      } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be greater than "
            "'%s'.\n");
          xmlSchemaErr(actxt, error, node, (const char *) msg, value,
            facet->value);
      } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
            "'%s'.\n");
          xmlSchemaErr(actxt, error, node, (const char *) msg, value,
            facet->value);
      } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
            "digits than are allowed ('%s').\n");
          xmlSchemaErr(actxt, error, node, (const char*) msg, value,
            facet->value);
      } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
            "digits than are allowed ('%s').\n");
          xmlSchemaErr(actxt, error, node, (const char*) msg, value,
            facet->value);
      } else if (nodeType == XML_ATTRIBUTE_NODE) {          
          msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
          xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);  
      } else {        
          msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
          xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
      }
    } else {
      msg = xmlStrcat(msg, (const xmlChar *) message);
      msg = xmlStrcat(msg, BAD_CAST ".\n");
      xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
    }        
    FREE_AND_NULL(str)
    xmlFree(msg);
}

#define VERROR(err, type, msg) \
    xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);

#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);

#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);

#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);


/**
 * xmlSchemaPMissingAttrErr:
 * @ctxt: the schema validation context
 * @ownerDes: the designation of  the owner
 * @ownerName: the name of the owner
 * @ownerItem: the owner as a schema object
 * @ownerElem: the owner as an element node
 * @node: the parent element node of the missing attribute node
 * @type: the corresponding type of the attribute node
 *
 * Reports an illegal attribute.
 */
static void
xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
                   xmlParserErrors error,
                   xmlSchemaBasicItemPtr ownerItem,
                   xmlNodePtr ownerElem,
                   const char *name,
                   const char *message)
{
    xmlChar *des = NULL;

    xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);

    if (message != NULL)
      xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
    else
      xmlSchemaPErr(ctxt, ownerElem, error,
          "%s: The attribute '%s' is required but missing.\n",
          BAD_CAST des, BAD_CAST name);
    FREE_AND_NULL(des);
}


/**
 * xmlSchemaPResCompAttrErr:
 * @ctxt: the schema validation context
 * @error: the error code
 * @ownerDes: the designation of  the owner
 * @ownerItem: the owner as a schema object
 * @ownerElem: the owner as an element node
 * @name: the name of the attribute holding the QName
 * @refName: the referenced local name
 * @refURI: the referenced namespace URI
 * @message: optional message
 *
 * Used to report QName attribute values that failed to resolve
 * to schema components.
 */
static void
xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
                   xmlParserErrors error,
                   xmlSchemaBasicItemPtr ownerItem,
                   xmlNodePtr ownerElem,
                   const char *name,
                   const xmlChar *refName,
                   const xmlChar *refURI,
                   xmlSchemaTypeType refType,
                   const char *refTypeStr)
{
    xmlChar *des = NULL, *strA = NULL;

    xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
    if (refTypeStr == NULL)
      refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
      xmlSchemaPErrExt(ctxt, ownerElem, error,
          NULL, NULL, NULL,
          "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
          "%s.\n", BAD_CAST des, BAD_CAST name,
          xmlSchemaFormatQName(&strA, refURI, refName),
          BAD_CAST refTypeStr, NULL);
    FREE_AND_NULL(des)
    FREE_AND_NULL(strA)
}

/**
 * xmlSchemaPCustomAttrErr:
 * @ctxt: the schema parser context
 * @error: the error code
 * @ownerDes: the designation of the owner
 * @ownerItem: the owner as a schema object
 * @attr: the illegal attribute node
 *
 * Reports an illegal attribute during the parse.
 */
static void
xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
                  xmlParserErrors error,
                  xmlChar **ownerDes,
                  xmlSchemaBasicItemPtr ownerItem,
                  xmlAttrPtr attr,
                  const char *msg)
{
    xmlChar *des = NULL;

    if (ownerDes == NULL)
      xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
    else if (*ownerDes == NULL) {
      xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
      des = *ownerDes;
    } else
      des = *ownerDes;
    if (attr == NULL) {
      xmlSchemaPErrExt(ctxt, NULL, error, NULL, NULL, NULL,
          "%s, attribute '%s': %s.\n",
          BAD_CAST des, (const xmlChar *) "Unknown",
          (const xmlChar *) msg, NULL, NULL);
    } else {
      xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
          "%s, attribute '%s': %s.\n",
          BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
    }
    if (ownerDes == NULL)
      FREE_AND_NULL(des);
}

/**
 * xmlSchemaPIllegalAttrErr:
 * @ctxt: the schema parser context
 * @error: the error code
 * @ownerDes: the designation of the attribute's owner
 * @ownerItem: the attribute's owner item
 * @attr: the illegal attribute node
 *
 * Reports an illegal attribute during the parse.
 */
static void
xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
                   xmlParserErrors error,
                   xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
                   xmlAttrPtr attr)
{
    xmlChar *strA = NULL, *strB = NULL;

    xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
    xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
      "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
      xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
      NULL, NULL);
    FREE_AND_NULL(strA);
    FREE_AND_NULL(strB);
}

/**
 * xmlSchemaPCustomErr:
 * @ctxt: the schema parser context
 * @error: the error code
 * @itemDes: the designation of the schema item
 * @item: the schema item
 * @itemElem: the node of the schema item
 * @message: the error message
 * @str1: an optional param for the error message
 * @str2: an optional param for the error message
 * @str3: an optional param for the error message
 *
 * Reports an error during parsing.
 */
static void
xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
                xmlParserErrors error,
                xmlSchemaBasicItemPtr item,
                xmlNodePtr itemElem,
                const char *message,
                const xmlChar *str1,
                const xmlChar *str2,
                const xmlChar *str3)
{
    xmlChar *des = NULL, *msg = NULL;

    xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
    msg = xmlStrdup(BAD_CAST "%s: ");
    msg = xmlStrcat(msg, (const xmlChar *) message);
    msg = xmlStrcat(msg, BAD_CAST ".\n");
    if ((itemElem == NULL) && (item != NULL))
      itemElem = WXS_ITEM_NODE(item);
    xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
      (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
    FREE_AND_NULL(des);
    FREE_AND_NULL(msg);
}

/**
 * xmlSchemaPCustomErr:
 * @ctxt: the schema parser context
 * @error: the error code
 * @itemDes: the designation of the schema item
 * @item: the schema item
 * @itemElem: the node of the schema item
 * @message: the error message
 * @str1: the optional param for the error message
 *
 * Reports an error during parsing.
 */
static void
xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
                xmlParserErrors error,
                xmlSchemaBasicItemPtr item,
                xmlNodePtr itemElem,
                const char *message,
                const xmlChar *str1)
{
    xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
      str1, NULL, NULL);
}

/**
 * xmlSchemaPAttrUseErr:
 * @ctxt: the schema parser context
 * @error: the error code
 * @itemDes: the designation of the schema type
 * @item: the schema type
 * @itemElem: the node of the schema type
 * @attr: the invalid schema attribute
 * @message: the error message
 * @str1: the optional param for the error message
 *
 * Reports an attribute use error during parsing.
 */
static void
xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
                xmlParserErrors error,
                xmlNodePtr node,
                xmlSchemaBasicItemPtr ownerItem,
                const xmlSchemaAttributeUsePtr attruse,
                const char *message,
                const xmlChar *str1, const xmlChar *str2,
                const xmlChar *str3,const xmlChar *str4)
{
    xmlChar *str = NULL, *msg = NULL;
    
    xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
    msg = xmlStrcat(msg, BAD_CAST ", ");    
    msg = xmlStrcat(msg,
      BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
      WXS_BASIC_CAST attruse, NULL));
    FREE_AND_NULL(str);
    msg = xmlStrcat(msg, BAD_CAST ": ");
    msg = xmlStrcat(msg, (const xmlChar *) message);
    msg = xmlStrcat(msg, BAD_CAST ".\n");
    xmlSchemaErr4(ACTXT_CAST ctxt, error, node, 
      (const char *) msg, str1, str2, str3, str4);
    xmlFree(msg);
}

/**
 * xmlSchemaPIllegalFacetAtomicErr:
 * @ctxt: the schema parser context
 * @error: the error code
 * @type: the schema type
 * @baseType: the base type of type
 * @facet: the illegal facet
 *
 * Reports an illegal facet for atomic simple types.
 */
static void
xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
                    xmlParserErrors error,
                    xmlSchemaTypePtr type,
                    xmlSchemaTypePtr baseType,
                    xmlSchemaFacetPtr facet)
{
    xmlChar *des = NULL, *strT = NULL;

    xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
    xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
      "%s: The facet '%s' is not allowed on types derived from the "
      "type %s.\n",
      BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
      xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
      NULL, NULL);
    FREE_AND_NULL(des);
    FREE_AND_NULL(strT);
}

/**
 * xmlSchemaPIllegalFacetListUnionErr:
 * @ctxt: the schema parser context
 * @error: the error code
 * @itemDes: the designation of the schema item involved
 * @item: the schema item involved
 * @facet: the illegal facet
 *
 * Reports an illegal facet for <list> and <union>.
 */
static void
xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
                    xmlParserErrors error,
                    xmlSchemaTypePtr type,
                    xmlSchemaFacetPtr facet)
{
    xmlChar *des = NULL;

    xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
      type->node);
    xmlSchemaPErr(ctxt, type->node, error,
      "%s: The facet '%s' is not allowed.\n",
      BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
    FREE_AND_NULL(des);
}

/**
 * xmlSchemaPMutualExclAttrErr:
 * @ctxt: the schema validation context
 * @error: the error code
 * @elemDes: the designation of the parent element node
 * @attr: the bad attribute node
 * @type: the corresponding type of the attribute node
 *
 * Reports an illegal attribute.
 */
static void
xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
                   xmlParserErrors error,
                   xmlSchemaBasicItemPtr ownerItem,
                   xmlAttrPtr attr,
                   const char *name1,
                   const char *name2)
{
    xmlChar *des = NULL;

    xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
    xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
      "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
      BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
    FREE_AND_NULL(des);
}

/**
 * xmlSchemaPSimpleTypeErr:
 * @ctxt:  the schema validation context
 * @error: the error code
 * @type: the type specifier
 * @ownerDes: the designation of the owner
 * @ownerItem: the schema object if existent 
 * @node: the validated node
 * @value: the validated value
 *
 * Reports a simple type validation error.
 * TODO: Should this report the value of an element as well?
 */
static void
xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt, 
                  xmlParserErrors error,
                  xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
                  xmlNodePtr node,
                  xmlSchemaTypePtr type,
                  const char *expected,
                  const xmlChar *value,
                  const char *message,
                  const xmlChar *str1,
                  const xmlChar *str2)
{
    xmlChar *msg = NULL;
    
    xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
    if (message == NULL) {
      /*
      * Use default messages.
      */    
      if (type != NULL) {
          if (node->type == XML_ATTRIBUTE_NODE)
            msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
          else
            msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
            "valid value of ");     
          if (! xmlSchemaIsGlobalItem(type))
            msg = xmlStrcat(msg, BAD_CAST "the local ");
          else
            msg = xmlStrcat(msg, BAD_CAST "the ");
          
          if (WXS_IS_ATOMIC(type))
            msg = xmlStrcat(msg, BAD_CAST "atomic type");
          else if (WXS_IS_LIST(type))
            msg = xmlStrcat(msg, BAD_CAST "list type");
          else if (WXS_IS_UNION(type))
            msg = xmlStrcat(msg, BAD_CAST "union type");
          
          if (xmlSchemaIsGlobalItem(type)) {
            xmlChar *str = NULL;
            msg = xmlStrcat(msg, BAD_CAST " '");
            if (type->builtInType != 0) {
                msg = xmlStrcat(msg, BAD_CAST "xs:");
                msg = xmlStrcat(msg, type->name);
            } else 
                msg = xmlStrcat(msg,
                  xmlSchemaFormatQName(&str,
                      type->targetNamespace, type->name));
            msg = xmlStrcat(msg, BAD_CAST "'.");
            FREE_AND_NULL(str);
          }
      } else {
          if (node->type == XML_ATTRIBUTE_NODE)
            msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
          else
            msg = xmlStrcat(msg, BAD_CAST "The character content is not "
            "valid.");
      }     
      if (expected) {
          msg = xmlStrcat(msg, BAD_CAST " Expected is '");
          msg = xmlStrcat(msg, BAD_CAST expected);
          msg = xmlStrcat(msg, BAD_CAST "'.\n");
      } else
          msg = xmlStrcat(msg, BAD_CAST "\n");
      if (node->type == XML_ATTRIBUTE_NODE)
          xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
      else
          xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
    } else {
      msg = xmlStrcat(msg, BAD_CAST message);
      msg = xmlStrcat(msg, BAD_CAST ".\n");
      xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
           (const char*) msg, str1, str2, NULL, NULL, NULL);
    }
    /* Cleanup. */    
    FREE_AND_NULL(msg)
}

/**
 * xmlSchemaPContentErr:
 * @ctxt: the schema parser context
 * @error: the error code
 * @onwerDes: the designation of the holder of the content
 * @ownerItem: the owner item of the holder of the content
 * @ownerElem: the node of the holder of the content
 * @child: the invalid child node
 * @message: the optional error message
 * @content: the optional string describing the correct content
 *
 * Reports an error concerning the content of a schema element.
 */
static void
xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
                 xmlParserErrors error,
                 xmlSchemaBasicItemPtr ownerItem,
                 xmlNodePtr ownerElem,
                 xmlNodePtr child,
                 const char *message,
                 const char *content)
{
    xmlChar *des = NULL;

    xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
    if (message != NULL)
      xmlSchemaPErr2(ctxt, ownerElem, child, error,
          "%s: %s.\n",
          BAD_CAST des, BAD_CAST message);
    else {
      if (content != NULL) {
          xmlSchemaPErr2(ctxt, ownerElem, child, error,
            "%s: The content is not valid. Expected is %s.\n",
            BAD_CAST des, BAD_CAST content);
      } else {
          xmlSchemaPErr2(ctxt, ownerElem, child, error,
            "%s: The content is not valid.\n",
            BAD_CAST des, NULL);
      }
    }
    FREE_AND_NULL(des)
}

/************************************************************************
 *                                                    *
 *                Streamable error functions                      *
 *                                                    *
 ************************************************************************/




/************************************************************************
 *                                                    *
 *                Validation helper functions               *
 *                                                    *
 ************************************************************************/


/************************************************************************
 *                                                    *
 *                Allocation functions                      *
 *                                                    *
 ************************************************************************/

/**
 * xmlSchemaNewSchemaForParserCtxt:
 * @ctxt:  a schema validation context
 *
 * Allocate a new Schema structure.
 *
 * Returns the newly allocated structure or NULL in case or error
 */
static xmlSchemaPtr
xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
{
    xmlSchemaPtr ret;

    ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchema));
    ret->dict = ctxt->dict;
    xmlDictReference(ret->dict);

    return (ret);
}

/**
 * xmlSchemaNewFacet:
 *
 * Allocate a new Facet structure.
 *
 * Returns the newly allocated structure or NULL in case or error
 */
xmlSchemaFacetPtr
xmlSchemaNewFacet(void)
{
    xmlSchemaFacetPtr ret;

    ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
    if (ret == NULL) {
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaFacet));

    return (ret);
}

/**
 * xmlSchemaNewAnnot:
 * @ctxt:  a schema validation context
 * @node:  a node
 *
 * Allocate a new annotation structure.
 *
 * Returns the newly allocated structure or NULL in case or error
 */
static xmlSchemaAnnotPtr
xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
{
    xmlSchemaAnnotPtr ret;

    ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaAnnot));
    ret->content = node;
    return (ret);
}

static xmlSchemaItemListPtr
xmlSchemaItemListCreate(void)
{
    xmlSchemaItemListPtr ret;

    ret = xmlMalloc(sizeof(xmlSchemaItemList));
    if (ret == NULL) {
      xmlSchemaPErrMemory(NULL,
          "allocating an item list structure", NULL);
      return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaItemList));
    return (ret);
}

static void
xmlSchemaItemListClear(xmlSchemaItemListPtr list)
{
    if (list->items != NULL) {
      xmlFree(list->items);
      list->items = NULL;
    }
    list->nbItems = 0;
    list->sizeItems = 0;
}

static int
xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
{
    if (list->items == NULL) {
      list->items = (void **) xmlMalloc(
          20 * sizeof(void *));
      if (list->items == NULL) {
          xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
          return(-1);
      }
      list->sizeItems = 20;
    } else if (list->sizeItems <= list->nbItems) {
      list->sizeItems *= 2;
      list->items = (void **) xmlRealloc(list->items,
          list->sizeItems * sizeof(void *));
      if (list->items == NULL) {
          xmlSchemaPErrMemory(NULL, "growing item list", NULL);
          list->sizeItems = 0;
          return(-1);
      }
    }
    list->items[list->nbItems++] = item;
    return(0);
}

static int
xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
                   int initialSize,
                   void *item)
{
    if (list->items == NULL) {
      if (initialSize <= 0)
          initialSize = 1;
      list->items = (void **) xmlMalloc(
          initialSize * sizeof(void *));
      if (list->items == NULL) {
          xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
          return(-1);
      }
      list->sizeItems = initialSize;
    } else if (list->sizeItems <= list->nbItems) {
      list->sizeItems *= 2;
      list->items = (void **) xmlRealloc(list->items,
          list->sizeItems * sizeof(void *));
      if (list->items == NULL) {
          xmlSchemaPErrMemory(NULL, "growing item list", NULL);
          list->sizeItems = 0;
          return(-1);
      }
    }
    list->items[list->nbItems++] = item;
    return(0);
}

static int
xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
{    
    if (list->items == NULL) {
      list->items = (void **) xmlMalloc(
          20 * sizeof(void *));
      if (list->items == NULL) {
          xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
          return(-1);
      }
      list->sizeItems = 20;
    } else if (list->sizeItems <= list->nbItems) {
      list->sizeItems *= 2;
      list->items = (void **) xmlRealloc(list->items,
          list->sizeItems * sizeof(void *));
      if (list->items == NULL) {
          xmlSchemaPErrMemory(NULL, "growing item list", NULL);
          list->sizeItems = 0;
          return(-1);
      }
    }
    /*
    * Just append if the index is greater/equal than the item count.
    */
    if (idx >= list->nbItems) {
      list->items[list->nbItems++] = item;
    } else {
      int i;
      for (i = list->nbItems; i > idx; i--)
          list->items[i] = list->items[i-1];
      list->items[idx] = item;
      list->nbItems++;
    }
    return(0);
}

#if 0 /* enable if ever needed */
static int
xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
                      int initialSize,
                      void *item,
                      int idx)
{    
    if (list->items == NULL) {
      if (initialSize <= 0)
          initialSize = 1;
      list->items = (void **) xmlMalloc(
          initialSize * sizeof(void *));
      if (list->items == NULL) {
          xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
          return(-1);
      }
      list->sizeItems = initialSize;
    } else if (list->sizeItems <= list->nbItems) {
      list->sizeItems *= 2;
      list->items = (void **) xmlRealloc(list->items,
          list->sizeItems * sizeof(void *));
      if (list->items == NULL) {
          xmlSchemaPErrMemory(NULL, "growing item list", NULL);
          list->sizeItems = 0;
          return(-1);
      }
    }
    /*
    * Just append if the index is greater/equal than the item count.
    */
    if (idx >= list->nbItems) {
      list->items[list->nbItems++] = item;
    } else {
      int i;
      for (i = list->nbItems; i > idx; i--)
          list->items[i] = list->items[i-1];
      list->items[idx] = item;
      list->nbItems++;
    }
    return(0);
}
#endif

static int
xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
{
    int i;
    if ((list->items == NULL) || (idx >= list->nbItems)) {
      xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
          "index error.\n");
      return(-1);
    }

    if (list->nbItems == 1) {
      /* TODO: Really free the list? */
      xmlFree(list->items);
      list->items = NULL;
      list->nbItems = 0;
      list->sizeItems = 0;
    } else if (list->nbItems -1 == idx) {
      list->nbItems--;
    } else {      
      for (i = idx; i < list->nbItems -1; i++)
          list->items[i] = list->items[i+1];
      list->nbItems--;
    }
    return(0);
}

/**
 * xmlSchemaItemListFree:
 * @annot:  a schema type structure
 *
 * Deallocate a annotation structure
 */
static void
xmlSchemaItemListFree(xmlSchemaItemListPtr list)
{
    if (list == NULL)
      return;
    if (list->items != NULL)
      xmlFree(list->items);
    xmlFree(list);
}

static void
xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
{
    if (bucket == NULL)
      return;
    if (bucket->globals != NULL) {
      xmlSchemaComponentListFree(bucket->globals);
      xmlSchemaItemListFree(bucket->globals);
    }
    if (bucket->locals != NULL) {
      xmlSchemaComponentListFree(bucket->locals);
      xmlSchemaItemListFree(bucket->locals);    
    }
    if (bucket->relations != NULL) {
      xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
      do {
          prev = cur;       
          cur = cur->next;
          xmlFree(prev);
      } while (cur != NULL);
    }
    if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
      xmlFreeDoc(bucket->doc);
    } 
    if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
      if (WXS_IMPBUCKET(bucket)->schema != NULL)
          xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
    }
    xmlFree(bucket);
}

static xmlSchemaBucketPtr
xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
                   int type, const xmlChar *targetNamespace)
{
    xmlSchemaBucketPtr ret;
    int size;
    xmlSchemaPtr mainSchema;

    if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
      PERROR_INT("xmlSchemaBucketCreate",
          "no main schema on constructor");
      return(NULL);
    }
    mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
    /* Create the schema bucket. */
    if (WXS_IS_BUCKET_INCREDEF(type))
      size = sizeof(xmlSchemaInclude);
    else
      size = sizeof(xmlSchemaImport);
    ret = (xmlSchemaBucketPtr) xmlMalloc(size);
    if (ret == NULL) {
      xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
      return(NULL);
    }
    memset(ret, 0, size);
    ret->targetNamespace = targetNamespace;
    ret->type = type;
    ret->globals = xmlSchemaItemListCreate();
    if (ret->globals == NULL) {
      xmlFree(ret);
      return(NULL);
    }
    ret->locals = xmlSchemaItemListCreate();
    if (ret->locals == NULL) {
      xmlFree(ret);
      return(NULL);
    }
    /* 
    * The following will assure that only the first bucket is marked as
    * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
    * For each following import buckets an xmlSchema will be created.
    * An xmlSchema will be created for every distinct targetNamespace.
    * We assign the targetNamespace to the schemata here.
    */
    if (! WXS_HAS_BUCKETS(pctxt)) {
      if (WXS_IS_BUCKET_INCREDEF(type)) {
          PERROR_INT("xmlSchemaBucketCreate",
            "first bucket but it's an include or redefine");
          xmlSchemaBucketFree(ret);
          return(NULL);
      }
      /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */ 
      ret->type = XML_SCHEMA_SCHEMA_MAIN;
      /* Point to the *main* schema. */
      WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
      WXS_IMPBUCKET(ret)->schema = mainSchema;
      /*
      * Ensure that the main schema gets a targetNamespace.
      */
      mainSchema->targetNamespace = targetNamespace;
    } else {
      if (type == XML_SCHEMA_SCHEMA_MAIN) {         
          PERROR_INT("xmlSchemaBucketCreate",
            "main bucket but it's not the first one");
          xmlSchemaBucketFree(ret);
          return(NULL);
      } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {      
          /*
          * Create a schema for imports and assign the
          * targetNamespace.
          */
          WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
          if (WXS_IMPBUCKET(ret)->schema == NULL) {
            xmlSchemaBucketFree(ret);
            return(NULL);
          }
          WXS_IMPBUCKET(ret)->schema->targetNamespace = targetNamespace;
      }
    }    
    if (WXS_IS_BUCKET_IMPMAIN(type)) {
      int res;
      /*
      * Imports go into the "schemasImports" slot of the main *schema*.
      * Note that we create an import entry for the main schema as well; i.e.,
      * even if there's only one schema, we'll get an import.
      */
      if (mainSchema->schemasImports == NULL) {
          mainSchema->schemasImports = xmlHashCreateDict(5,
            WXS_CONSTRUCTOR(pctxt)->dict);
          if (mainSchema->schemasImports == NULL) {
            xmlSchemaBucketFree(ret);
            return(NULL);
          }
      }
      if (targetNamespace == NULL)
          res = xmlHashAddEntry(mainSchema->schemasImports,
            XML_SCHEMAS_NO_NAMESPACE, ret);
      else
          res = xmlHashAddEntry(mainSchema->schemasImports,
            targetNamespace, ret);
      if (res != 0) {
          PERROR_INT("xmlSchemaBucketCreate",
            "failed to add the schema bucket to the hash");
          xmlSchemaBucketFree(ret);
          return(NULL);
      }
    } else {
      /* Set the @ownerImport of an include bucket. */
      if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
          WXS_INCBUCKET(ret)->ownerImport =
            WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
      else
          WXS_INCBUCKET(ret)->ownerImport =
            WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;

      /* Includes got into the "includes" slot of the *main* schema. */
      if (mainSchema->includes == NULL) {
          mainSchema->includes = xmlSchemaItemListCreate();
          if (mainSchema->includes == NULL) {
            xmlSchemaBucketFree(ret);
            return(NULL);
          }     
      }
      xmlSchemaItemListAdd(mainSchema->includes, ret);
    }
    /* 
    * Add to list of all buckets; this is used for lookup
    * during schema construction time only.
    */
    if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
      return(NULL);
    return(ret);
}

static int
xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
{
    if (*list == NULL) {
      *list = xmlSchemaItemListCreate();
      if (*list == NULL)
          return(-1);
    }
    xmlSchemaItemListAddSize(*list, initialSize, item);
    return(0);
}

/**
 * xmlSchemaFreeAnnot:
 * @annot:  a schema type structure
 *
 * Deallocate a annotation structure
 */
static void
xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
{
    if (annot == NULL)
        return;
    if (annot->next == NULL) {
      xmlFree(annot);
    } else {
      xmlSchemaAnnotPtr prev;

      do {
          prev = annot;
          annot = annot->next;
          xmlFree(prev);
      } while (annot != NULL);
    }
}

/**
 * xmlSchemaFreeNotation:
 * @schema:  a schema notation structure
 *
 * Deallocate a Schema Notation structure.
 */
static void
xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
{
    if (nota == NULL)
        return;
    xmlFree(nota);
}

/**
 * xmlSchemaFreeAttribute:
 * @attr:  an attribute declaration
 *
 * Deallocates an attribute declaration structure.
 */
static void
xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
{
    if (attr == NULL)
        return;
    if (attr->annot != NULL)
      xmlSchemaFreeAnnot(attr->annot);
    if (attr->defVal != NULL)
      xmlSchemaFreeValue(attr->defVal);
    xmlFree(attr);
}

/**
 * xmlSchemaFreeAttributeUse:
 * @use:  an attribute use
 *
 * Deallocates an attribute use structure.
 */
static void
xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
{
    if (use == NULL)
        return;
    if (use->annot != NULL)
      xmlSchemaFreeAnnot(use->annot);
    if (use->defVal != NULL)
      xmlSchemaFreeValue(use->defVal);
    xmlFree(use);
}

/**
 * xmlSchemaFreeAttributeUseProhib:
 * @prohib:  an attribute use prohibition
 *
 * Deallocates an attribute use structure.
 */
static void
xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
{
    if (prohib == NULL)
        return;
    xmlFree(prohib);
}

/**
 * xmlSchemaFreeWildcardNsSet:
 * set:  a schema wildcard namespace
 *
 * Deallocates a list of wildcard constraint structures.
 */
static void
xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
{
    xmlSchemaWildcardNsPtr next;

    while (set != NULL) {
      next = set->next;
      xmlFree(set);
      set = next;
    }
}

/**
 * xmlSchemaFreeWildcard:
 * @wildcard:  a wildcard structure
 *
 * Deallocates a wildcard structure.
 */
void
xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
{
    if (wildcard == NULL)
        return;
    if (wildcard->annot != NULL)
        xmlSchemaFreeAnnot(wildcard->annot);
    if (wildcard->nsSet != NULL)
      xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
    if (wildcard->negNsSet != NULL)
      xmlFree(wildcard->negNsSet);
    xmlFree(wildcard);
}

/**
 * xmlSchemaFreeAttributeGroup:
 * @schema:  a schema attribute group structure
 *
 * Deallocate a Schema Attribute Group structure.
 */
static void
xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
{
    if (attrGr == NULL)
        return;
    if (attrGr->annot != NULL)
        xmlSchemaFreeAnnot(attrGr->annot);
    if (attrGr->attrUses != NULL)
      xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
    xmlFree(attrGr);
}

/**
 * xmlSchemaFreeQNameRef:
 * @item: a QName reference structure
 *
 * Deallocatea a QName reference structure.
 */
static void
xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
{
    xmlFree(item);
}

/**
 * xmlSchemaFreeTypeLinkList:
 * @alink: a type link
 *
 * Deallocate a list of types.
 */
static void
xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
{
    xmlSchemaTypeLinkPtr next;

    while (link != NULL) {
      next = link->next;
      xmlFree(link);
      link = next;
    }
}

static void
xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
{
    xmlSchemaIDCStateObjPtr next;
    while (sto != NULL) {
      next = sto->next;
      if (sto->history != NULL)
          xmlFree(sto->history);
      if (sto->xpathCtxt != NULL)
          xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
      xmlFree(sto);
      sto = next;
    }
}

/**
 * xmlSchemaFreeIDC:
 * @idc: a identity-constraint definition
 *
 * Deallocates an identity-constraint definition.
 */
static void
xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
{
    xmlSchemaIDCSelectPtr cur, prev;

    if (idcDef == NULL)
      return;
    if (idcDef->annot != NULL)
        xmlSchemaFreeAnnot(idcDef->annot);
    /* Selector */
    if (idcDef->selector != NULL) {
      if (idcDef->selector->xpathComp != NULL)
          xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
      xmlFree(idcDef->selector);
    }
    /* Fields */
    if (idcDef->fields != NULL) {
      cur = idcDef->fields;
      do {
          prev = cur;
          cur = cur->next;
          if (prev->xpathComp != NULL)
            xmlFreePattern((xmlPatternPtr) prev->xpathComp);
          xmlFree(prev);
      } while (cur != NULL);
    }
    xmlFree(idcDef);
}

/**
 * xmlSchemaFreeElement:
 * @schema:  a schema element structure
 *
 * Deallocate a Schema Element structure.
 */
static void
xmlSchemaFreeElement(xmlSchemaElementPtr elem)
{
    if (elem == NULL)
        return;
    if (elem->annot != NULL)
        xmlSchemaFreeAnnot(elem->annot);
    if (elem->contModel != NULL)
        xmlRegFreeRegexp(elem->contModel);
    if (elem->defVal != NULL)
      xmlSchemaFreeValue(elem->defVal);
    xmlFree(elem);
}

/**
 * xmlSchemaFreeFacet:
 * @facet:  a schema facet structure
 *
 * Deallocate a Schema Facet structure.
 */
void
xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
{
    if (facet == NULL)
        return;
    if (facet->val != NULL)
        xmlSchemaFreeValue(facet->val);
    if (facet->regexp != NULL)
        xmlRegFreeRegexp(facet->regexp);
    if (facet->annot != NULL)
        xmlSchemaFreeAnnot(facet->annot);
    xmlFree(facet);
}

/**
 * xmlSchemaFreeType:
 * @type:  a schema type structure
 *
 * Deallocate a Schema Type structure.
 */
void
xmlSchemaFreeType(xmlSchemaTypePtr type)
{
    if (type == NULL)
        return;
    if (type->annot != NULL)
        xmlSchemaFreeAnnot(type->annot);
    if (type->facets != NULL) {
        xmlSchemaFacetPtr facet, next;

        facet = type->facets;
        while (facet != NULL) {
            next = facet->next;
            xmlSchemaFreeFacet(facet);
            facet = next;
        }
    }
    if (type->attrUses != NULL)
      xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
    if (type->memberTypes != NULL)
      xmlSchemaFreeTypeLinkList(type->memberTypes);
    if (type->facetSet != NULL) {
      xmlSchemaFacetLinkPtr next, link;

      link = type->facetSet;
      do {
          next = link->next;
          xmlFree(link);
          link = next;
      } while (link != NULL);
    }
    if (type->contModel != NULL)
        xmlRegFreeRegexp(type->contModel);
    xmlFree(type);
}

/**
 * xmlSchemaFreeModelGroupDef:
 * @item:  a schema model group definition
 *
 * Deallocates a schema model group definition.
 */
static void
xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
{
    if (item->annot != NULL)
      xmlSchemaFreeAnnot(item->annot);
    xmlFree(item);
}

/**
 * xmlSchemaFreeModelGroup:
 * @item:  a schema model group
 *
 * Deallocates a schema model group structure.
 */
static void
xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
{
    if (item->annot != NULL)
      xmlSchemaFreeAnnot(item->annot);
    xmlFree(item);
}

static void
xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
{
    if ((list == NULL) || (list->nbItems == 0))
      return;
    {
      xmlSchemaTreeItemPtr item;
      xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
      int i;

      for (i = 0; i < list->nbItems; i++) {
          item = items[i];
          if (item == NULL)
            continue;       
          switch (item->type) {
            case XML_SCHEMA_TYPE_SIMPLE:
            case XML_SCHEMA_TYPE_COMPLEX:
                xmlSchemaFreeType((xmlSchemaTypePtr) item);
                break;
            case XML_SCHEMA_TYPE_ATTRIBUTE:
                xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
                break;
            case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
                xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
                break;
            case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
                xmlSchemaFreeAttributeUseProhib(
                  (xmlSchemaAttributeUseProhibPtr) item);
                break;
            case XML_SCHEMA_TYPE_ELEMENT:
                xmlSchemaFreeElement((xmlSchemaElementPtr) item);
                break;
            case XML_SCHEMA_TYPE_PARTICLE:
                if (item->annot != NULL)
                  xmlSchemaFreeAnnot(item->annot);
                xmlFree(item);
                break;
            case XML_SCHEMA_TYPE_SEQUENCE:
            case XML_SCHEMA_TYPE_CHOICE:
            case XML_SCHEMA_TYPE_ALL:
                xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
                break;
            case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
                xmlSchemaFreeAttributeGroup(
                  (xmlSchemaAttributeGroupPtr) item);
                break;
            case XML_SCHEMA_TYPE_GROUP:
                xmlSchemaFreeModelGroupDef(
                  (xmlSchemaModelGroupDefPtr) item);
                break;
            case XML_SCHEMA_TYPE_ANY:
            case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
                xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
                break;
            case XML_SCHEMA_TYPE_IDC_KEY:
            case XML_SCHEMA_TYPE_IDC_UNIQUE:
            case XML_SCHEMA_TYPE_IDC_KEYREF:
                xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
                break;
            case XML_SCHEMA_TYPE_NOTATION:
                xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
                break;
            case XML_SCHEMA_EXTRA_QNAMEREF:
                xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
                break;
            default: {
                /* TODO: This should never be hit. */
                xmlSchemaPSimpleInternalErr(NULL,
                  "Internal error: xmlSchemaComponentListFree, "
                  "unexpected component type '%s'\n",
                  (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
                   }
                break;
          }
      }
      list->nbItems = 0;
    }
}

/**
 * xmlSchemaFree:
 * @schema:  a schema structure
 *
 * Deallocate a Schema structure.
 */
void
xmlSchemaFree(xmlSchemaPtr schema)
{
    if (schema == NULL)
        return;
    /* @volatiles is not used anymore :-/ */
    if (schema->volatiles != NULL)
      TODO
    /*
    * Note that those slots are not responsible for freeing
    * schema components anymore; this will now be done by
    * the schema buckets.
    */
    if (schema->notaDecl != NULL)
        xmlHashFree(schema->notaDecl, NULL);
    if (schema->attrDecl != NULL)
        xmlHashFree(schema->attrDecl, NULL);
    if (schema->attrgrpDecl != NULL)
        xmlHashFree(schema->attrgrpDecl, NULL);
    if (schema->elemDecl != NULL)
        xmlHashFree(schema->elemDecl, NULL);
    if (schema->typeDecl != NULL)
        xmlHashFree(schema->typeDecl, NULL);
    if (schema->groupDecl != NULL)
        xmlHashFree(schema->groupDecl, NULL);
    if (schema->idcDef != NULL)
        xmlHashFree(schema->idcDef, NULL);

    if (schema->schemasImports != NULL)
      xmlHashFree(schema->schemasImports,
                (xmlHashDeallocator) xmlSchemaBucketFree);
    if (schema->includes != NULL) {
      xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
      int i;
      for (i = 0; i < list->nbItems; i++) {
          xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);     
      }
      xmlSchemaItemListFree(list);
    }
    if (schema->annot != NULL)
        xmlSchemaFreeAnnot(schema->annot);
    /* Never free the doc here, since this will be done by the buckets. */

    xmlDictFree(schema->dict);
    xmlFree(schema);
}

/************************************************************************
 *                                                    *
 *                Debug functions                           *
 *                                                    *
 ************************************************************************/

#ifdef LIBXML_OUTPUT_ENABLED

static void
xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */

/**
 * xmlSchemaElementDump:
 * @elem:  an element
 * @output:  the file output
 *
 * Dump the element
 */
static void
xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
                     const xmlChar * name ATTRIBUTE_UNUSED,
                 const xmlChar * namespace ATTRIBUTE_UNUSED,
                     const xmlChar * context ATTRIBUTE_UNUSED)
{
    if (elem == NULL)
        return;


    fprintf(output, "Element");
    if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
      fprintf(output, " (global)");
    fprintf(output, ": '%s' ", elem->name);
    if (namespace != NULL)
      fprintf(output, "ns '%s'", namespace);
    fprintf(output, "\n");
#if 0
    if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
      fprintf(output, "  min %d ", elem->minOccurs);
        if (elem->maxOccurs >= UNBOUNDED)
            fprintf(output, "max: unbounded\n");
        else if (elem->maxOccurs != 1)
            fprintf(output, "max: %d\n", elem->maxOccurs);
        else
            fprintf(output, "\n");
    }
#endif
    /*
    * Misc other properties.
    */
    if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
      (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
      (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
      (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
      fprintf(output, "  props: ");
      if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
          fprintf(output, "[fixed] ");
      if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
          fprintf(output, "[default] ");
      if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
          fprintf(output, "[abstract] ");
      if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
          fprintf(output, "[nillable] ");
      fprintf(output, "\n");
    }
    /*
    * Default/fixed value.
    */
    if (elem->value != NULL)
      fprintf(output, "  value: '%s'\n", elem->value);
    /*
    * Type.
    */
    if (elem->namedType != NULL) {
      fprintf(output, "  type: '%s' ", elem->namedType);
      if (elem->namedTypeNs != NULL)
          fprintf(output, "ns '%s'\n", elem->namedTypeNs);
      else
          fprintf(output, "\n");
    } else if (elem->subtypes != NULL) {
      /*
      * Dump local types.
      */
      xmlSchemaTypeDump(elem->subtypes, output);
    }
    /*
    * Substitution group.
    */
    if (elem->substGroup != NULL) {
      fprintf(output, "  substitutionGroup: '%s' ", elem->substGroup);
      if (elem->substGroupNs != NULL)
          fprintf(output, "ns '%s'\n", elem->substGroupNs);
      else
          fprintf(output, "\n");
    }
}

/**
 * xmlSchemaAnnotDump:
 * @output:  the file output
 * @annot:  a annotation
 *
 * Dump the annotation
 */
static void
xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
{
    xmlChar *content;

    if (annot == NULL)
        return;

    content = xmlNodeGetContent(annot->content);
    if (content != NULL) {
        fprintf(output, "  Annot: %s\n", content);
        xmlFree(content);
    } else
        fprintf(output, "  Annot: empty\n");
}

/**
 * xmlSchemaContentModelDump:
 * @particle: the schema particle
 * @output: the file output
 * @depth: the depth used for intentation
 *
 * Dump a SchemaType structure
 */
static void
xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
{
    xmlChar *str = NULL;
    xmlSchemaTreeItemPtr term;
    char shift[100];
    int i;

    if (particle == NULL)
      return;
    for (i = 0;((i < depth) && (i < 25));i++)
        shift[2 * i] = shift[2 * i + 1] = ' ';
    shift[2 * i] = shift[2 * i + 1] = 0;
    fprintf(output, shift);
    if (particle->children == NULL) {
      fprintf(output, "MISSING particle term\n");
      return;
    }
    term = particle->children;
    if (term == NULL) {
      fprintf(output, "(NULL)");
    } else {
      switch (term->type) {
          case XML_SCHEMA_TYPE_ELEMENT:
            fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
                ((xmlSchemaElementPtr)term)->targetNamespace,
                ((xmlSchemaElementPtr)term)->name));
            FREE_AND_NULL(str);
            break;
          case XML_SCHEMA_TYPE_SEQUENCE:
            fprintf(output, "SEQUENCE");
            break;
          case XML_SCHEMA_TYPE_CHOICE:
            fprintf(output, "CHOICE");
            break;
          case XML_SCHEMA_TYPE_ALL:
            fprintf(output, "ALL");
            break;
          case XML_SCHEMA_TYPE_ANY:
            fprintf(output, "ANY");
            break;
          default:
            fprintf(output, "UNKNOWN\n");
            return;
      }
    }
    if (particle->minOccurs != 1)
      fprintf(output, " min: %d", particle->minOccurs);
    if (particle->maxOccurs >= UNBOUNDED)
      fprintf(output, " max: unbounded");
    else if (particle->maxOccurs != 1)
      fprintf(output, " max: %d", particle->maxOccurs);
    fprintf(output, "\n");
    if (term &&
      ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
       (term->type == XML_SCHEMA_TYPE_CHOICE) ||
       (term->type == XML_SCHEMA_TYPE_ALL)) &&
       (term->children != NULL)) {
      xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
          output, depth +1);
    }
    if (particle->next != NULL)
      xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
            output, depth);
}

/**
 * xmlSchemaAttrUsesDump:
 * @uses:  attribute uses list
 * @output:  the file output 
 *
 * Dumps a list of attribute use components.
 */
static void
xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
{
    xmlSchemaAttributeUsePtr use;
    xmlSchemaAttributeUseProhibPtr prohib;
    xmlSchemaQNameRefPtr ref;
    const xmlChar *name, *tns;
    xmlChar *str = NULL;
    int i;

    if ((uses == NULL) || (uses->nbItems == 0))
        return;

    fprintf(output, "  attributes:\n");    
    for (i = 0; i < uses->nbItems; i++) {
      use = uses->items[i];
      if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
          fprintf(output, "  [prohibition] ");
          prohib = (xmlSchemaAttributeUseProhibPtr) use;
          name = prohib->name;
          tns = prohib->targetNamespace;
      } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
          fprintf(output, "  [reference] ");
          ref = (xmlSchemaQNameRefPtr) use;
          name = ref->name;
          tns = ref->targetNamespace;
      } else {
          fprintf(output, "  [use] ");
          name = WXS_ATTRUSE_DECL_NAME(use);
          tns = WXS_ATTRUSE_DECL_TNS(use);
      }
      fprintf(output, "'%s'\n",
          (const char *) xmlSchemaFormatQName(&str, tns, name));
      FREE_AND_NULL(str);
    }
}

/**
 * xmlSchemaTypeDump:
 * @output:  the file output
 * @type:  a type structure
 *
 * Dump a SchemaType structure
 */
static void
xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
{
    if (type == NULL) {
        fprintf(output, "Type: NULL\n");
        return;
    }
    fprintf(output, "Type: ");
    if (type->name != NULL)
        fprintf(output, "'%s' ", type->name);
    else
        fprintf(output, "(no name) ");
    if (type->targetNamespace != NULL)
      fprintf(output, "ns '%s' ", type->targetNamespace);
    switch (type->type) {
        case XML_SCHEMA_TYPE_BASIC:
            fprintf(output, "[basic] ");
            break;
        case XML_SCHEMA_TYPE_SIMPLE:
            fprintf(output, "[simple] ");
            break;
        case XML_SCHEMA_TYPE_COMPLEX:
            fprintf(output, "[complex] ");
            break;
        case XML_SCHEMA_TYPE_SEQUENCE:
            fprintf(output, "[sequence] ");
            break;
        case XML_SCHEMA_TYPE_CHOICE:
            fprintf(output, "[choice] ");
            break;
        case XML_SCHEMA_TYPE_ALL:
            fprintf(output, "[all] ");
            break;
        case XML_SCHEMA_TYPE_UR:
            fprintf(output, "[ur] ");
            break;
        case XML_SCHEMA_TYPE_RESTRICTION:
            fprintf(output, "[restriction] ");
            break;
        case XML_SCHEMA_TYPE_EXTENSION:
            fprintf(output, "[extension] ");
            break;
        default:
            fprintf(output, "[unknown type %d] ", type->type);
            break;
    }
    fprintf(output, "content: ");
    switch (type->contentType) {
        case XML_SCHEMA_CONTENT_UNKNOWN:
            fprintf(output, "[unknown] ");
            break;
        case XML_SCHEMA_CONTENT_EMPTY:
            fprintf(output, "[empty] ");
            break;
        case XML_SCHEMA_CONTENT_ELEMENTS:
            fprintf(output, "[element] ");
            break;
        case XML_SCHEMA_CONTENT_MIXED:
            fprintf(output, "[mixed] ");
            break;
        case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
      /* not used. */
            break;
        case XML_SCHEMA_CONTENT_BASIC:
            fprintf(output, "[basic] ");
            break;
        case XML_SCHEMA_CONTENT_SIMPLE:
            fprintf(output, "[simple] ");
            break;
        case XML_SCHEMA_CONTENT_ANY:
            fprintf(output, "[any] ");
            break;
    }
    fprintf(output, "\n");
    if (type->base != NULL) {
        fprintf(output, "  base type: '%s'", type->base);
      if (type->baseNs != NULL)
          fprintf(output, " ns '%s'\n", type->baseNs);
      else
          fprintf(output, "\n");
    }
    if (type->attrUses != NULL)
      xmlSchemaAttrUsesDump(type->attrUses, output);
    if (type->annot != NULL)
        xmlSchemaAnnotDump(output, type->annot);
#ifdef DUMP_CONTENT_MODEL
    if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
      (type->subtypes != NULL)) {
      xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
          output, 1);
    }
#endif
}

/**
 * xmlSchemaDump:
 * @output:  the file output
 * @schema:  a schema structure
 *
 * Dump a Schema structure.
 */
void
xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
{
    if (output == NULL)
        return;
    if (schema == NULL) {
        fprintf(output, "Schemas: NULL\n");
        return;
    }
    fprintf(output, "Schemas: ");
    if (schema->name != NULL)
        fprintf(output, "%s, ", schema->name);
    else
        fprintf(output, "no name, ");
    if (schema->targetNamespace != NULL)
        fprintf(output, "%s", (const char *) schema->targetNamespace);
    else
        fprintf(output, "no target namespace");
    fprintf(output, "\n");
    if (schema->annot != NULL)
        xmlSchemaAnnotDump(output, schema->annot);
    xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
                output);
    xmlHashScanFull(schema->elemDecl,
                    (xmlHashScannerFull) xmlSchemaElementDump, output);
}

#ifdef DEBUG_IDC_NODE_TABLE
/**
 * xmlSchemaDebugDumpIDCTable:
 * @vctxt: the WXS validation context
 *
 * Displays the current IDC table for debug purposes.
 */
static void
xmlSchemaDebugDumpIDCTable(FILE * output,
                     const xmlChar *namespaceName,
                     const xmlChar *localName,
                     xmlSchemaPSVIIDCBindingPtr bind)
{
    xmlChar *str = NULL;
    const xmlChar *value;
    xmlSchemaPSVIIDCNodePtr tab;
    xmlSchemaPSVIIDCKeyPtr key;
    int i, j, res;

    fprintf(output, "IDC: TABLES on '%s'\n",
      xmlSchemaFormatQName(&str, namespaceName, localName));
    FREE_AND_NULL(str)

    if (bind == NULL)
      return;
    do {
      fprintf(output, "IDC:   BINDING '%s' (%d)\n",
          xmlSchemaGetComponentQName(&str, 
            bind->definition), bind->nbNodes);
      FREE_AND_NULL(str)
      for (i = 0; i < bind->nbNodes; i++) {
          tab = bind->nodeTable[i];
          fprintf(output, "         ( ");
          for (j = 0; j < bind->definition->nbFields; j++) {
            key = tab->keys[j];
            if ((key != NULL) && (key->val != NULL)) {
                res = xmlSchemaGetCanonValue(key->val, &value);
                if (res >= 0)
                  fprintf(output, "'%s' ", value);
                else
                  fprintf(output, "CANON-VALUE-FAILED ");
                if (res == 0)
                  FREE_AND_NULL(value)
            } else if (key != NULL)
                fprintf(output, "(no val), ");
            else
                fprintf(output, "(key missing), ");
          }
          fprintf(output, ")\n");
      }
      if (bind->dupls && bind->dupls->nbItems) {
          fprintf(output, "IDC:     dupls (%d):\n", bind->dupls->nbItems);
          for (i = 0; i < bind->dupls->nbItems; i++) {
            tab = bind->dupls->items[i];
            fprintf(output, "         ( ");
            for (j = 0; j < bind->definition->nbFields; j++) {
                key = tab->keys[j];
                if ((key != NULL) && (key->val != NULL)) {
                  res = xmlSchemaGetCanonValue(key->val, &value);
                  if (res >= 0)
                      fprintf(output, "'%s' ", value);
                  else
                      fprintf(output, "CANON-VALUE-FAILED ");
                  if (res == 0)
                      FREE_AND_NULL(value)
                } else if (key != NULL)
                fprintf(output, "(no val), ");
                  else
                      fprintf(output, "(key missing), ");
            }
            fprintf(output, ")\n");
          }
      }
      bind = bind->next;
    } while (bind != NULL);
}
#endif /* DEBUG_IDC */
#endif /* LIBXML_OUTPUT_ENABLED */

/************************************************************************
 *                                                    *
 *                Utilities                           *
 *                                                    *
 ************************************************************************/

/**
 * xmlSchemaGetPropNode:
 * @node: the element node
 * @name: the name of the attribute
 *
 * Seeks an attribute with a name of @name in
 * no namespace.
 *
 * Returns the attribute or NULL if not present.
 */
static xmlAttrPtr
xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
{
    xmlAttrPtr prop;

    if ((node == NULL) || (name == NULL))
      return(NULL);
    prop = node->properties;
    while (prop != NULL) {
        if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
          return(prop);
      prop = prop->next;
    }
    return (NULL);
}

/**
 * xmlSchemaGetPropNodeNs:
 * @node: the element node
 * @uri: the uri
 * @name: the name of the attribute
 *
 * Seeks an attribute with a local name of @name and
 * a namespace URI of @uri.
 *
 * Returns the attribute or NULL if not present.
 */
static xmlAttrPtr
xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
{
    xmlAttrPtr prop;

    if ((node == NULL) || (name == NULL))
      return(NULL);    
    prop = node->properties;
    while (prop != NULL) {
      if ((prop->ns != NULL) &&
          xmlStrEqual(prop->name, BAD_CAST name) &&
          xmlStrEqual(prop->ns->href, BAD_CAST uri))
          return(prop);
      prop = prop->next;
    }
    return (NULL);
}

static const xmlChar *
xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
{
    xmlChar *val;
    const xmlChar *ret;

    val = xmlNodeGetContent(node);
    if (val == NULL)
      val = xmlStrdup((xmlChar *)"");
    ret = xmlDictLookup(ctxt->dict, val, -1);
    xmlFree(val);
    return(ret);
}

static const xmlChar *
xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
{
    return((const xmlChar*) xmlNodeGetContent(node));
}

/**
 * xmlSchemaGetProp:
 * @ctxt: the parser context
 * @node: the node
 * @name: the property name
 *
 * Read a attribute value and internalize the string
 *
 * Returns the string or NULL if not present.
 */
static const xmlChar *
xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
                 const char *name)
{
    xmlChar *val;
    const xmlChar *ret;

    val = xmlGetNoNsProp(node, BAD_CAST name);
    if (val == NULL)
        return(NULL);
    ret = xmlDictLookup(ctxt->dict, val, -1);
    xmlFree(val);
    return(ret);
}

/************************************************************************
 *                                                    *
 *                Parsing functions                   *
 *                                                    *
 ************************************************************************/

#define WXS_FIND_GLOBAL_ITEM(slot)              \
    if (xmlStrEqual(nsName, schema->targetNamespace)) { \
      ret = xmlHashLookup(schema->slot, name); \
      if (ret != NULL) goto exit; \
    } \
    if (xmlHashSize(schema->schemasImports) > 1) { \
      xmlSchemaImportPtr import; \
      if (nsName == NULL) \
          import = xmlHashLookup(schema->schemasImports, \
            XML_SCHEMAS_NO_NAMESPACE); \
      else \
          import = xmlHashLookup(schema->schemasImports, nsName); \
      if (import == NULL) \
          goto exit; \
      ret = xmlHashLookup(import->schema->slot, name); \
    }

/**
 * xmlSchemaGetElem:
 * @schema:  the schema context
 * @name:  the element name
 * @ns:  the element namespace
 *
 * Lookup a global element declaration in the schema.
 *
 * Returns the element declaration or NULL if not found.
 */
static xmlSchemaElementPtr
xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
                 const xmlChar * nsName)
{
    xmlSchemaElementPtr ret = NULL;
    
    if ((name == NULL) || (schema == NULL))
        return(NULL);
    if (schema != NULL) {
      WXS_FIND_GLOBAL_ITEM(elemDecl)
    }   
exit:
#ifdef DEBUG
    if (ret == NULL) {
        if (nsName == NULL)
            fprintf(stderr, "Unable to lookup element decl. %s", name);
        else
            fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
                    nsName);
    }
#endif
    return (ret);
}

/**
 * xmlSchemaGetType:
 * @schema:  the main schema
 * @name:  the type's name
 * nsName:  the type's namespace
 *
 * Lookup a type in the schemas or the predefined types
 *
 * Returns the group definition or NULL if not found.
 */
static xmlSchemaTypePtr
xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
                 const xmlChar * nsName)
{
    xmlSchemaTypePtr ret = NULL;

    if (name == NULL)
        return (NULL);    
    /* First try the built-in types. */
    if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {   
      ret = xmlSchemaGetPredefinedType(name, nsName);
      if (ret != NULL)
          goto exit;
      /*
      * Note that we try the parsed schemas as well here
      * since one might have parsed the S4S, which contain more
      * than the built-in types.
      * TODO: Can we optimize this?
      */
    }
    if (schema != NULL) {
      WXS_FIND_GLOBAL_ITEM(typeDecl)
    } 
exit:

#ifdef DEBUG
    if (ret == NULL) {
        if (nsName == NULL)
            fprintf(stderr, "Unable to lookup type %s", name);
        else
            fprintf(stderr, "Unable to lookup type %s:%s", name,
                    nsName);
    }
#endif
    return (ret);
}

/**
 * xmlSchemaGetAttributeDecl:
 * @schema:  the context of the schema
 * @name:  the name of the attribute
 * @ns:  the target namespace of the attribute
 *
 * Lookup a an attribute in the schema or imported schemas
 *
 * Returns the attribute declaration or NULL if not found.
 */
static xmlSchemaAttributePtr
xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
                 const xmlChar * nsName)
{
    xmlSchemaAttributePtr ret = NULL;

    if ((name == NULL) || (schema == NULL))
        return (NULL);
    if (schema != NULL) {
      WXS_FIND_GLOBAL_ITEM(attrDecl)
    }
exit:
#ifdef DEBUG
    if (ret == NULL) {
        if (nsName == NULL)
            fprintf(stderr, "Unable to lookup attribute %s", name);
        else
            fprintf(stderr, "Unable to lookup attribute %s:%s", name,
                    nsName);
    }
#endif
    return (ret);
}

/**
 * xmlSchemaGetAttributeGroup:
 * @schema:  the context of the schema
 * @name:  the name of the attribute group
 * @ns:  the target namespace of the attribute group
 *
 * Lookup a an attribute group in the schema or imported schemas
 *
 * Returns the attribute group definition or NULL if not found.
 */
static xmlSchemaAttributeGroupPtr
xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
                 const xmlChar * nsName)
{
    xmlSchemaAttributeGroupPtr ret = NULL;

    if ((name == NULL) || (schema == NULL))
        return (NULL);
    if (schema != NULL) {
      WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
    }
exit:
    /* TODO:
    if ((ret != NULL) && (ret->redef != NULL)) {
      * Return the last redefinition. *
      ret = ret->redef;
    }
    */
#ifdef DEBUG
    if (ret == NULL) {
        if (nsName == NULL)
            fprintf(stderr, "Unable to lookup attribute group %s", name);
        else
            fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
                    nsName);
    }
#endif
    return (ret);
}

/**
 * xmlSchemaGetGroup:
 * @schema:  the context of the schema
 * @name:  the name of the group
 * @ns:  the target namespace of the group
 *
 * Lookup a group in the schema or imported schemas
 *
 * Returns the group definition or NULL if not found.
 */
static xmlSchemaModelGroupDefPtr
xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
                 const xmlChar * nsName)
{
    xmlSchemaModelGroupDefPtr ret = NULL;

    if ((name == NULL) || (schema == NULL))
        return (NULL);
    if (schema != NULL) {
      WXS_FIND_GLOBAL_ITEM(groupDecl)
    }
exit:
    
#ifdef DEBUG
    if (ret == NULL) {
        if (nsName == NULL)
            fprintf(stderr, "Unable to lookup group %s", name);
        else
            fprintf(stderr, "Unable to lookup group %s:%s", name,
                    nsName);
    }
#endif
    return (ret);
}

static xmlSchemaNotationPtr
xmlSchemaGetNotation(xmlSchemaPtr schema,
                 const xmlChar *name,
                 const xmlChar *nsName)
{
    xmlSchemaNotationPtr ret = NULL;

    if ((name == NULL) || (schema == NULL))
        return (NULL);
    if (schema != NULL) {
      WXS_FIND_GLOBAL_ITEM(notaDecl)
    }
exit:
    return (ret);
}

static xmlSchemaIDCPtr
xmlSchemaGetIDC(xmlSchemaPtr schema,
            const xmlChar *name,
            const xmlChar *nsName)
{
    xmlSchemaIDCPtr ret = NULL;

    if ((name == NULL) || (schema == NULL))
        return (NULL);
    if (schema != NULL) {
      WXS_FIND_GLOBAL_ITEM(idcDef)
    }
exit:
    return (ret);
}

/**
 * xmlSchemaGetNamedComponent:
 * @schema:  the schema
 * @name:  the name of the group
 * @ns:  the target namespace of the group
 *
 * Lookup a group in the schema or imported schemas
 *
 * Returns the group definition or NULL if not found.
 */
static xmlSchemaBasicItemPtr
xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
                     xmlSchemaTypeType itemType,
                     const xmlChar *name,
                     const xmlChar *targetNs)
{
    switch (itemType) {
      case XML_SCHEMA_TYPE_GROUP:
          return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
            name, targetNs));
      case XML_SCHEMA_TYPE_ELEMENT:
          return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
            name, targetNs));
      default:
          TODO
          return (NULL);
    }
}

/************************************************************************
 *                                                    *
 *                Parsing functions                   *
 *                                                    *
 ************************************************************************/

#define IS_BLANK_NODE(n)                                    \
    (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))

/**
 * xmlSchemaIsBlank:
 * @str:  a string
 * @len: the length of the string or -1
 *
 * Check if a string is ignorable
 *
 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
 */
static int
xmlSchemaIsBlank(xmlChar * str, int len)
{
    if (str == NULL)
        return (1);
    if (len < 0) {
      while (*str != 0) {
          if (!(IS_BLANK_CH(*str)))
            return (0);
          str++;
      }
    } else while ((*str != 0) && (len != 0)) {
      if (!(IS_BLANK_CH(*str)))
          return (0);
      str++;
      len--;
    }
    
    return (1);
}

#define WXS_COMP_NAME(c, t) ((t) (c))->name
#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
/*
* xmlSchemaFindRedefCompInGraph:
* ATTENTION TODO: This uses pointer comp. for strings.
*/
static xmlSchemaBasicItemPtr
xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
                        xmlSchemaTypeType type,
                        const xmlChar *name,
                        const xmlChar *nsName)
{
    xmlSchemaBasicItemPtr ret;
    int i;

    if ((bucket == NULL) || (name == NULL))
      return(NULL);
    if ((bucket->globals == NULL) ||
      (bucket->globals->nbItems == 0))
      goto subschemas;
    /*
    * Search in global components.
    */
    for (i = 0; i < bucket->globals->nbItems; i++) {
      ret = bucket->globals->items[i];
      if (ret->type == type) {
          switch (type) {
            case XML_SCHEMA_TYPE_COMPLEX:
            case XML_SCHEMA_TYPE_SIMPLE:
                if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
                  (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
                  nsName))
                {
                  return(ret);
                }
                break;
            case XML_SCHEMA_TYPE_GROUP:
                if ((WXS_COMP_NAME(ret,
                      xmlSchemaModelGroupDefPtr) == name) &&
                  (WXS_COMP_TNS(ret,
                      xmlSchemaModelGroupDefPtr) == nsName))
                {
                  return(ret);
                }
                break;
            case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
                if ((WXS_COMP_NAME(ret,
                      xmlSchemaAttributeGroupPtr) == name) &&
                  (WXS_COMP_TNS(ret,
                      xmlSchemaAttributeGroupPtr) == nsName))
                {
                  return(ret);
                }
                break;
            default:
                /* Should not be hit. */
                return(NULL);
          }       
      }
    }
subschemas:
    /*
    * Process imported/included schemas.
    */
    if (bucket->relations != NULL) {
      xmlSchemaSchemaRelationPtr rel = bucket->relations;

      /*
      * TODO: Marking the bucket will not avoid multiple searches
      * in the same schema, but avoids at least circularity.
      */
      bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
      do {
          if ((rel->bucket != NULL) &&
            ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
            ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
                type, name, nsName);
            if (ret != NULL)
                return(ret);
          }
          rel = rel->next;
      } while (rel != NULL);
       bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
    }
    return(NULL);
}

/**
 * xmlSchemaAddNotation:
 * @ctxt:  a schema parser context
 * @schema:  the schema being built
 * @name:  the item name
 *
 * Add an XML schema annotation declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaNotationPtr
xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                     const xmlChar *name, const xmlChar *nsName,
                 xmlNodePtr node ATTRIBUTE_UNUSED)
{
    xmlSchemaNotationPtr ret = NULL;

    if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
        return (NULL);

    ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaNotation));
    ret->type = XML_SCHEMA_TYPE_NOTATION;
    ret->name = name;
    ret->targetNamespace = nsName;
    /* TODO: do we need the node to be set?
    * ret->node = node;*/
    WXS_ADD_GLOBAL(ctxt, ret);
    return (ret);
}

/**
 * xmlSchemaAddAttribute:
 * @ctxt:  a schema parser context
 * @schema:  the schema being built
 * @name:  the item name
 * @namespace:  the namespace
 *
 * Add an XML schema Attrribute declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaAttributePtr
xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                      const xmlChar * name, const xmlChar * nsName,
                  xmlNodePtr node, int topLevel)
{
    xmlSchemaAttributePtr ret = NULL;

    if ((ctxt == NULL) || (schema == NULL))
        return (NULL);

    ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaAttribute));
    ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
    ret->node = node;
    ret->name = name;
    ret->targetNamespace = nsName;

    if (topLevel)
      WXS_ADD_GLOBAL(ctxt, ret);
    else
      WXS_ADD_LOCAL(ctxt, ret);
    WXS_ADD_PENDING(ctxt, ret);
    return (ret);
}

/**
 * xmlSchemaAddAttributeUse:
 * @ctxt:  a schema parser context
 * @schema:  the schema being built
 * @name:  the item name
 * @namespace:  the namespace
 *
 * Add an XML schema Attrribute declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaAttributeUsePtr
xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
                   xmlNodePtr node)
{
    xmlSchemaAttributeUsePtr ret = NULL;

    if (pctxt == NULL)
        return (NULL);

    ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
    if (ret == NULL) {
        xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaAttributeUse));
    ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
    ret->node = node;

    WXS_ADD_LOCAL(pctxt, ret);
    return (ret);
}

/*
* xmlSchemaAddRedef:
*
* Adds a redefinition information. This is used at a later stage to:
* resolve references to the redefined components and to check constraints.
*/
static xmlSchemaRedefPtr
xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,         
              xmlSchemaBucketPtr targetBucket,
              void *item,
              const xmlChar *refName,
              const xmlChar *refTargetNs)
{
    xmlSchemaRedefPtr ret;

    ret = (xmlSchemaRedefPtr)
      xmlMalloc(sizeof(xmlSchemaRedef));
    if (ret == NULL) {
      xmlSchemaPErrMemory(pctxt,
          "allocating redefinition info", NULL);
      return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaRedef));
    ret->item = item;
    ret->targetBucket = targetBucket;
    ret->refName = refName;
    ret->refTargetNs = refTargetNs;
    if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
      WXS_CONSTRUCTOR(pctxt)->redefs = ret;
    else
      WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
    WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;

    return (ret);
}

/**
 * xmlSchemaAddAttributeGroupDefinition:
 * @ctxt:  a schema parser context
 * @schema:  the schema being built
 * @name:  the item name
 * @nsName:  the target namespace
 * @node: the corresponding node
 *
 * Add an XML schema Attrribute Group definition.
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaAttributeGroupPtr
xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
                           xmlSchemaPtr schema ATTRIBUTE_UNUSED,
                     const xmlChar *name,
                     const xmlChar *nsName,
                     xmlNodePtr node)
{
    xmlSchemaAttributeGroupPtr ret = NULL;

    if ((pctxt == NULL) || (name == NULL))
        return (NULL);

    ret = (xmlSchemaAttributeGroupPtr)
        xmlMalloc(sizeof(xmlSchemaAttributeGroup));
    if (ret == NULL) {
      xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
      return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
    ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
    ret->name = name;
    ret->targetNamespace = nsName;
    ret->node = node;   

    /* TODO: Remove the flag. */
    ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
    if (pctxt->isRedefine) {
      pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
          ret, name, nsName);
      if (pctxt->redef == NULL) {
          xmlFree(ret);
          return(NULL);
      }         
      pctxt->redefCounter = 0;
    }
    WXS_ADD_GLOBAL(pctxt, ret);
    WXS_ADD_PENDING(pctxt, ret);
    return (ret);
}

/**
 * xmlSchemaAddElement:
 * @ctxt:  a schema parser context
 * @schema:  the schema being built
 * @name:  the type name
 * @namespace:  the type namespace
 *
 * Add an XML schema Element declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaElementPtr
xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
                    const xmlChar * name, const xmlChar * nsName,
                xmlNodePtr node, int topLevel)
{
    xmlSchemaElementPtr ret = NULL;

    if ((ctxt == NULL) || (name == NULL))
        return (NULL);

    ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaElement));
    ret->type = XML_SCHEMA_TYPE_ELEMENT;
    ret->name = name;
    ret->targetNamespace = nsName;
    ret->node = node;

    if (topLevel)
      WXS_ADD_GLOBAL(ctxt, ret);
    else
      WXS_ADD_LOCAL(ctxt, ret);
    WXS_ADD_PENDING(ctxt, ret);
    return (ret);
}

/**
 * xmlSchemaAddType:
 * @ctxt:  a schema parser context
 * @schema:  the schema being built
 * @name:  the item name
 * @namespace:  the namespace
 *
 * Add an XML schema item
 * *WARNING* this interface is highly subject to change
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaTypePtr
xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
             xmlSchemaTypeType type,
                 const xmlChar * name, const xmlChar * nsName,
             xmlNodePtr node, int topLevel)
{
    xmlSchemaTypePtr ret = NULL;

    if ((ctxt == NULL) || (schema == NULL))
        return (NULL);

    ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaType));
    ret->type = type;
    ret->name = name;
    ret->targetNamespace = nsName;
    ret->node = node;
    if (topLevel) {
      if (ctxt->isRedefine) {
          ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
            ret, name, nsName);
          if (ctxt->redef == NULL) {
            xmlFree(ret);
            return(NULL);
          }     
          ctxt->redefCounter = 0;
      }
      WXS_ADD_GLOBAL(ctxt, ret);
    } else
      WXS_ADD_LOCAL(ctxt, ret);
    WXS_ADD_PENDING(ctxt, ret);    
    return (ret);
}

static xmlSchemaQNameRefPtr
xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
                 xmlSchemaTypeType refType,
                 const xmlChar *refName,
                 const xmlChar *refNs)
{
    xmlSchemaQNameRefPtr ret;

    ret = (xmlSchemaQNameRefPtr)
      xmlMalloc(sizeof(xmlSchemaQNameRef));
    if (ret == NULL) {
      xmlSchemaPErrMemory(pctxt,
          "allocating QName reference item", NULL);
      return (NULL);
    }
    ret->node = NULL;
    ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
    ret->name = refName;
    ret->targetNamespace = refNs;
    ret->item = NULL;
    ret->itemType = refType;
    /*
    * Store the reference item in the schema.
    */
    WXS_ADD_LOCAL(pctxt, ret);
    return (ret);
}

static xmlSchemaAttributeUseProhibPtr
xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
{
    xmlSchemaAttributeUseProhibPtr ret;

    ret = (xmlSchemaAttributeUseProhibPtr)
      xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
    if (ret == NULL) {
      xmlSchemaPErrMemory(pctxt,
          "allocating attribute use prohibition", NULL);
      return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
    ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
    WXS_ADD_LOCAL(pctxt, ret);
    return (ret);
}


/**
 * xmlSchemaAddModelGroup:
 * @ctxt:  a schema parser context
 * @schema:  the schema being built
 * @type: the "compositor" type of the model group
 * @node: the node in the schema doc
 *
 * Adds a schema model group
 * *WARNING* this interface is highly subject to change
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaModelGroupPtr
xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
                   xmlSchemaPtr schema,
                   xmlSchemaTypeType type,
                   xmlNodePtr node)
{
    xmlSchemaModelGroupPtr ret = NULL;

    if ((ctxt == NULL) || (schema == NULL))
        return (NULL);

    ret = (xmlSchemaModelGroupPtr)
      xmlMalloc(sizeof(xmlSchemaModelGroup));
    if (ret == NULL) {
      xmlSchemaPErrMemory(ctxt, "allocating model group component",
          NULL);
      return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaModelGroup));
    ret->type = type;
    ret->node = node;
    WXS_ADD_LOCAL(ctxt, ret);
    if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
      (type == XML_SCHEMA_TYPE_CHOICE))
      WXS_ADD_PENDING(ctxt, ret);
    return (ret);
}


/**
 * xmlSchemaAddParticle:
 * @ctxt:  a schema parser context
 * @schema:  the schema being built
 * @node: the corresponding node in the schema doc
 * @min: the minOccurs
 * @max: the maxOccurs
 *
 * Adds an XML schema particle component.
 * *WARNING* this interface is highly subject to change
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaParticlePtr
xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt,
                 xmlNodePtr node, int min, int max)
{
    xmlSchemaParticlePtr ret = NULL;
    if (ctxt == NULL)
        return (NULL);

#ifdef DEBUG
    fprintf(stderr, "Adding particle component\n");
#endif
    ret = (xmlSchemaParticlePtr)
      xmlMalloc(sizeof(xmlSchemaParticle));
    if (ret == NULL) {
      xmlSchemaPErrMemory(ctxt, "allocating particle component",
          NULL);
      return (NULL);
    }
    ret->type = XML_SCHEMA_TYPE_PARTICLE;
    ret->annot = NULL;
    ret->node = node;
    ret->minOccurs = min;
    ret->maxOccurs = max;
    ret->next = NULL;
    ret->children = NULL;

    WXS_ADD_LOCAL(ctxt, ret);
    /* 
    * Note that addition to pending components will be done locally
    * to the specific parsing function, since the most particles
    * need not to be fixed up (i.e. the reference to be resolved).
    * REMOVED: WXS_ADD_PENDING(ctxt, ret);
    */
    return (ret);
}

/**
 * xmlSchemaAddModelGroupDefinition:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @name:  the group name
 *
 * Add an XML schema Group definition
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaModelGroupDefPtr
xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
                         xmlSchemaPtr schema,
                         const xmlChar *name,
                         const xmlChar *nsName,
                         xmlNodePtr node)
{
    xmlSchemaModelGroupDefPtr ret = NULL;

    if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
        return (NULL);

    ret = (xmlSchemaModelGroupDefPtr)
      xmlMalloc(sizeof(xmlSchemaModelGroupDef));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt, "adding group", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
    ret->name = name;
    ret->type = XML_SCHEMA_TYPE_GROUP;
    ret->node = node;
    ret->targetNamespace = nsName;

    if (ctxt->isRedefine) {
      ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
          ret, name, nsName);
      if (ctxt->redef == NULL) {
          xmlFree(ret);
          return(NULL);
      }         
      ctxt->redefCounter = 0;
    }
    WXS_ADD_GLOBAL(ctxt, ret);
    WXS_ADD_PENDING(ctxt, ret);
    return (ret);
}

/**
 * xmlSchemaNewWildcardNs:
 * @ctxt:  a schema validation context
 *
 * Creates a new wildcard namespace constraint.
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaWildcardNsPtr
xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
{
    xmlSchemaWildcardNsPtr ret;

    ret = (xmlSchemaWildcardNsPtr)
      xmlMalloc(sizeof(xmlSchemaWildcardNs));
    if (ret == NULL) {
      xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
      return (NULL);
    }
    ret->value = NULL;
    ret->next = NULL;
    return (ret);
}

static xmlSchemaIDCPtr
xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                  const xmlChar *name, const xmlChar *nsName,
              int category, xmlNodePtr node)
{
    xmlSchemaIDCPtr ret = NULL;    

    if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
        return (NULL);

    ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt,
          "allocating an identity-constraint definition", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaIDC));
    /* The target namespace of the parent element declaration. */
    ret->targetNamespace = nsName;
    ret->name = name;
    ret->type = category;
    ret->node = node;        
    
    WXS_ADD_GLOBAL(ctxt, ret);
    /*
    * Only keyrefs need to be fixup up.
    */
    if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
      WXS_ADD_PENDING(ctxt, ret);
    return (ret);
}

/**
 * xmlSchemaAddWildcard:
 * @ctxt:  a schema validation context
 * @schema: a schema
 *
 * Adds a wildcard.
 * It corresponds to a xsd:anyAttribute and xsd:any.
 *
 * Returns the new struture or NULL in case of error
 */
static xmlSchemaWildcardPtr
xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                 xmlSchemaTypeType type, xmlNodePtr node)
{
    xmlSchemaWildcardPtr ret = NULL;

    if ((ctxt == NULL) || (schema == NULL))
        return (NULL);

    ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
    if (ret == NULL) {
        xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaWildcard));
    ret->type = type;
    ret->node = node;    
    WXS_ADD_LOCAL(ctxt, ret);
    return (ret);
}

static void
xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
{
    if (group == NULL)
      return;
    if (group->members != NULL)
      xmlSchemaItemListFree(group->members);
    xmlFree(group);
}

static xmlSchemaSubstGroupPtr
xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
                   xmlSchemaElementPtr head)
{
    xmlSchemaSubstGroupPtr ret;

    /* Init subst group hash. */
    if (WXS_SUBST_GROUPS(pctxt) == NULL) {
      WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
      if (WXS_SUBST_GROUPS(pctxt) == NULL)
          return(NULL);
    }
    /* Create a new substitution group. */
    ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
    if (ret == NULL) {
      xmlSchemaPErrMemory(NULL,
          "allocating a substitution group container", NULL);
      return(NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaSubstGroup));
    ret->head = head;
    /* Create list of members. */
    ret->members = xmlSchemaItemListCreate();
    if (ret->members == NULL) {
      xmlSchemaSubstGroupFree(ret);
      return(NULL);
    }
    /* Add subst group to hash. */
    if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
      head->name, head->targetNamespace, ret) != 0) {
      PERROR_INT("xmlSchemaSubstGroupAdd",
          "failed to add a new substitution container");
      xmlSchemaSubstGroupFree(ret);
      return(NULL);
    }
    return(ret);
}

static xmlSchemaSubstGroupPtr
xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
                   xmlSchemaElementPtr head)
{   
    if (WXS_SUBST_GROUPS(pctxt) == NULL)
      return(NULL);
    return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
      head->name, head->targetNamespace));

}

/**
 * xmlSchemaAddElementSubstitutionMember:
 * @pctxt:  a schema parser context
 * @head:  the head of the substitution group
 * @member: the new member of the substitution group
 *
 * Allocate a new annotation structure.
 *
 * Returns the newly allocated structure or NULL in case or error
 */
static int
xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
                              xmlSchemaElementPtr head,
                              xmlSchemaElementPtr member)
{
    xmlSchemaSubstGroupPtr substGroup = NULL;

    if ((pctxt == NULL) || (head == NULL) || (member == NULL))
      return (-1);

    substGroup = xmlSchemaSubstGroupGet(pctxt, head);
    if (substGroup == NULL)
      substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
    if (substGroup == NULL)
      return(-1);
    if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
      return(-1);
    return(0);
}

/************************************************************************
 *                                                    *
 *          Utilities for parsing                           *
 *                                                    *
 ************************************************************************/

/**
 * xmlSchemaPValAttrNodeQNameValue:
 * @ctxt:  a schema parser context
 * @schema: the schema context
 * @ownerDes: the designation of the parent element
 * @ownerItem: the parent as a schema object
 * @value:  the QName value
 * @local: the resulting local part if found, the attribute value otherwise
 * @uri:  the resulting namespace URI if found
 *
 * Extracts the local name and the URI of a QName value and validates it.
 * This one is intended to be used on attribute values that
 * should resolve to schema components.
 *
 * Returns 0, in case the QName is valid, a positive error code
 * if not valid and -1 if an internal error occurs.
 */
static int
xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
                               xmlSchemaPtr schema,
                               xmlSchemaBasicItemPtr ownerItem,
                               xmlAttrPtr attr,
                               const xmlChar *value,
                               const xmlChar **uri,
                               const xmlChar **local)
{
    const xmlChar *pref;
    xmlNsPtr ns;
    int len, ret;

    *uri = NULL;
    *local = NULL;
    ret = xmlValidateQName(value, 1);
    if (ret > 0) {
      xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          ownerItem, (xmlNodePtr) attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
          NULL, value, NULL, NULL, NULL);
      *local = value;
      return (ctxt->err);
    } else if (ret < 0)
      return (-1);

    if (!strchr((char *) value, ':')) {
      ns = xmlSearchNs(attr->doc, attr->parent, NULL);
      if (ns)
          *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
      else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
          /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
          * parser context. */
          /*
          * This one takes care of included schemas with no
          * target namespace.
          */
          *uri = ctxt->targetNamespace;
      }
      *local = xmlDictLookup(ctxt->dict, value, -1);
      return (0);
    }
    /*
    * At this point xmlSplitQName3 has to return a local name.
    */
    *local = xmlSplitQName3(value, &len);
    *local = xmlDictLookup(ctxt->dict, *local, -1);
    pref = xmlDictLookup(ctxt->dict, value, len);
    ns = xmlSearchNs(attr->doc, attr->parent, pref);
    if (ns == NULL) {
      xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          ownerItem, (xmlNodePtr) attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
          "The value '%s' of simple type 'xs:QName' has no "
          "corresponding namespace declaration in scope", value, NULL);
      return (ctxt->err);
    } else {
        *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
    }
    return (0);
}

/**
 * xmlSchemaPValAttrNodeQName:
 * @ctxt:  a schema parser context
 * @schema: the schema context
 * @ownerDes: the designation of the owner element
 * @ownerItem: the owner as a schema object
 * @attr:  the attribute node
 * @local: the resulting local part if found, the attribute value otherwise
 * @uri:  the resulting namespace URI if found
 *
 * Extracts and validates the QName of an attribute value.
 * This one is intended to be used on attribute values that
 * should resolve to schema components.
 *
 * Returns 0, in case the QName is valid, a positive error code
 * if not valid and -1 if an internal error occurs.
 */
static int
xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
                               xmlSchemaPtr schema,
                               xmlSchemaBasicItemPtr ownerItem,
                               xmlAttrPtr attr,
                               const xmlChar **uri,
                               const xmlChar **local)
{
    const xmlChar *value;

    value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
    return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
      ownerItem, attr, value, uri, local));
}

/**
 * xmlSchemaPValAttrQName:
 * @ctxt:  a schema parser context
 * @schema: the schema context
 * @ownerDes: the designation of the parent element
 * @ownerItem: the owner as a schema object
 * @ownerElem:  the parent node of the attribute
 * @name:  the name of the attribute
 * @local: the resulting local part if found, the attribute value otherwise
 * @uri:  the resulting namespace URI if found
 *
 * Extracts and validates the QName of an attribute value.
 *
 * Returns 0, in case the QName is valid, a positive error code
 * if not valid and -1 if an internal error occurs.
 */
static int
xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
                           xmlSchemaPtr schema,
                           xmlSchemaBasicItemPtr ownerItem,
                           xmlNodePtr ownerElem,
                           const char *name,
                           const xmlChar **uri,
                           const xmlChar **local)
{
    xmlAttrPtr attr;

    attr = xmlSchemaGetPropNode(ownerElem, name);
    if (attr == NULL) {
      *local = NULL;
      *uri = NULL;
      return (0);
    }
    return (xmlSchemaPValAttrNodeQName(ctxt, schema,
      ownerItem, attr, uri, local));
}

/**
 * xmlSchemaPValAttrID:
 * @ctxt:  a schema parser context
 * @schema: the schema context
 * @ownerDes: the designation of the parent element
 * @ownerItem: the owner as a schema object
 * @ownerElem:  the parent node of the attribute
 * @name:  the name of the attribute
 *
 * Extracts and validates the ID of an attribute value.
 *
 * Returns 0, in case the ID is valid, a positive error code
 * if not valid and -1 if an internal error occurs.
 */
static int
xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
{
    int ret;
    const xmlChar *value;

    if (attr == NULL)
      return(0);
    value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
    ret = xmlValidateNCName(value, 1);
    if (ret == 0) {
      /*
      * NOTE: the IDness might have already be declared in the DTD
      */
      if (attr->atype != XML_ATTRIBUTE_ID) {
          xmlIDPtr res;
          xmlChar *strip;

          /*
          * TODO: Use xmlSchemaStrip here; it's not exported at this
          * moment.
          */
          strip = xmlSchemaCollapseString(value);
          if (strip != NULL) {
            xmlFree((xmlChar *) value);
            value = strip;
          }
          res = xmlAddID(NULL, attr->doc, value, attr);
          if (res == NULL) {
            ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
            xmlSchemaPSimpleTypeErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
                NULL, (xmlNodePtr) attr,
                xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
                NULL, NULL, "Duplicate value '%s' of simple "
                "type 'xs:ID'", value, NULL);
          } else
            attr->atype = XML_ATTRIBUTE_ID;
      }
    } else if (ret > 0) {
      ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
      xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          NULL, (xmlNodePtr) attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
          NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
          "not a valid 'xs:NCName'",
          value, NULL);
    }
    if (value != NULL)
      xmlFree((xmlChar *)value);

    return (ret);
}

static int
xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
                xmlNodePtr ownerElem,
                const xmlChar *name)
{
    xmlAttrPtr attr;

    attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
    if (attr == NULL)
      return(0);
    return(xmlSchemaPValAttrNodeID(ctxt, attr));

}

/**
 * xmlGetMaxOccurs:
 * @ctxt:  a schema validation context
 * @node:  a subtree containing XML Schema informations
 *
 * Get the maxOccurs property
 *
 * Returns the default if not found, or the value
 */
static int
xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
            int min, int max, int def, const char *expected)
{
    const xmlChar *val, *cur;
    int ret = 0;
    xmlAttrPtr attr;

    attr = xmlSchemaGetPropNode(node, "maxOccurs");
    if (attr == NULL)
      return (def);
    val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);

    if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
      if (max != UNBOUNDED) {
          xmlSchemaPSimpleTypeErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
            /* XML_SCHEMAP_INVALID_MINOCCURS, */
            NULL, (xmlNodePtr) attr, NULL, expected,
            val, NULL, NULL, NULL);
          return (def);
      } else
          return (UNBOUNDED);  /* encoding it with -1 might be another option */
    }

    cur = val;
    while (IS_BLANK_CH(*cur))
        cur++;
    if (*cur == 0) {
        xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          /* XML_SCHEMAP_INVALID_MINOCCURS, */
          NULL, (xmlNodePtr) attr, NULL, expected,
          val, NULL, NULL, NULL);
      return (def);
    }
    while ((*cur >= '0') && (*cur <= '9')) {
        ret = ret * 10 + (*cur - '0');
        cur++;
    }
    while (IS_BLANK_CH(*cur))
        cur++;
    /*
    * TODO: Restrict the maximal value to Integer.
    */
    if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
      xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          /* XML_SCHEMAP_INVALID_MINOCCURS, */
          NULL, (xmlNodePtr) attr, NULL, expected,
          val, NULL, NULL, NULL);
        return (def);
    }
    return (ret);
}

/**
 * xmlGetMinOccurs:
 * @ctxt:  a schema validation context
 * @node:  a subtree containing XML Schema informations
 *
 * Get the minOccurs property
 *
 * Returns the default if not found, or the value
 */
static int
xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
            int min, int max, int def, const char *expected)
{
    const xmlChar *val, *cur;
    int ret = 0;
    xmlAttrPtr attr;

    attr = xmlSchemaGetPropNode(node, "minOccurs");
    if (attr == NULL)
      return (def);
    val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
    cur = val;
    while (IS_BLANK_CH(*cur))
        cur++;
    if (*cur == 0) {
        xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          /* XML_SCHEMAP_INVALID_MINOCCURS, */
          NULL, (xmlNodePtr) attr, NULL, expected,
          val, NULL, NULL, NULL);
        return (def);
    }
    while ((*cur >= '0') && (*cur <= '9')) {
        ret = ret * 10 + (*cur - '0');
        cur++;
    }
    while (IS_BLANK_CH(*cur))
        cur++;
    /*
    * TODO: Restrict the maximal value to Integer.
    */
    if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
      xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          /* XML_SCHEMAP_INVALID_MINOCCURS, */
          NULL, (xmlNodePtr) attr, NULL, expected,
          val, NULL, NULL, NULL);
        return (def);
    }
    return (ret);
}

/**
 * xmlSchemaPGetBoolNodeValue:
 * @ctxt:  a schema validation context
 * @ownerDes:  owner designation
 * @ownerItem:  the owner as a schema item
 * @node: the node holding the value
 *
 * Converts a boolean string value into 1 or 0.
 *
 * Returns 0 or 1.
 */
static int
xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
                     xmlSchemaBasicItemPtr ownerItem,
                     xmlNodePtr node)
{
    xmlChar *value = NULL;
    int res = 0;

    value = xmlNodeGetContent(node);
    /*
    * 3.2.2.1 Lexical representation
    * An instance of a datatype that is defined as ·boolean·
    * can have the following legal literals {true, false, 1, 0}.
    */
    if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
        res = 1;
    else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
        res = 0;
    else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
      res = 1;
    else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
        res = 0;
    else {
        xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_INVALID_BOOLEAN,
          ownerItem, node,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
          NULL, BAD_CAST value,
          NULL, NULL, NULL);
    }
    if (value != NULL)
      xmlFree(value);
    return (res);
}

/**
 * xmlGetBooleanProp:
 * @ctxt:  a schema validation context
 * @node:  a subtree containing XML Schema informations
 * @name:  the attribute name
 * @def:  the default value
 *
 * Evaluate if a boolean property is set
 *
 * Returns the default if not found, 0 if found to be false,
 * 1 if found to be true
 */
static int
xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
              xmlNodePtr node,
                  const char *name, int def)
{
    const xmlChar *val;

    val = xmlSchemaGetProp(ctxt, node, name);
    if (val == NULL)
        return (def);
    /*
    * 3.2.2.1 Lexical representation
    * An instance of a datatype that is defined as ·boolean·
    * can have the following legal literals {true, false, 1, 0}.
    */
    if (xmlStrEqual(val, BAD_CAST "true"))
        def = 1;
    else if (xmlStrEqual(val, BAD_CAST "false"))
        def = 0;
    else if (xmlStrEqual(val, BAD_CAST "1"))
      def = 1;
    else if (xmlStrEqual(val, BAD_CAST "0"))
        def = 0;
    else {
        xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_INVALID_BOOLEAN,
          NULL,
          (xmlNodePtr) xmlSchemaGetPropNode(node, name),
          xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
          NULL, val, NULL, NULL, NULL);
    }
    return (def);
}

/************************************************************************
 *                                                    *
 *          Shema extraction from an Infoset                *
 *                                                    *
 ************************************************************************/
static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
                                                 ctxt, xmlSchemaPtr schema,
                                                 xmlNodePtr node,
                                     int topLevel);
static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
                                                  ctxt,
                                                  xmlSchemaPtr schema,
                                                  xmlNodePtr node,
                                      int topLevel);
static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
                                                  ctxt,
                                                  xmlSchemaPtr schema,
                                                  xmlNodePtr node,
                                      xmlSchemaTypeType parentType);
static xmlSchemaBasicItemPtr
xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
                       xmlSchemaPtr schema,
                       xmlNodePtr node,
                       xmlSchemaItemListPtr uses,
                       int parentType);
static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
                                           xmlSchemaPtr schema,
                                           xmlNodePtr node);
static xmlSchemaWildcardPtr
xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
                           xmlSchemaPtr schema, xmlNodePtr node);

/**
 * xmlSchemaPValAttrNodeValue:
 *
 * @ctxt:  a schema parser context
 * @ownerDes: the designation of the parent element
 * @ownerItem: the schema object owner if existent
 * @attr:  the schema attribute node being validated
 * @value: the value
 * @type: the built-in type to be validated against
 *
 * Validates a value against the given built-in type.
 * This one is intended to be used internally for validation
 * of schema attribute values during parsing of the schema.
 *
 * Returns 0 if the value is valid, a positive error code
 * number otherwise and -1 in case of an internal or API error.
 */
static int
xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
                     xmlSchemaBasicItemPtr ownerItem,
                     xmlAttrPtr attr,
                     const xmlChar *value,
                     xmlSchemaTypePtr type)
{

    int ret = 0;

    /*
    * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
    * one is really meant to be used internally, so better not.
    */
    if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
      return (-1);
    if (type->type != XML_SCHEMA_TYPE_BASIC) {
      PERROR_INT("xmlSchemaPValAttrNodeValue",
          "the given type is not a built-in type");
      return (-1);
    }
    switch (type->builtInType) {
      case XML_SCHEMAS_NCNAME:
      case XML_SCHEMAS_QNAME:
      case XML_SCHEMAS_ANYURI:
      case XML_SCHEMAS_TOKEN:
      case XML_SCHEMAS_LANGUAGE:
          ret = xmlSchemaValPredefTypeNode(type, value, NULL,
            (xmlNodePtr) attr);
          break;
      default: {
          PERROR_INT("xmlSchemaPValAttrNodeValue",
            "validation using the given type is not supported while "
            "parsing a schema");
          return (-1);
      }
    }
    /*
    * TODO: Should we use the S4S error codes instead?
    */
    if (ret < 0) {
      PERROR_INT("xmlSchemaPValAttrNodeValue",
          "failed to validate a schema attribute value");
      return (-1);
    } else if (ret > 0) {
      if (WXS_IS_LIST(type))
          ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
      else
          ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
      xmlSchemaPSimpleTypeErr(pctxt, 
          ret, ownerItem, (xmlNodePtr) attr,
          type, NULL, value, NULL, NULL, NULL);
    }
    return (ret);
}

/**
 * xmlSchemaPValAttrNode:
 *
 * @ctxt:  a schema parser context
 * @ownerDes: the designation of the parent element
 * @ownerItem: the schema object owner if existent
 * @attr:  the schema attribute node being validated
 * @type: the built-in type to be validated against
 * @value: the resulting value if any
 *
 * Extracts and validates a value against the given built-in type.
 * This one is intended to be used internally for validation
 * of schema attribute values during parsing of the schema.
 *
 * Returns 0 if the value is valid, a positive error code
 * number otherwise and -1 in case of an internal or API error.
 */
static int
xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
                     xmlSchemaBasicItemPtr ownerItem,
                     xmlAttrPtr attr,
                     xmlSchemaTypePtr type,
                     const xmlChar **value)
{
    const xmlChar *val;

    if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
      return (-1);

    val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
    if (value != NULL)
      *value = val;

    return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
      val, type));
}

/**
 * xmlSchemaPValAttr:
 *
 * @ctxt:  a schema parser context
 * @node: the element node of the attribute
 * @ownerDes: the designation of the parent element
 * @ownerItem: the schema object owner if existent
 * @ownerElem: the owner element node
 * @name:  the name of the schema attribute node
 * @type: the built-in type to be validated against
 * @value: the resulting value if any
 *
 * Extracts and validates a value against the given built-in type.
 * This one is intended to be used internally for validation
 * of schema attribute values during parsing of the schema.
 *
 * Returns 0 if the value is valid, a positive error code
 * number otherwise and -1 in case of an internal or API error.
 */
static int
xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,               
                   xmlSchemaBasicItemPtr ownerItem,
                   xmlNodePtr ownerElem,
                   const char *name,
                   xmlSchemaTypePtr type,
                   const xmlChar **value)
{
    xmlAttrPtr attr;

    if ((ctxt == NULL) || (type == NULL)) {
      if (value != NULL)
          *value = NULL;
      return (-1);
    }
    if (type->type != XML_SCHEMA_TYPE_BASIC) {
      if (value != NULL)
          *value = NULL;
      xmlSchemaPErr(ctxt, ownerElem,
          XML_SCHEMAP_INTERNAL,
          "Internal error: xmlSchemaPValAttr, the given "
          "type '%s' is not a built-in type.\n",
          type->name, NULL);
      return (-1);
    }
    attr = xmlSchemaGetPropNode(ownerElem, name);
    if (attr == NULL) {
      if (value != NULL)
          *value = NULL;
      return (0);
    }
    return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
      type, value));
}

static int
xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
              xmlSchemaPtr schema ATTRIBUTE_UNUSED,
              xmlNodePtr node,
              xmlAttrPtr attr,
              const xmlChar *namespaceName)
{
    /* TODO: Pointer comparison instead? */
    if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
      return (0);
    if (xmlStrEqual(xmlSchemaNs, namespaceName))
      return (0);
    /*
    * Check if the referenced namespace was <import>ed.
    */
    if (WXS_BUCKET(pctxt)->relations != NULL) {
      xmlSchemaSchemaRelationPtr rel;

      rel = WXS_BUCKET(pctxt)->relations;
      do {
          if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
            xmlStrEqual(namespaceName, rel->importNamespace))
            return (0);
          rel = rel->next;
      } while (rel != NULL);
    }
    /*
    * No matching <import>ed namespace found.
    */
    {
      xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;

      if (namespaceName == NULL)
          xmlSchemaCustomErr(ACTXT_CAST pctxt,
            XML_SCHEMAP_SRC_RESOLVE, n, NULL,
            "References from this schema to components in no "
            "namespace are not allowed, since not indicated by an "
            "import statement", NULL, NULL);
      else
          xmlSchemaCustomErr(ACTXT_CAST pctxt,
            XML_SCHEMAP_SRC_RESOLVE, n, NULL,
            "References from this schema to components in the "
            "namespace '%s' are not allowed, since not indicated by an "
            "import statement", namespaceName, NULL);
    }
    return (XML_SCHEMAP_SRC_RESOLVE);
}

/**
 * xmlSchemaParseLocalAttributes:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 * @type:  the hosting type where the attributes will be anchored
 *
 * Parses attribute uses and attribute declarations and
 * attribute group references. 
 */
static int
xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                        xmlNodePtr *child, xmlSchemaItemListPtr *list,
                  int parentType, int *hasRefs)
{
    void *item;

    while ((IS_SCHEMA((*child), "attribute")) ||
           (IS_SCHEMA((*child), "attributeGroup"))) {
        if (IS_SCHEMA((*child), "attribute")) {
          item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
            *list, parentType);
        } else {
            item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
          if ((item != NULL) && (hasRefs != NULL))
            *hasRefs = 1;
        }
      if (item != NULL) {
          if (*list == NULL) {
            /* TODO: Customize grow factor. */
            *list = xmlSchemaItemListCreate();
            if (*list == NULL)
                return(-1);
          }
          if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
            return(-1);
      }
        *child = (*child)->next;
    }
    return (0);
}

/**
 * xmlSchemaParseAnnotation:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Attrribute declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns -1 in case of error, 0 if the declaration is improper and
 *         1 in case of success.
 */
static xmlSchemaAnnotPtr
xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int needed)
{
    xmlSchemaAnnotPtr ret;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    int barked = 0;

    /*
    * INFO: S4S completed.
    */
    /*
    * id = ID
    * {any attributes with non-schema namespace . . .}>
    * Content: (appinfo | documentation)*
    */
    if ((ctxt == NULL) || (node == NULL))
        return (NULL);
    if (needed)
      ret = xmlSchemaNewAnnot(ctxt, node);
    else
      ret = NULL;
    attr = node->properties;
    while (attr != NULL) {
      if (((attr->ns == NULL) &&
          (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
          ((attr->ns != NULL) &&
          xmlStrEqual(attr->ns->href, xmlSchemaNs))) {

          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * And now for the children...
    */
    child = node->children;
    while (child != NULL) {
      if (IS_SCHEMA(child, "appinfo")) {
          /* TODO: make available the content of "appinfo". */
          /*
          * source = anyURI
          * {any attributes with non-schema namespace . . .}>
          * Content: ({any})*
          */
          attr = child->properties;
          while (attr != NULL) {
            if (((attr->ns == NULL) &&
                 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
                 ((attr->ns != NULL) &&
                  xmlStrEqual(attr->ns->href, xmlSchemaNs))) {

                xmlSchemaPIllegalAttrErr(ctxt,
                  XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
            }
            attr = attr->next;
          }
          xmlSchemaPValAttr(ctxt, NULL, child, "source",
            xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
          child = child->next;
      } else if (IS_SCHEMA(child, "documentation")) {
          /* TODO: make available the content of "documentation". */
          /*
          * source = anyURI
          * {any attributes with non-schema namespace . . .}>
          * Content: ({any})*
          */
          attr = child->properties;
          while (attr != NULL) {
            if (attr->ns == NULL) {
                if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
                  xmlSchemaPIllegalAttrErr(ctxt,
                      XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
                }
            } else {
                if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
                  (xmlStrEqual(attr->name, BAD_CAST "lang") &&
                  (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {

                  xmlSchemaPIllegalAttrErr(ctxt,
                      XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
                }
            }
            attr = attr->next;
          }
          /*
          * Attribute "xml:lang".
          */
          attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
          if (attr != NULL)
            xmlSchemaPValAttrNode(ctxt, NULL, attr,
            xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
          child = child->next;
      } else {
          if (!barked)
            xmlSchemaPContentErr(ctxt,
                XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
                NULL, node, child, NULL, "(appinfo | documentation)*");
          barked = 1;
          child = child->next;
      }
    }

    return (ret);
}

/**
 * xmlSchemaParseFacet:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Facet declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns the new type structure or NULL in case of error
 */
static xmlSchemaFacetPtr
xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                    xmlNodePtr node)
{
    xmlSchemaFacetPtr facet;
    xmlNodePtr child = NULL;
    const xmlChar *value;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    facet = xmlSchemaNewFacet();
    if (facet == NULL) {
        xmlSchemaPErrMemory(ctxt, "allocating facet", node);
        return (NULL);
    }
    facet->node = node;
    value = xmlSchemaGetProp(ctxt, node, "value");
    if (value == NULL) {
        xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
                       "Facet %s has no value\n", node->name, NULL);
        xmlSchemaFreeFacet(facet);
        return (NULL);
    }
    if (IS_SCHEMA(node, "minInclusive")) {
        facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
    } else if (IS_SCHEMA(node, "minExclusive")) {
        facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
    } else if (IS_SCHEMA(node, "maxInclusive")) {
        facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
    } else if (IS_SCHEMA(node, "maxExclusive")) {
        facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
    } else if (IS_SCHEMA(node, "totalDigits")) {
        facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
    } else if (IS_SCHEMA(node, "fractionDigits")) {
        facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
    } else if (IS_SCHEMA(node, "pattern")) {
        facet->type = XML_SCHEMA_FACET_PATTERN;
    } else if (IS_SCHEMA(node, "enumeration")) {
        facet->type = XML_SCHEMA_FACET_ENUMERATION;
    } else if (IS_SCHEMA(node, "whiteSpace")) {
        facet->type = XML_SCHEMA_FACET_WHITESPACE;
    } else if (IS_SCHEMA(node, "length")) {
        facet->type = XML_SCHEMA_FACET_LENGTH;
    } else if (IS_SCHEMA(node, "maxLength")) {
        facet->type = XML_SCHEMA_FACET_MAXLENGTH;
    } else if (IS_SCHEMA(node, "minLength")) {
        facet->type = XML_SCHEMA_FACET_MINLENGTH;
    } else {
        xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
                       "Unknown facet type %s\n", node->name, NULL);
        xmlSchemaFreeFacet(facet);
        return (NULL);
    }
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    facet->value = value;
    if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
      (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
      const xmlChar *fixed;

      fixed = xmlSchemaGetProp(ctxt, node, "fixed");
      if (fixed != NULL) {
          if (xmlStrEqual(fixed, BAD_CAST "true"))
            facet->fixed = 1;
      }
    }
    child = node->children;

    if (IS_SCHEMA(child, "annotation")) {
        facet->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
        child = child->next;
    }
    if (child != NULL) {
        xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
                       "Facet %s has unexpected child content\n",
                       node->name, NULL);
    }
    return (facet);
}

/**
 * xmlSchemaParseWildcardNs:
 * @ctxt:  a schema parser context
 * @wildc:  the wildcard, already created
 * @node:  a subtree containing XML Schema informations
 *
 * Parses the attribute "processContents" and "namespace"
 * of a xsd:anyAttribute and xsd:any.
 * *WARNING* this interface is highly subject to change
 *
 * Returns 0 if everything goes fine, a positive error code
 * if something is not valid and -1 if an internal error occurs.
 */
static int
xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
                   xmlSchemaPtr schema ATTRIBUTE_UNUSED,
                   xmlSchemaWildcardPtr wildc,
                   xmlNodePtr node)
{
    const xmlChar *pc, *ns, *dictnsItem;
    int ret = 0;
    xmlChar *nsItem;
    xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
    xmlAttrPtr attr;

    pc = xmlSchemaGetProp(ctxt, node, "processContents");
    if ((pc == NULL)
        || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
        wildc->processContents = XML_SCHEMAS_ANY_STRICT;
    } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
        wildc->processContents = XML_SCHEMAS_ANY_SKIP;
    } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
        wildc->processContents = XML_SCHEMAS_ANY_LAX;
    } else {
        xmlSchemaPSimpleTypeErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          NULL, node,
          NULL, "(strict | skip | lax)", pc,
          NULL, NULL, NULL);
        wildc->processContents = XML_SCHEMAS_ANY_STRICT;
      ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
    }
    /*
     * Build the namespace constraints.
     */
    attr = xmlSchemaGetPropNode(node, "namespace");
    ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
    if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
      wildc->any = 1;
    else if (xmlStrEqual(ns, BAD_CAST "##other")) {
      wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
      if (wildc->negNsSet == NULL) {
          return (-1);
      }
      wildc->negNsSet->value = ctxt->targetNamespace;
    } else {
      const xmlChar *end, *cur;

      cur = ns;
      do {
          while (IS_BLANK_CH(*cur))
            cur++;
          end = cur;
          while ((*end != 0) && (!(IS_BLANK_CH(*end))))
            end++;
          if (end == cur)
            break;
          nsItem = xmlStrndup(cur, end - cur);
          if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
                (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
            xmlSchemaPSimpleTypeErr(ctxt,
                XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
                NULL, (xmlNodePtr) attr,
                NULL,
                "((##any | ##other) | List of (xs:anyURI | "
                "(##targetNamespace | ##local)))",
                nsItem, NULL, NULL, NULL);
            ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
          } else {
            if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
                dictnsItem = ctxt->targetNamespace;
            } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
                dictnsItem = NULL;
            } else {
                /*
                * Validate the item (anyURI).
                */
                xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
                  nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
                dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
            }
            /*
            * Avoid dublicate namespaces.
            */
            tmp = wildc->nsSet;
            while (tmp != NULL) {
                if (dictnsItem == tmp->value)
                  break;
                tmp = tmp->next;
            }
            if (tmp == NULL) {
                tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
                if (tmp == NULL) {
                  xmlFree(nsItem);
                  return (-1);
                }
                tmp->value = dictnsItem;
                tmp->next = NULL;
                if (wildc->nsSet == NULL)
                  wildc->nsSet = tmp;
                else
                  lastNs->next = tmp;
                lastNs = tmp;
            }

          }
          xmlFree(nsItem);
          cur = end;
      } while (*cur != 0);
    }
    return (ret);
}

static int
xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
                         xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
                         xmlNodePtr node,
                         int minOccurs,
                         int maxOccurs) {

    if ((maxOccurs == 0) && ( minOccurs == 0))
      return (0);
    if (maxOccurs != UNBOUNDED) {
      /*
      * TODO: Maybe we should better not create the particle,
      * if min/max is invalid, since it could confuse the build of the
      * content model.
      */
      /*
      * 3.9.6 Schema Component Constraint: Particle Correct
      *
      */
      if (maxOccurs < 1) {
          /*
          * 2.2 {max occurs} must be greater than or equal to 1.
          */
          xmlSchemaPCustomAttrErr(ctxt,
            XML_SCHEMAP_P_PROPS_CORRECT_2_2,
            NULL, NULL,
            xmlSchemaGetPropNode(node, "maxOccurs"),
            "The value must be greater than or equal to 1");
          return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
      } else if (minOccurs > maxOccurs) {
          /*
          * 2.1 {min occurs} must not be greater than {max occurs}.
          */
          xmlSchemaPCustomAttrErr(ctxt,
            XML_SCHEMAP_P_PROPS_CORRECT_2_1,
            NULL, NULL,
            xmlSchemaGetPropNode(node, "minOccurs"),
            "The value must not be greater than the value of 'maxOccurs'");
          return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
      }
    }
    return (0);
}

/**
 * xmlSchemaParseAny:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * Parsea a XML schema <any> element. A particle and wildcard
 * will be created (except if minOccurs==maxOccurs==0, in this case
 * nothing will be created).
 * *WARNING* this interface is highly subject to change
 *
 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
 */
static xmlSchemaParticlePtr
xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                  xmlNodePtr node)
{
    xmlSchemaParticlePtr particle;
    xmlNodePtr child = NULL;
    xmlSchemaWildcardPtr wild;
    int min, max;
    xmlAttrPtr attr;
    xmlSchemaAnnotPtr annot = NULL;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
              (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * minOccurs/maxOccurs.
    */
    max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
      "(xs:nonNegativeInteger | unbounded)");
    min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
      "xs:nonNegativeInteger");
    xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
    /*
    * Create & parse the wildcard.
    */
    wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
    if (wild == NULL)
      return (NULL);
    xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        annot = xmlSchemaParseAnnotation(ctxt, child, 1);
        child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child,
          NULL, "(annotation?)");
    }
    /*
    * No component if minOccurs==maxOccurs==0.
    */
    if ((min == 0) && (max == 0)) {
      /* Don't free the wildcard, since it's already on the list. */
      return (NULL);
    }
    /*
    * Create the particle.
    */
    particle = xmlSchemaAddParticle(ctxt, node, min, max);
    if (particle == NULL)
        return (NULL);
    particle->annot = annot;
    particle->children = (xmlSchemaTreeItemPtr) wild;

    return (particle);
}

/**
 * xmlSchemaParseNotation:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Notation declaration
 *
 * Returns the new structure or NULL in case of error
 */
static xmlSchemaNotationPtr
xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                       xmlNodePtr node)
{
    const xmlChar *name;
    xmlSchemaNotationPtr ret;
    xmlNodePtr child = NULL;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);
    name = xmlSchemaGetProp(ctxt, node, "name");
    if (name == NULL) {
        xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
                       "Notation has no name\n", NULL, NULL);
        return (NULL);
    }
    ret = xmlSchemaAddNotation(ctxt, schema, name,
      ctxt->targetNamespace, node);
    if (ret == NULL)
        return (NULL);
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");

    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
        child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child,
          NULL, "(annotation?)");
    }

    return (ret);
}

/**
 * xmlSchemaParseAnyAttribute:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema AnyAttrribute declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns a wildcard or NULL.
 */
static xmlSchemaWildcardPtr
xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
                           xmlSchemaPtr schema, xmlNodePtr node)
{
    xmlSchemaWildcardPtr ret;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
      node);
    if (ret == NULL) {
        return (NULL);
    }
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
              (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * Parse the namespace list.
    */
    if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
      return (NULL);
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
        child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child,
          NULL, "(annotation?)");
    }

    return (ret);
}


/**
 * xmlSchemaParseAttribute:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Attrribute declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns the attribute declaration.
 */
static xmlSchemaBasicItemPtr
xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
                       xmlSchemaPtr schema,
                       xmlNodePtr node,
                       xmlSchemaItemListPtr uses,
                       int parentType)
{
    const xmlChar *attrValue, *name = NULL, *ns = NULL;
    xmlSchemaAttributeUsePtr use = NULL;    
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
    int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
    int     nberrors, hasForm = 0, defValueType = 0;

#define WXS_ATTR_DEF_VAL_DEFAULT 1
#define WXS_ATTR_DEF_VAL_FIXED 2

    /*
     * 3.2.3 Constraints on XML Representations of Attribute Declarations
     */

    if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);
    attr = xmlSchemaGetPropNode(node, "ref");
    if (attr != NULL) {
      if (xmlSchemaPValAttrNodeQName(pctxt, schema,
          NULL, attr, &tmpNs, &tmpName) != 0) {
          return (NULL);
      }
      if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
          return(NULL);
      isRef = 1;
    }
    nberrors = pctxt->nberrors;
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if (isRef) {
            if (xmlStrEqual(attr->name, BAD_CAST "id")) {
                xmlSchemaPValAttrNodeID(pctxt, attr);
                goto attr_next;
            } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
                goto attr_next;
            }
          } else {
            if (xmlStrEqual(attr->name, BAD_CAST "name")) {
                goto attr_next;
            } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
                xmlSchemaPValAttrNodeID(pctxt, attr);
                goto attr_next;
            } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
                xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
                  attr, &tmpNs, &tmpName);
                goto attr_next;
            } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
                /*
                * Evaluate the target namespace
                */
                hasForm = 1;            
                attrValue = xmlSchemaGetNodeContent(pctxt,
                  (xmlNodePtr) attr);
                if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
                  ns = pctxt->targetNamespace;
                } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
                {
                  xmlSchemaPSimpleTypeErr(pctxt,
                      XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
                      NULL, (xmlNodePtr) attr,
                      NULL, "(qualified | unqualified)",
                      attrValue, NULL, NULL, NULL);
                }
                goto attr_next;
            }
          }
          if (xmlStrEqual(attr->name, BAD_CAST "use")) {

            attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
            /* TODO: Maybe we need to normalize the value beforehand. */
            if (xmlStrEqual(attrValue, BAD_CAST "optional"))
                occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
            else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
                occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
            else if (xmlStrEqual(attrValue, BAD_CAST "required"))
                occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
            else {
                xmlSchemaPSimpleTypeErr(pctxt,
                  XML_SCHEMAP_INVALID_ATTR_USE,
                  NULL, (xmlNodePtr) attr,
                  NULL, "(optional | prohibited | required)",
                  attrValue, NULL, NULL, NULL);
            }
            goto attr_next;
          } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
            /*
            * 3.2.3 : 1
            * default and fixed must not both be present.
            */
            if (defValue) {
                xmlSchemaPMutualExclAttrErr(pctxt,
                  XML_SCHEMAP_SRC_ATTRIBUTE_1,
                  NULL, attr, "default", "fixed");
            } else {
                defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
                defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
            }
            goto attr_next;
          } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
            /*
            * 3.2.3 : 1
            * default and fixed must not both be present.
            */
            if (defValue) {
                xmlSchemaPMutualExclAttrErr(pctxt,
                  XML_SCHEMAP_SRC_ATTRIBUTE_1,
                  NULL, attr, "default", "fixed");
            } else {
                defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
                defValueType = WXS_ATTR_DEF_VAL_FIXED;
            }
            goto attr_next;
          }
      } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
          goto attr_next;

      xmlSchemaPIllegalAttrErr(pctxt,
          XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);

attr_next:
      attr = attr->next;
    }        
    /*
    * 3.2.3 : 2
    * If default and use are both present, use must have
    * the actual value optional.
    */
    if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
      (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
      xmlSchemaPSimpleTypeErr(pctxt,
          XML_SCHEMAP_SRC_ATTRIBUTE_2,
          NULL, node, NULL,
          "(optional | prohibited | required)", NULL,
          "The value of the attribute 'use' must be 'optional' "
          "if the attribute 'default' is present",
          NULL, NULL);
    }
    /*
    * We want correct attributes.
    */
    if (nberrors != pctxt->nberrors)
      return(NULL);
    if (! isRef) {
      xmlSchemaAttributePtr attrDecl;

      /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
      if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
          ns = pctxt->targetNamespace;          
      /*
      * 3.2.6 Schema Component Constraint: xsi: Not Allowed
      * TODO: Move this to the component layer.
      */
      if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
          xmlSchemaCustomErr(ACTXT_CAST pctxt,
            XML_SCHEMAP_NO_XSI,
            node, NULL,
            "The target namespace must not match '%s'",
            xmlSchemaInstanceNs, NULL);
      }
      attr = xmlSchemaGetPropNode(node, "name");
      if (attr == NULL) {
          xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
            NULL, node, "name", NULL);
          return (NULL);
      }
      if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
          return (NULL);
      }
      /*
      * 3.2.6 Schema Component Constraint: xmlns Not Allowed
      * TODO: Move this to the component layer.
      */
      if (xmlStrEqual(name, BAD_CAST "xmlns")) {
          xmlSchemaPSimpleTypeErr(pctxt,
            XML_SCHEMAP_NO_XMLNS,
            NULL, (xmlNodePtr) attr,
            xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
            "The value of the attribute must not match 'xmlns'",
            NULL, NULL);
          return (NULL);
      }
      if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
          goto check_children;
      /*
      * Create the attribute use component.
      */
      use = xmlSchemaAddAttributeUse(pctxt, node);
      if (use == NULL)
          return(NULL);
      use->occurs = occurs;
      /*
      * Create the attribute declaration.
      */
      attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
      if (attrDecl == NULL)
          return (NULL);
      if (tmpName != NULL) {
          attrDecl->typeName = tmpName;
          attrDecl->typeNs = tmpNs;
      }
      use->attrDecl = attrDecl;
      /*
      * Value constraint.
      */    
      if (defValue != NULL) {
          attrDecl->defValue = defValue;
          if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
            attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
      }
    } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
      xmlSchemaQNameRefPtr ref;

      /*
      * Create the attribute use component.
      */
      use = xmlSchemaAddAttributeUse(pctxt, node);
      if (use == NULL)
          return(NULL);
      /*
      * We need to resolve the reference at later stage.
      */
      WXS_ADD_PENDING(pctxt, use);
      use->occurs = occurs;
      /*
      * Create a QName reference to the attribute declaration.
      */
      ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
          tmpName, tmpNs);
      if (ref == NULL)
          return(NULL);
      /*
      * Assign the reference. This will be substituted for the
      * referenced attribute declaration when the QName is resolved.
      */
      use->attrDecl = WXS_ATTR_CAST ref;
      /*
      * Value constraint.
      */
      if (defValue != NULL)
          use->defValue = defValue;
          if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
            use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
    }
    
check_children:
    /*
    * And now for the children...
    */
    child = node->children;
    if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
      xmlSchemaAttributeUseProhibPtr prohib;    

      if (IS_SCHEMA(child, "annotation")) {
          xmlSchemaParseAnnotation(pctxt, child, 0);
          child = child->next;
      }
      if (child != NULL) {
          xmlSchemaPContentErr(pctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "(annotation?)");
      }
      /*
      * Check for pointlessness of attribute prohibitions.
      */
      if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {         
          xmlSchemaCustomWarning(ACTXT_CAST pctxt,
            XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
            node, NULL,
            "Skipping attribute use prohibition, since it is "
            "pointless inside an <attributeGroup>",
            NULL, NULL, NULL);
          return(NULL);
      } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
          xmlSchemaCustomWarning(ACTXT_CAST pctxt,
            XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
            node, NULL,
            "Skipping attribute use prohibition, since it is "
            "pointless when extending a type",
            NULL, NULL, NULL);
          return(NULL);
      }
      if (! isRef) {
          tmpName = name; 
          tmpNs = ns;
      }
      /*
      * Check for duplicate attribute prohibitions.
      */
      if (uses) {
          int i;
          
          for (i = 0; i < uses->nbItems; i++) {
            use = uses->items[i];
            if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
                (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
                (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
            {
                xmlChar *str = NULL;
                
                xmlSchemaCustomWarning(ACTXT_CAST pctxt,
                  XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
                  node, NULL,
                  "Skipping duplicate attribute use prohibition '%s'",
                  xmlSchemaFormatQName(&str, tmpNs, tmpName),
                  NULL, NULL);
                FREE_AND_NULL(str)              
                return(NULL);
            }     
          }
      }
      /*
      * Create the attribute prohibition helper component.
      */
      prohib = xmlSchemaAddAttributeUseProhib(pctxt);
      if (prohib == NULL)
          return(NULL);
      prohib->node = node;
      prohib->name = tmpName;
      prohib->targetNamespace = tmpNs;
      if (isRef) {
          /*
          * We need at least to resolve to the attribute declaration.
          */
          WXS_ADD_PENDING(pctxt, prohib);
      }
      return(WXS_BASIC_CAST prohib);
    } else {        
      if (IS_SCHEMA(child, "annotation")) {
          /*
          * TODO: Should this go into the attr decl?
          */
          use->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
          child = child->next;
      }
      if (isRef) {
          if (child != NULL) {
            if (IS_SCHEMA(child, "simpleType"))
                /*
                * 3.2.3 : 3.2
                * If ref is present, then all of <simpleType>,
                * form and type must be absent.
                */
                xmlSchemaPContentErr(pctxt,
                  XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
                  NULL, node, child, NULL,
                  "(annotation?)");
            else
                xmlSchemaPContentErr(pctxt,
                  XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
                  NULL, node, child, NULL,
                  "(annotation?)");
          }
      } else {
          if (IS_SCHEMA(child, "simpleType")) {
            if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
                /*
                * 3.2.3 : 4
                * type and <simpleType> must not both be present.
                */
                xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
                  NULL, node, child,
                  "The attribute 'type' and the <simpleType> child "
                  "are mutually exclusive", NULL);
            } else
                WXS_ATTRUSE_TYPEDEF(use) =
                  xmlSchemaParseSimpleType(pctxt, schema, child, 0);
            child = child->next;
          }
          if (child != NULL)
            xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "(annotation?, simpleType?)");
      }
    }   
    return (WXS_BASIC_CAST use);
}


static xmlSchemaAttributePtr
xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
                        xmlSchemaPtr schema,
                        xmlNodePtr node)
{
    const xmlChar *attrValue;
    xmlSchemaAttributePtr ret;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;

    /*
     * Note that the w3c spec assumes the schema to be validated with schema
     * for schemas beforehand.
     *
     * 3.2.3 Constraints on XML Representations of Attribute Declarations
     */
    if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);        
    /*
    * 3.2.3 : 3.1
    * One of ref or name must be present, but not both
    */
    attr = xmlSchemaGetPropNode(node, "name");
    if (attr == NULL) {
      xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node, "name", NULL);
      return (NULL);
    }
    if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
      xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
      return (NULL);
    }
    /*
    * 3.2.6 Schema Component Constraint: xmlns Not Allowed
    * TODO: Move this to the component layer.
    */
    if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
      xmlSchemaPSimpleTypeErr(pctxt,
          XML_SCHEMAP_NO_XMLNS,
          NULL, (xmlNodePtr) attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
          "The value of the attribute must not match 'xmlns'",
          NULL, NULL);
      return (NULL);
    }
    /*
    * 3.2.6 Schema Component Constraint: xsi: Not Allowed
    * TODO: Move this to the component layer.
    *       Or better leave it here and add it to the component layer
    *       if we have a schema construction API.
    */
    if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
      xmlSchemaCustomErr(ACTXT_CAST pctxt,
          XML_SCHEMAP_NO_XSI, node, NULL,
          "The target namespace must not match '%s'",
          xmlSchemaInstanceNs, NULL);
    }

    ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
      pctxt->targetNamespace, node, 1);
    if (ret == NULL)
      return (NULL);
    ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
      
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "type")))
          {
            xmlSchemaPIllegalAttrErr(pctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);          
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(pctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    xmlSchemaPValAttrQName(pctxt, schema, NULL,
      node, "type", &ret->typeNs, &ret->typeName);
    
    xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
    /*
    * Attribute "fixed".
    */
    ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
    if (ret->defValue != NULL)
      ret->flags |= XML_SCHEMAS_ATTR_FIXED;
    /*
    * Attribute "default".
    */
    attr = xmlSchemaGetPropNode(node, "default");
    if (attr != NULL) {
      /*
      * 3.2.3 : 1
      * default and fixed must not both be present.
      */
      if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
          xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
            WXS_BASIC_CAST ret, attr, "default", "fixed");
      } else
          ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
    }
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
        child = child->next;
    }
    if (IS_SCHEMA(child, "simpleType")) {
      if (ret->typeName != NULL) {
          /*
          * 3.2.3 : 4
          * type and <simpleType> must not both be present.
          */
          xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
            NULL, node, child,
            "The attribute 'type' and the <simpleType> child "
            "are mutually exclusive", NULL);
      } else
          ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
      child = child->next;
    }
    if (child != NULL)
      xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL,
          "(annotation?, simpleType?)");

    return (ret);
}

/**
 * xmlSchemaParseAttributeGroupRef:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * Parse an attribute group definition reference.
 * Note that a reference to an attribute group does not
 * correspond to any component at all.
 * *WARNING* this interface is highly subject to change
 *
 * Returns the attribute group or NULL in case of error.
 */
static xmlSchemaQNameRefPtr
xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
                        xmlSchemaPtr schema,
                        xmlNodePtr node)
{
    xmlSchemaQNameRefPtr ret;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    const xmlChar *refNs = NULL, *ref = NULL;

    if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    attr = xmlSchemaGetPropNode(node, "ref");
    if (attr == NULL) {
      xmlSchemaPMissingAttrErr(pctxt,
          XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node, "ref", NULL);
      return (NULL);
    } 
    xmlSchemaPValAttrNodeQName(pctxt, schema,
      NULL, attr, &refNs, &ref);
    if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
      return(NULL);
   
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "id")))
          {
            xmlSchemaPIllegalAttrErr(pctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(pctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    /* Attribute ID */
    xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");

    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      /*
      * TODO: We do not have a place to store the annotation, do we?
      */
        xmlSchemaParseAnnotation(pctxt, child, 0);
        child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(pctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL,
          "(annotation?)");
    }

    /*
    * Handle attribute group redefinitions.
    */
    if (pctxt->isRedefine && pctxt->redef &&
      (pctxt->redef->item->type ==
          XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
      (ref == pctxt->redef->refName) &&
      (refNs == pctxt->redef->refTargetNs))
    {
      /*
      * SPEC src-redefine:
      * (7.1) "If it has an <attributeGroup> among its contents
      * the ·actual value· of whose ref [attribute] is the same
      * as the ·actual value· of its own name attribute plus
      * target namespace, then it must have exactly one such group."
      */
      if (pctxt->redefCounter != 0) {
          xmlChar *str = NULL;
          
          xmlSchemaCustomErr(ACTXT_CAST pctxt,
            XML_SCHEMAP_SRC_REDEFINE, node, NULL,
            "The redefining attribute group definition "
            "'%s' must not contain more than one "
            "reference to the redefined definition",
            xmlSchemaFormatQName(&str, refNs, ref), NULL);
          FREE_AND_NULL(str);
          return(NULL);
      }
      pctxt->redefCounter++;
      /*
      * URGENT TODO: How to ensure that the reference will not be
      * handled by the normal component resolution mechanism?
      */
      ret = xmlSchemaNewQNameRef(pctxt,
          XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
      if (ret == NULL)
          return(NULL);
      ret->node = node;
      pctxt->redef->reference = WXS_BASIC_CAST ret;
    } else {
      /*
      * Create a QName-reference helper component. We will substitute this
      * component for the attribute uses of the referenced attribute group
      * definition.
      */
      ret = xmlSchemaNewQNameRef(pctxt,
          XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
      if (ret == NULL)
          return(NULL);
      ret->node = node;
      /* Add to pending items, to be able to resolve the reference. */
      WXS_ADD_PENDING(pctxt, ret);
    }    
    return (ret);
}

/**
 * xmlSchemaParseAttributeGroupDefinition:
 * @pctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Attribute Group declaration
 * *WARNING* this interface is highly subject to change
 *
 * Returns the attribute group definition or NULL in case of error.
 */
static xmlSchemaAttributeGroupPtr
xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
                               xmlSchemaPtr schema,
                               xmlNodePtr node)
{
    const xmlChar *name;
    xmlSchemaAttributeGroupPtr ret;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    int hasRefs = 0;

    if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    attr = xmlSchemaGetPropNode(node, "name");
    if (attr == NULL) {
      xmlSchemaPMissingAttrErr(pctxt,
          XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node, "name", NULL);
      return (NULL);
    }
    /*
    * The name is crucial, exit if invalid.
    */
    if (xmlSchemaPValAttrNode(pctxt,
      NULL, attr,
      xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
      return (NULL);
    }
    ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
      name, pctxt->targetNamespace, node);
    if (ret == NULL)
      return (NULL);    
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "id")))
          {
            xmlSchemaPIllegalAttrErr(pctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(pctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    /* Attribute ID */
    xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
        child = child->next;
    }
    /*
    * Parse contained attribute decls/refs.
    */
    if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
      (xmlSchemaItemListPtr *) &(ret->attrUses),
      XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
      return(NULL);
    if (hasRefs)
      ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
    /*
    * Parse the attribute wildcard.
    */
    if (IS_SCHEMA(child, "anyAttribute")) {
      ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
          schema, child);
      child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(pctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL,
          "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
    }
    return (ret);
}

/**
 * xmlSchemaPValAttrFormDefault:
 * @value:  the value
 * @flags: the flags to be modified
 * @flagQualified: the specific flag for "qualified"
 *
 * Returns 0 if the value is valid, 1 otherwise.
 */
static int
xmlSchemaPValAttrFormDefault(const xmlChar *value,
                       int *flags,
                       int flagQualified)
{
    if (xmlStrEqual(value, BAD_CAST "qualified")) {
      if  ((*flags & flagQualified) == 0)
          *flags |= flagQualified;
    } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
      return (1);

    return (0);
}

/**
 * xmlSchemaPValAttrBlockFinal:
 * @value:  the value
 * @flags: the flags to be modified
 * @flagAll: the specific flag for "#all"
 * @flagExtension: the specific flag for "extension"
 * @flagRestriction: the specific flag for "restriction"
 * @flagSubstitution: the specific flag for "substitution"
 * @flagList: the specific flag for "list"
 * @flagUnion: the specific flag for "union"
 *
 * Validates the value of the attribute "final" and "block". The value
 * is converted into the specified flag values and returned in @flags.
 *
 * Returns 0 if the value is valid, 1 otherwise.
 */

static int
xmlSchemaPValAttrBlockFinal(const xmlChar *value,
                      int *flags,
                      int flagAll,
                      int flagExtension,
                      int flagRestriction,
                      int flagSubstitution,
                      int flagList,
                      int flagUnion)
{
    int ret = 0;

    /*
    * TODO: This does not check for dublicate entries.
    */
    if ((flags == NULL) || (value == NULL))
      return (-1);
    if (value[0] == 0)
      return (0);
    if (xmlStrEqual(value, BAD_CAST "#all")) {
      if (flagAll != -1)
          *flags |= flagAll;
      else {
          if (flagExtension != -1)
            *flags |= flagExtension;
          if (flagRestriction != -1)
            *flags |= flagRestriction;
          if (flagSubstitution != -1)
            *flags |= flagSubstitution;
          if (flagList != -1)
            *flags |= flagList;
          if (flagUnion != -1)
            *flags |= flagUnion;
      }
    } else {
      const xmlChar *end, *cur = value;
      xmlChar *item;

      do {
          while (IS_BLANK_CH(*cur))
            cur++;
          end = cur;
          while ((*end != 0) && (!(IS_BLANK_CH(*end))))
            end++;
          if (end == cur)
            break;
          item = xmlStrndup(cur, end - cur);
          if (xmlStrEqual(item, BAD_CAST "extension")) {
            if (flagExtension != -1) {
                if ((*flags & flagExtension) == 0)
                  *flags |= flagExtension;
            } else
                ret = 1;
          } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
            if (flagRestriction != -1) {
                if ((*flags & flagRestriction) == 0)
                  *flags |= flagRestriction;
            } else
                ret = 1;
          } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
            if (flagSubstitution != -1) {
                if ((*flags & flagSubstitution) == 0)
                  *flags |= flagSubstitution;
            } else
                ret = 1;
          } else if (xmlStrEqual(item, BAD_CAST "list")) {
            if (flagList != -1) {
                if ((*flags & flagList) == 0)
                  *flags |= flagList;
            } else
                ret = 1;
          } else if (xmlStrEqual(item, BAD_CAST "union")) {
            if (flagUnion != -1) {
                if ((*flags & flagUnion) == 0)
                  *flags |= flagUnion;
            } else
                ret = 1;
          } else
            ret = 1;
          if (item != NULL)
            xmlFree(item);
          cur = end;
      } while ((ret == 0) && (*cur != 0));
    }

    return (ret);
}

static int
xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
                       xmlSchemaIDCPtr idc,
                       xmlSchemaIDCSelectPtr selector,
                       xmlAttrPtr attr,
                       int isField)
{
    xmlNodePtr node;

    /*
    * c-selector-xpath:
    * Schema Component Constraint: Selector Value OK
    *
    * TODO: 1 The {selector} must be a valid XPath expression, as defined
    * in [XPath].
    */
    if (selector == NULL) {
      xmlSchemaPErr(ctxt, idc->node,
          XML_SCHEMAP_INTERNAL,
          "Internal error: xmlSchemaCheckCSelectorXPath, "
          "the selector is not specified.\n", NULL, NULL);
      return (-1);
    }
    if (attr == NULL)
      node = idc->node;
    else
      node = (xmlNodePtr) attr;
    if (selector->xpath == NULL) {
      xmlSchemaPCustomErr(ctxt,
          /* TODO: Adjust error code. */
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          NULL, node,
          "The XPath expression of the selector is not valid", NULL);
      return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
    } else {
      const xmlChar **nsArray = NULL;
      xmlNsPtr *nsList = NULL;
      /*
      * Compile the XPath expression.
      */
      /*
      * TODO: We need the array of in-scope namespaces for compilation.
      * TODO: Call xmlPatterncompile with different options for selector/
      * field.
      */
      if (attr == NULL)
          nsList = NULL;
      else
          nsList = xmlGetNsList(attr->doc, attr->parent);
      /*
      * Build an array of prefixes and namespaces.
      */
      if (nsList != NULL) {
          int i, count = 0;

          for (i = 0; nsList[i] != NULL; i++)
            count++;

          nsArray = (const xmlChar **) xmlMalloc(
            (count * 2 + 1) * sizeof(const xmlChar *));
          if (nsArray == NULL) {
            xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
                NULL);
            xmlFree(nsList);
            return (-1);
          }
          for (i = 0; i < count; i++) {
            nsArray[2 * i] = nsList[i]->href;
            nsArray[2 * i + 1] = nsList[i]->prefix;
          }
          nsArray[count * 2] = NULL;
          xmlFree(nsList);
      }
      /*
      * TODO: Differentiate between "selector" and "field".
      */
      if (isField)
          selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
            NULL, XML_PATTERN_XSFIELD, nsArray);
      else
          selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
            NULL, XML_PATTERN_XSSEL, nsArray);
      if (nsArray != NULL)
          xmlFree((xmlChar **) nsArray);

      if (selector->xpathComp == NULL) {
          xmlSchemaPCustomErr(ctxt,
            /* TODO: Adjust error code? */
            XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
            NULL, node,
            "The XPath expression '%s' could not be "
            "compiled", selector->xpath);
          return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
      }
    }
    return (0);
}

#define ADD_ANNOTATION(annot)   \
    xmlSchemaAnnotPtr cur = item->annot; \
    if (item->annot == NULL) {  \
      item->annot = annot;    \
      return (annot);         \
    }                           \
    cur = item->annot;          \
    if (cur->next != NULL) {    \
      cur = cur->next;  \
    }                           \
    cur->next = annot;

/**
 * xmlSchemaAssignAnnotation:
 * @item: the schema component
 * @annot: the annotation
 *
 * Adds the annotation to the given schema component.
 *
 * Returns the given annotaion.
 */
static xmlSchemaAnnotPtr
xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
                   xmlSchemaAnnotPtr annot)
{
    if ((annItem == NULL) || (annot == NULL))
      return (NULL);
    switch (annItem->type) {
      case XML_SCHEMA_TYPE_ELEMENT: {
            xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_TYPE_ATTRIBUTE: {
            xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
      case XML_SCHEMA_TYPE_ANY: {
            xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_TYPE_PARTICLE:
      case XML_SCHEMA_TYPE_IDC_KEY:
      case XML_SCHEMA_TYPE_IDC_KEYREF:
      case XML_SCHEMA_TYPE_IDC_UNIQUE: {
            xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
            xmlSchemaAttributeGroupPtr item =
                (xmlSchemaAttributeGroupPtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_TYPE_NOTATION: {
            xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_FACET_MININCLUSIVE:
      case XML_SCHEMA_FACET_MINEXCLUSIVE:
      case XML_SCHEMA_FACET_MAXINCLUSIVE:
      case XML_SCHEMA_FACET_MAXEXCLUSIVE:
      case XML_SCHEMA_FACET_TOTALDIGITS:
      case XML_SCHEMA_FACET_FRACTIONDIGITS:
      case XML_SCHEMA_FACET_PATTERN:
      case XML_SCHEMA_FACET_ENUMERATION:
      case XML_SCHEMA_FACET_WHITESPACE:
      case XML_SCHEMA_FACET_LENGTH:
      case XML_SCHEMA_FACET_MAXLENGTH:
      case XML_SCHEMA_FACET_MINLENGTH: {
            xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_TYPE_SIMPLE:
      case XML_SCHEMA_TYPE_COMPLEX: {
            xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_TYPE_GROUP: {
            xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      case XML_SCHEMA_TYPE_SEQUENCE:
      case XML_SCHEMA_TYPE_CHOICE:
      case XML_SCHEMA_TYPE_ALL: {
            xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
            ADD_ANNOTATION(annot)
          }
          break;
      default:
           xmlSchemaPCustomErr(NULL,
            XML_SCHEMAP_INTERNAL,
            NULL, NULL,
            "Internal error: xmlSchemaAddAnnotation, "
            "The item is not a annotated schema component", NULL);
           break;
    }
    return (annot);
}

/**
 * xmlSchemaParseIDCSelectorAndField:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * Parses a XML Schema identity-contraint definition's
 * <selector> and <field> elements.
 *
 * Returns the parsed identity-constraint definition.
 */
static xmlSchemaIDCSelectPtr
xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,                  
                    xmlSchemaIDCPtr idc,
                    xmlNodePtr node,
                    int isField)
{
    xmlSchemaIDCSelectPtr item;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;

    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    /*
    * Create the item.
    */
    item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
    if (item == NULL) {
        xmlSchemaPErrMemory(ctxt,
          "allocating a 'selector' of an identity-constraint definition",
          NULL);
        return (NULL);
    }
    memset(item, 0, sizeof(xmlSchemaIDCSelect));
    /*
    * Attribute "xpath" (mandatory).
    */
    attr = xmlSchemaGetPropNode(node, "xpath");
    if (attr == NULL) {
      xmlSchemaPMissingAttrErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node,
          "name", NULL);
    } else {
      item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
      /*
      * URGENT TODO: "field"s have an other syntax than "selector"s.
      */

      if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
          isField) == -1) {
          xmlSchemaPErr(ctxt,
            (xmlNodePtr) attr,
            XML_SCHEMAP_INTERNAL,
            "Internal error: xmlSchemaParseIDCSelectorAndField, "
            "validating the XPath expression of a IDC selector.\n",
            NULL, NULL);
      }

    }
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      /*
      * Add the annotation to the parent IDC.
      */
      xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
          xmlSchemaParseAnnotation(ctxt, child, 1));
      child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child,
          NULL, "(annotation?)");
    }

    return (item);
}

/**
 * xmlSchemaParseIDC:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * Parses a XML Schema identity-contraint definition.
 *
 * Returns the parsed identity-constraint definition.
 */
static xmlSchemaIDCPtr
xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
              xmlSchemaPtr schema,
              xmlNodePtr node,
              xmlSchemaTypeType idcCategory,
              const xmlChar *targetNamespace)
{
    xmlSchemaIDCPtr item = NULL;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    const xmlChar *name = NULL;
    xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;

    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
            ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
             (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    /*
    * Attribute "name" (mandatory).
    */
    attr = xmlSchemaGetPropNode(node, "name");
    if (attr == NULL) {
      xmlSchemaPMissingAttrErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node,
          "name", NULL);
      return (NULL);
    } else if (xmlSchemaPValAttrNode(ctxt,
      NULL, attr,
      xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
      return (NULL);
    }
    /* Create the component. */
    item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
      idcCategory, node);
    if (item == NULL)
      return(NULL);

    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
      /*
      * Attribute "refer" (mandatory).
      */
      attr = xmlSchemaGetPropNode(node, "refer");
      if (attr == NULL) {
          xmlSchemaPMissingAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_MISSING,
            NULL, node,
            "refer", NULL);
      } else {
          /*
          * Create a reference item.
          */
          item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
            NULL, NULL);
          if (item->ref == NULL)
            return (NULL);
          xmlSchemaPValAttrNodeQName(ctxt, schema,
            NULL, attr,
            &(item->ref->targetNamespace),
            &(item->ref->name));
          xmlSchemaCheckReference(ctxt, schema, node, attr,
            item->ref->targetNamespace);
      }
    }
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
      child = child->next;
    }
    if (child == NULL) {
      xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_MISSING,
            NULL, node, child,
            "A child element is missing",
            "(annotation?, (selector, field+))");
    }
    /*
    * Child element <selector>.
    */
    if (IS_SCHEMA(child, "selector")) {
      item->selector = xmlSchemaParseIDCSelectorAndField(ctxt,
          item, child, 0);
      child = child->next;
      /*
      * Child elements <field>.
      */
      if (IS_SCHEMA(child, "field")) {
          do {
            field = xmlSchemaParseIDCSelectorAndField(ctxt,
                item, child, 1);
            if (field != NULL) {
                field->index = item->nbFields;
                item->nbFields++;
                if (lastField != NULL)
                  lastField->next = field;
                else
                  item->fields = field;
                lastField = field;
            }
            child = child->next;
          } while (IS_SCHEMA(child, "field"));
      } else {
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child,
            NULL, "(annotation?, (selector, field+))");
      }
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child,
          NULL, "(annotation?, (selector, field+))");
    }

    return (item);
}

/**
 * xmlSchemaParseElement:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 * @topLevel: indicates if this is global declaration
 *
 * Parses a XML schema element declaration.
 * *WARNING* this interface is highly subject to change
 *
 * Returns the element declaration or a particle; NULL in case
 * of an error or if the particle has minOccurs==maxOccurs==0.
 */
static xmlSchemaBasicItemPtr
xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                      xmlNodePtr node, int *isElemRef, int topLevel)
{
    xmlSchemaElementPtr decl = NULL;
    xmlSchemaParticlePtr particle = NULL;
    xmlSchemaAnnotPtr annot = NULL;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr, nameAttr;
    int min, max, isRef = 0;
    xmlChar *des = NULL;

    /* 3.3.3 Constraints on XML Representations of Element Declarations */
    /* TODO: Complete implementation of 3.3.6 */

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    if (isElemRef != NULL)
      *isElemRef = 0;
    /*
    * If we get a "ref" attribute on a local <element> we will assume it's
    * a reference - even if there's a "name" attribute; this seems to be more
    * robust.
    */
    nameAttr = xmlSchemaGetPropNode(node, "name");
    attr = xmlSchemaGetPropNode(node, "ref");
    if ((topLevel) || (attr == NULL)) {
      if (nameAttr == NULL) {
          xmlSchemaPMissingAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_MISSING,
            NULL, node, "name", NULL);
          return (NULL);
      }
    } else
      isRef = 1;

    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      annot = xmlSchemaParseAnnotation(ctxt, child, 1);
      child = child->next;
    }
    /*
    * Skip particle part if a global declaration.
    */
    if (topLevel)
      goto declaration_part;
    /*
    * The particle part ==================================================
    */
    min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
    max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
    xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
    particle = xmlSchemaAddParticle(ctxt, node, min, max);
    if (particle == NULL)
      goto return_null;

    /* ret->flags |= XML_SCHEMAS_ELEM_REF; */

    if (isRef) {
      const xmlChar *refNs = NULL, *ref = NULL;
      xmlSchemaQNameRefPtr refer = NULL;
      /*
      * The reference part =============================================
      */
      if (isElemRef != NULL)
          *isElemRef = 1;

      xmlSchemaPValAttrNodeQName(ctxt, schema,
          NULL, attr, &refNs, &ref);
      xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
      /*
      * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
      */
      if (nameAttr != NULL) {
          xmlSchemaPMutualExclAttrErr(ctxt,
            XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
      }
      /*
      * Check for illegal attributes.
      */
      attr = node->properties;
      while (attr != NULL) {
          if (attr->ns == NULL) {
            if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
                xmlStrEqual(attr->name, BAD_CAST "name") ||
                xmlStrEqual(attr->name, BAD_CAST "id") ||
                xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
                xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
            {
                attr = attr->next;
                continue;
            } else {
                /* SPEC (3.3.3 : 2.2) */
                xmlSchemaPCustomAttrErr(ctxt,
                  XML_SCHEMAP_SRC_ELEMENT_2_2,
                  NULL, NULL, attr,
                  "Only the attributes 'minOccurs', 'maxOccurs' and "
                  "'id' are allowed in addition to 'ref'");
                break;
            }
          } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
          attr = attr->next;
      }
      /*
      * No children except <annotation> expected.
      */
      if (child != NULL) {
          xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL, "(annotation?)");
      }
      if ((min == 0) && (max == 0))
          goto return_null;
      /*
      * Create the reference item and attach it to the particle.
      */
      refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
          ref, refNs);
      if (refer == NULL)
          goto return_null;
      particle->children = (xmlSchemaTreeItemPtr) refer;
      particle->annot = annot;
      /*
      * Add the particle to pending components, since the reference
      * need to be resolved.
      */
      WXS_ADD_PENDING(ctxt, particle);
      return ((xmlSchemaBasicItemPtr) particle);
    }
    /*
    * The declaration part ===============================================
    */
declaration_part:
    {
      const xmlChar *ns = NULL, *fixed, *name, *attrValue;
      xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;

      if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
          goto return_null;
      /*
      * Evaluate the target namespace.
      */
      if (topLevel) {
          ns = ctxt->targetNamespace;
      } else {
          attr = xmlSchemaGetPropNode(node, "form");
          if (attr != NULL) {
            attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
            if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
                ns = ctxt->targetNamespace;
            } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
                xmlSchemaPSimpleTypeErr(ctxt,
                  XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
                  NULL, (xmlNodePtr) attr,
                  NULL, "(qualified | unqualified)",
                  attrValue, NULL, NULL, NULL);
            }
          } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
            ns = ctxt->targetNamespace;
      }
      decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
      if (decl == NULL) {
          goto return_null;
      }
      /*
      * Check for illegal attributes.
      */
      attr = node->properties;
      while (attr != NULL) {
          if (attr->ns == NULL) {
            if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
            {
                if (topLevel == 0) {
                  if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
                      (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
                      (!xmlStrEqual(attr->name, BAD_CAST "form")))
                  {
                      xmlSchemaPIllegalAttrErr(ctxt,
                        XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
                  }
                } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
                  (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
                  (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {

                  xmlSchemaPIllegalAttrErr(ctxt,
                      XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
                }
            }
          } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {

            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
          attr = attr->next;
      }
      /*
      * Extract/validate attributes.
      */
      if (topLevel) {
          /*
          * Process top attributes of global element declarations here.
          */
          decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
          decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
          xmlSchemaPValAttrQName(ctxt, schema,
            NULL, node, "substitutionGroup",
            &(decl->substGroupNs), &(decl->substGroup));
          if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
            decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
          /*
          * Attribute "final".
          */
          attr = xmlSchemaGetPropNode(node, "final");
          if (attr == NULL) {
            if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
                decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
            if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
                decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
          } else {
            attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
            if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
                -1,
                XML_SCHEMAS_ELEM_FINAL_EXTENSION,
                XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
                xmlSchemaPSimpleTypeErr(ctxt,
                  XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
                  NULL, (xmlNodePtr) attr,
                  NULL, "(#all | List of (extension | restriction))",
                  attrValue, NULL, NULL, NULL);
            }
          }
      }
      /*
      * Attribute "block".
      */
      attr = xmlSchemaGetPropNode(node, "block");
      if (attr == NULL) {
          /*
          * Apply default "block" values.
          */
          if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
            decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
          if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
            decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
          if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
            decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
      } else {
          attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
          if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
            -1,
            XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
            XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
            XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
            xmlSchemaPSimpleTypeErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
                NULL, (xmlNodePtr) attr,
                NULL, "(#all | List of (extension | "
                "restriction | substitution))", attrValue,
                NULL, NULL, NULL);
          }
      }
      if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
          decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;

      attr = xmlSchemaGetPropNode(node, "type");
      if (attr != NULL) {
          xmlSchemaPValAttrNodeQName(ctxt, schema,
            NULL, attr,
            &(decl->namedTypeNs), &(decl->namedType));
          xmlSchemaCheckReference(ctxt, schema, node,
            attr, decl->namedTypeNs);
      }
      decl->value = xmlSchemaGetProp(ctxt, node, "default");
      attr = xmlSchemaGetPropNode(node, "fixed");
      if (attr != NULL) {
          fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
          if (decl->value != NULL) {
            /*
            * 3.3.3 : 1
            * default and fixed must not both be present.
            */
            xmlSchemaPMutualExclAttrErr(ctxt,
                XML_SCHEMAP_SRC_ELEMENT_1,
                NULL, attr, "default", "fixed");
          } else {
            decl->flags |= XML_SCHEMAS_ELEM_FIXED;
            decl->value = fixed;
          }
      }
      /*
      * And now for the children...
      */
      if (IS_SCHEMA(child, "complexType")) {
          /*
          * 3.3.3 : 3
          * "type" and either <simpleType> or <complexType> are mutually
          * exclusive
          */
          if (decl->namedType != NULL) {
            xmlSchemaPContentErr(ctxt,
                XML_SCHEMAP_SRC_ELEMENT_3,
                NULL, node, child,
                "The attribute 'type' and the <complexType> child are "
                "mutually exclusive", NULL);
          } else
            WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
          child = child->next;
      } else if (IS_SCHEMA(child, "simpleType")) {
          /*
          * 3.3.3 : 3
          * "type" and either <simpleType> or <complexType> are
          * mutually exclusive
          */
          if (decl->namedType != NULL) {
            xmlSchemaPContentErr(ctxt,
                XML_SCHEMAP_SRC_ELEMENT_3,
                NULL, node, child,
                "The attribute 'type' and the <simpleType> child are "
                "mutually exclusive", NULL);
          } else
            WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
          child = child->next;
      }
      while ((IS_SCHEMA(child, "unique")) ||
          (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
          if (IS_SCHEMA(child, "unique")) {
            curIDC = xmlSchemaParseIDC(ctxt, schema, child,
                XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
          } else if (IS_SCHEMA(child, "key")) {
            curIDC = xmlSchemaParseIDC(ctxt, schema, child,
                XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
          } else if (IS_SCHEMA(child, "keyref")) {
            curIDC = xmlSchemaParseIDC(ctxt, schema, child,
                XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
          }
          if (lastIDC != NULL)
            lastIDC->next = curIDC;
          else
            decl->idcs = (void *) curIDC;
          lastIDC = curIDC;
          child = child->next;
      }
      if (child != NULL) {
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child,
            NULL, "(annotation?, ((simpleType | complexType)?, "
            "(unique | key | keyref)*))");
      }
      decl->annot = annot;
    }
    /*
    * NOTE: Element Declaration Representation OK 4. will be checked at a
    * different layer.
    */
    FREE_AND_NULL(des)
    if (topLevel)
      return ((xmlSchemaBasicItemPtr) decl);
    else {
      particle->children = (xmlSchemaTreeItemPtr) decl;
      return ((xmlSchemaBasicItemPtr) particle);
    }

return_null:
    FREE_AND_NULL(des);
    if (annot != NULL) {
      if (particle != NULL)
          particle->annot = NULL;
      if (decl != NULL)
          decl->annot = NULL;
      xmlSchemaFreeAnnot(annot);
    }
    return (NULL);
}

/**
 * xmlSchemaParseUnion:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Union definition
 * *WARNING* this interface is highly subject to change
 *
 * Returns -1 in case of internal error, 0 in case of success and a positive
 * error code otherwise.
 */
static int
xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                    xmlNodePtr node)
{
    xmlSchemaTypePtr type;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    const xmlChar *cur = NULL;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (-1);
    /* Not a component, don't create it. */
    type = ctxt->ctxtType;
    /*
    * Mark the simple type as being of variety "union".
    */
    type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
    /*
    * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
    * then the ·simple ur-type definition·."
    */
    type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * Attribute "memberTypes". This is a list of QNames.
    * TODO: Check the value to contain anything.
    */
    attr = xmlSchemaGetPropNode(node, "memberTypes");
    if (attr != NULL) {
      const xmlChar *end;
      xmlChar *tmp;
      const xmlChar *localName, *nsName;
      xmlSchemaTypeLinkPtr link, lastLink = NULL;
      xmlSchemaQNameRefPtr ref;

      cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
      type->base = cur;
      do {
          while (IS_BLANK_CH(*cur))
            cur++;
          end = cur;
          while ((*end != 0) && (!(IS_BLANK_CH(*end))))
            end++;
          if (end == cur)
            break;
          tmp = xmlStrndup(cur, end - cur);
          if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
            NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
            /*
            * Create the member type link.
            */
            link = (xmlSchemaTypeLinkPtr)
                xmlMalloc(sizeof(xmlSchemaTypeLink));
            if (link == NULL) {
                xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
                  "allocating a type link", NULL);
                return (-1);
            }
            link->type = NULL;
            link->next = NULL;
            if (lastLink == NULL)
                type->memberTypes = link;
            else
                lastLink->next = link;
            lastLink = link;
            /*
            * Create a reference item.
            */
            ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
                localName, nsName);
            if (ref == NULL) {
                FREE_AND_NULL(tmp)
                return (-1);
            }
            /*
            * Assign the reference to the link, it will be resolved
            * later during fixup of the union simple type.
            */
            link->type = (xmlSchemaTypePtr) ref;
          }
          FREE_AND_NULL(tmp)
          cur = end;
      } while (*cur != 0);

    }
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      /*
      * Add the annotation to the simple type ancestor.
      */
      xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
          xmlSchemaParseAnnotation(ctxt, child, 1));
        child = child->next;
    }
    if (IS_SCHEMA(child, "simpleType")) {
      xmlSchemaTypePtr subtype, last = NULL;

      /*
      * Anchor the member types in the "subtypes" field of the
      * simple type.
      */
      while (IS_SCHEMA(child, "simpleType")) {
          subtype = (xmlSchemaTypePtr)
            xmlSchemaParseSimpleType(ctxt, schema, child, 0);
          if (subtype != NULL) {
            if (last == NULL) {
                type->subtypes = subtype;
                last = subtype;
            } else {
                last->next = subtype;
                last = subtype;
            }
            last->next = NULL;
          }
          child = child->next;
      }
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL, "(annotation?, simpleType*)");
    }
    if ((attr == NULL) && (type->subtypes == NULL)) {
       /*
      * src-union-memberTypes-or-simpleTypes
      * Either the memberTypes [attribute] of the <union> element must
      * be non-empty or there must be at least one simpleType [child].
      */
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
          NULL, node,
          "Either the attribute 'memberTypes' or "
          "at least one <simpleType> child must be present", NULL);
    }
    return (0);
}

/**
 * xmlSchemaParseList:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema List definition
 * *WARNING* this interface is highly subject to change
 *
 * Returns -1 in case of error, 0 if the declaration is improper and
 *         1 in case of success.
 */
static xmlSchemaTypePtr
xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                   xmlNodePtr node)
{
    xmlSchemaTypePtr type;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);
    /* Not a component, don't create it. */
    type = ctxt->ctxtType;
    /*
    * Mark the type as being of variety "list".
    */
    type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
    /*
    * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
    * then the ·simple ur-type definition·."
    */
    type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }

    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");

    /*
    * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
    * fields for holding the reference to the itemType.
    *
    * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
    * the "ref" fields.
    */
    xmlSchemaPValAttrQName(ctxt, schema, NULL,
      node, "itemType", &(type->baseNs), &(type->base));
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
          xmlSchemaParseAnnotation(ctxt, child, 1));
        child = child->next;
    }
    if (IS_SCHEMA(child, "simpleType")) {
      /*
      * src-list-itemType-or-simpleType
      * Either the itemType [attribute] or the <simpleType> [child] of
      * the <list> element must be present, but not both.
      */
      if (type->base != NULL) {
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
            NULL, node,
            "The attribute 'itemType' and the <simpleType> child "
            "are mutually exclusive", NULL);
      } else {
          type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
      }
        child = child->next;
    } else if (type->base == NULL) {
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
          NULL, node,
          "Either the attribute 'itemType' or the <simpleType> child "
          "must be present", NULL);
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL, "(annotation?, simpleType?)");
    }
    if ((type->base == NULL) &&
      (type->subtypes == NULL) &&
      (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
          NULL, node,
          "Either the attribute 'itemType' or the <simpleType> child "
          "must be present", NULL);
    }
    return (NULL);
}

/**
 * xmlSchemaParseSimpleType:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Simple Type definition
 * *WARNING* this interface is highly subject to change
 *
 * Returns -1 in case of error, 0 if the declaration is improper and
 * 1 in case of success.
 */
static xmlSchemaTypePtr
xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                         xmlNodePtr node, int topLevel)
{
    xmlSchemaTypePtr type, oldCtxtType;
    xmlNodePtr child = NULL;
    const xmlChar *attrValue = NULL;
    xmlAttrPtr attr;
    int hasRestriction = 0;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    if (topLevel) {
      attr = xmlSchemaGetPropNode(node, "name");
      if (attr == NULL) {
          xmlSchemaPMissingAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_MISSING,
            NULL, node,
            "name", NULL);
          return (NULL);
      } else {
          if (xmlSchemaPValAttrNode(ctxt,
            NULL, attr,
            xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
            return (NULL);
          /*
          * Skip built-in types.
          */
          if (ctxt->isS4S) {
            xmlSchemaTypePtr biType;

            if (ctxt->isRedefine) {
                /*
                * REDEFINE: Disallow redefinition of built-in-types.
                * TODO: It seems that the spec does not say anything
                * about this case.
                */
                xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
                  NULL, node,
                  "Redefinition of built-in simple types is not "
                  "supported", NULL);
                return(NULL);
            }
            biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
            if (biType != NULL)
                return (biType);
          }
      }
    }
    /*
    * TargetNamespace:
    * SPEC "The ·actual value· of the targetNamespace [attribute]
    * of the <schema> ancestor element information item if present,
    * otherwise ·absent·.
    */
    if (topLevel == 0) {
#ifdef ENABLE_NAMED_LOCALS
        char buf[40];
#endif
      /*
      * Parse as local simple type definition.
      */
#ifdef ENABLE_NAMED_LOCALS
        snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
      type = xmlSchemaAddType(ctxt, schema,
          XML_SCHEMA_TYPE_SIMPLE,
          xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
          ctxt->targetNamespace, node, 0);
#else
      type = xmlSchemaAddType(ctxt, schema,
          XML_SCHEMA_TYPE_SIMPLE,
          NULL, ctxt->targetNamespace, node, 0);
#endif
      if (type == NULL)
          return (NULL);
      type->type = XML_SCHEMA_TYPE_SIMPLE;
      type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
      /*
      * Check for illegal attributes.
      */
      attr = node->properties;
      while (attr != NULL) {
          if (attr->ns == NULL) {
            if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
                xmlSchemaPIllegalAttrErr(ctxt,
                  XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr); 
            }
          } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
                xmlSchemaPIllegalAttrErr(ctxt,
                  XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
          attr = attr->next;
      }
    } else {
      /*
      * Parse as global simple type definition.
      *
      * Note that attrValue is the value of the attribute "name" here.
      */
      type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
          attrValue, ctxt->targetNamespace, node, 1);
      if (type == NULL)
          return (NULL);
      type->type = XML_SCHEMA_TYPE_SIMPLE;
      type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
      type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
      /*
      * Check for illegal attributes.
      */
      attr = node->properties;
      while (attr != NULL) {
          if (attr->ns == NULL) {
            if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
                xmlSchemaPIllegalAttrErr(ctxt,
                  XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
            }
          } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
          attr = attr->next;
      }
      /*
      * Attribute "final".
      */
      attr = xmlSchemaGetPropNode(node, "final");
      if (attr == NULL) {
          if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
            type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
          if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
            type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
          if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
            type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
      } else {
          attrValue = xmlSchemaGetProp(ctxt, node, "final");
          if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
            -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
            XML_SCHEMAS_TYPE_FINAL_LIST,
            XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {

            xmlSchemaPSimpleTypeErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
                WXS_BASIC_CAST type, (xmlNodePtr) attr,
                NULL, "(#all | List of (list | union | restriction)",
                attrValue, NULL, NULL, NULL);
          }
      }
    }
    type->targetNamespace = ctxt->targetNamespace;
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * And now for the children...
    */
    oldCtxtType = ctxt->ctxtType;
    
    ctxt->ctxtType = type;
    
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
        child = child->next;
    }
    if (child == NULL) {
      xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
          NULL, node, child, NULL,
          "(annotation?, (restriction | list | union))");
    } else if (IS_SCHEMA(child, "restriction")) {     
        xmlSchemaParseRestriction(ctxt, schema, child,
          XML_SCHEMA_TYPE_SIMPLE);  
      hasRestriction = 1;     
        child = child->next;
    } else if (IS_SCHEMA(child, "list")) {
        xmlSchemaParseList(ctxt, schema, child);
        child = child->next;
    } else if (IS_SCHEMA(child, "union")) {
        xmlSchemaParseUnion(ctxt, schema, child);
        child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL,
          "(annotation?, (restriction | list | union))");
    }
    /*
    * REDEFINE: SPEC src-redefine (5)
    * "Within the [children], each <simpleType> must have a
    * <restriction> among its [children] ... the ·actual value· of whose
    * base [attribute] must be the same as the ·actual value· of its own
    * name attribute plus target namespace;"
    */
    if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
      xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
          NULL, node, "This is a redefinition, thus the "
          "<simpleType> must have a <restriction> child", NULL);
    }
    
    ctxt->ctxtType = oldCtxtType;
    return (type);
}

/**
 * xmlSchemaParseModelGroupDefRef:
 * @ctxt:  the parser context
 * @schema: the schema being built
 * @node:  the node
 *
 * Parses a reference to a model group definition.
 *
 * We will return a particle component with a qname-component or
 * NULL in case of an error.
 */
static xmlSchemaTreeItemPtr
xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
                         xmlSchemaPtr schema,
                         xmlNodePtr node)
{
    xmlSchemaParticlePtr item;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    const xmlChar *ref = NULL, *refNs = NULL;
    int min, max;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    attr = xmlSchemaGetPropNode(node, "ref");
    if (attr == NULL) {
      xmlSchemaPMissingAttrErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node, "ref", NULL);
      return (NULL);
    } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
      attr, &refNs, &ref) != 0) {
      return (NULL);
    }
    xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
    min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
    max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
      "(xs:nonNegativeInteger | unbounded)");
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    item = xmlSchemaAddParticle(ctxt, node, min, max);
    if (item == NULL)
      return (NULL);    
    /*
    * Create a qname-reference and set as the term; it will be substituted
    * for the model group after the reference has been resolved.
    */
    item->children = (xmlSchemaTreeItemPtr)
      xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);    
    xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
    /*
    * And now for the children...
    */
    child = node->children;
    /* TODO: Is annotation even allowed for a model group reference? */
    if (IS_SCHEMA(child, "annotation")) {
      /*
      * TODO: What to do exactly with the annotation?
      */
      item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
      child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL,
          "(annotation?)");
    }
    /*
    * Corresponds to no component at all if minOccurs==maxOccurs==0.
    */
    if ((min == 0) && (max == 0))
      return (NULL);

    return ((xmlSchemaTreeItemPtr) item);
}

/**
 * xmlSchemaParseModelGroupDefinition:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * Parses a XML schema model group definition.
 *
 * Note that the contraint src-redefine (6.2) can't be applied until
 * references have been resolved. So we will do this at the
 * component fixup level.
 *    
 * *WARNING* this interface is highly subject to change
 *
 * Returns -1 in case of error, 0 if the declaration is improper and
 *         1 in case of success.
 */
static xmlSchemaModelGroupDefPtr
xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
                           xmlSchemaPtr schema,
                           xmlNodePtr node)
{
    xmlSchemaModelGroupDefPtr item;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    const xmlChar *name;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    attr = xmlSchemaGetPropNode(node, "name");
    if (attr == NULL) {
      xmlSchemaPMissingAttrErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node,
          "name", NULL);
      return (NULL);
    } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
      xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
      return (NULL);
    }
    item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
      ctxt->targetNamespace, node);
    if (item == NULL)
      return (NULL);
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
      child = child->next;
    }
    if (IS_SCHEMA(child, "all")) {
      item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
          XML_SCHEMA_TYPE_ALL, 0);
      child = child->next;
    } else if (IS_SCHEMA(child, "choice")) {
      item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
          XML_SCHEMA_TYPE_CHOICE, 0);
      child = child->next;
    } else if (IS_SCHEMA(child, "sequence")) {
      item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
          XML_SCHEMA_TYPE_SEQUENCE, 0);
      child = child->next;
    }

   

    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL,
          "(annotation?, (all | choice | sequence)?)");
    }
    return (item);
}

/**
 * xmlSchemaCleanupDoc:
 * @ctxt:  a schema validation context
 * @node:  the root of the document.
 *
 * removes unwanted nodes in a schemas document tree
 */
static void
xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
{
    xmlNodePtr delete, cur;

    if ((ctxt == NULL) || (root == NULL)) return;

    /*
     * Remove all the blank text nodes
     */
    delete = NULL;
    cur = root;
    while (cur != NULL) {
        if (delete != NULL) {
            xmlUnlinkNode(delete);
            xmlFreeNode(delete);
            delete = NULL;
        }
        if (cur->type == XML_TEXT_NODE) {
            if (IS_BLANK_NODE(cur)) {
                if (xmlNodeGetSpacePreserve(cur) != 1) {
                    delete = cur;
                }
            }
        } else if ((cur->type != XML_ELEMENT_NODE) &&
                   (cur->type != XML_CDATA_SECTION_NODE)) {
            delete = cur;
            goto skip_children;
        }

        /*
         * Skip to next node
         */
        if (cur->children != NULL) {
            if ((cur->children->type != XML_ENTITY_DECL) &&
                (cur->children->type != XML_ENTITY_REF_NODE) &&
                (cur->children->type != XML_ENTITY_NODE)) {
                cur = cur->children;
                continue;
            }
        }
      skip_children:
        if (cur->next != NULL) {
            cur = cur->next;
            continue;
        }

        do {
            cur = cur->parent;
            if (cur == NULL)
                break;
            if (cur == root) {
                cur = NULL;
                break;
            }
            if (cur->next != NULL) {
                cur = cur->next;
                break;
            }
        } while (cur != NULL);
    }
    if (delete != NULL) {
        xmlUnlinkNode(delete);
        xmlFreeNode(delete);
        delete = NULL;
    }
}


static void
xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
{
    if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
      schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;

    if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
      schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;

    if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
      schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
    if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
      schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
    if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
      schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
    if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
      schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;

    if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
      schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
    if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
      schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
    if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
      schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
}

static int
xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
                       xmlSchemaPtr schema,
                       xmlNodePtr node)
{
    xmlAttrPtr attr;
    const xmlChar *val;
    int res = 0, oldErrs = ctxt->nberrors;

    /*
    * Those flags should be moved to the parser context flags,
    * since they are not visible at the component level. I.e.
    * they are used if processing schema *documents* only.
    */
    res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    HFAILURE;

    /*
    * Since the version is of type xs:token, we won't bother to
    * check it.
    */
    /* REMOVED:
    attr = xmlSchemaGetPropNode(node, "version");
    if (attr != NULL) {
      res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);    
      HFAILURE;
    }
    */
    attr = xmlSchemaGetPropNode(node, "targetNamespace");
    if (attr != NULL) {
      res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
      HFAILURE;
      if (res != 0) {
          ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
          goto exit;
      }
    }
    attr = xmlSchemaGetPropNode(node, "elementFormDefault");
    if (attr != NULL) {
      val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
      res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
          XML_SCHEMAS_QUALIF_ELEM);
      HFAILURE;
      if (res != 0) {
          xmlSchemaPSimpleTypeErr(ctxt,
            XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
            NULL, (xmlNodePtr) attr, NULL,
            "(qualified | unqualified)", val, NULL, NULL, NULL);
      }
    }
    attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
    if (attr != NULL) {
      val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
      res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
          XML_SCHEMAS_QUALIF_ATTR);
      HFAILURE;
      if (res != 0) {
          xmlSchemaPSimpleTypeErr(ctxt,
            XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
            NULL, (xmlNodePtr) attr, NULL,
            "(qualified | unqualified)", val, NULL, NULL, NULL);
      }
    }
    attr = xmlSchemaGetPropNode(node, "finalDefault");
    if (attr != NULL) {
      val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
      res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
          XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
          XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
          -1,
          XML_SCHEMAS_FINAL_DEFAULT_LIST,
          XML_SCHEMAS_FINAL_DEFAULT_UNION);
      HFAILURE;
      if (res != 0) {
          xmlSchemaPSimpleTypeErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
            NULL, (xmlNodePtr) attr, NULL,
            "(#all | List of (extension | restriction | list | union))",
            val, NULL, NULL, NULL);
      }
    }
    attr = xmlSchemaGetPropNode(node, "blockDefault");
    if (attr != NULL) {
      val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);     
      res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
          XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
          XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
          XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
      HFAILURE;
      if (res != 0) {
          xmlSchemaPSimpleTypeErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
            NULL, (xmlNodePtr) attr, NULL,
            "(#all | List of (extension | restriction | substitution))",
            val, NULL, NULL, NULL);
      }
    }

exit:
    if (oldErrs != ctxt->nberrors)
      res = ctxt->err;
    return(res);
exit_failure:
    return(-1);
}

/**
 * xmlSchemaParseSchemaTopLevel:
 * @ctxt:  a schema validation context
 * @schema:  the schemas
 * @nodes:  the list of top level nodes
 *
 * Returns the internal XML Schema structure built from the resource or
 *         NULL in case of error
 */
static int
xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
                             xmlSchemaPtr schema, xmlNodePtr nodes)
{
    xmlNodePtr child;
    xmlSchemaAnnotPtr annot;
    int res = 0, oldErrs, tmpOldErrs;

    if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
        return(-1);

    oldErrs = ctxt->nberrors;
    child = nodes;
    while ((IS_SCHEMA(child, "include")) ||
         (IS_SCHEMA(child, "import")) ||
         (IS_SCHEMA(child, "redefine")) ||
         (IS_SCHEMA(child, "annotation"))) {
      if (IS_SCHEMA(child, "annotation")) {
          annot = xmlSchemaParseAnnotation(ctxt, child, 1);
          if (schema->annot == NULL)
            schema->annot = annot;
          else
            xmlSchemaFreeAnnot(annot);
      } else if (IS_SCHEMA(child, "import")) {
          tmpOldErrs = ctxt->nberrors;
          res = xmlSchemaParseImport(ctxt, schema, child);
          HFAILURE;
          HSTOP(ctxt);
          if (tmpOldErrs != ctxt->nberrors)
            goto exit;      
      } else if (IS_SCHEMA(child, "include")) {
          tmpOldErrs = ctxt->nberrors;
          res = xmlSchemaParseInclude(ctxt, schema, child);
          HFAILURE;
          HSTOP(ctxt);
          if (tmpOldErrs != ctxt->nberrors)
            goto exit;      
      } else if (IS_SCHEMA(child, "redefine")) {
          tmpOldErrs = ctxt->nberrors;
          res = xmlSchemaParseRedefine(ctxt, schema, child);
          HFAILURE;
          HSTOP(ctxt);
          if (tmpOldErrs != ctxt->nberrors)
            goto exit;
      }
      child = child->next;
    }
    /*
    * URGENT TODO: Change the functions to return int results.
    * We need especially to catch internal errors.
    */
    while (child != NULL) {
      if (IS_SCHEMA(child, "complexType")) {
          xmlSchemaParseComplexType(ctxt, schema, child, 1);
          child = child->next;
      } else if (IS_SCHEMA(child, "simpleType")) {
          xmlSchemaParseSimpleType(ctxt, schema, child, 1);
          child = child->next;
      } else if (IS_SCHEMA(child, "element")) {
          xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
          child = child->next;
      } else if (IS_SCHEMA(child, "attribute")) {
          xmlSchemaParseGlobalAttribute(ctxt, schema, child);
          child = child->next;
      } else if (IS_SCHEMA(child, "attributeGroup")) {
          xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
          child = child->next;
      } else if (IS_SCHEMA(child, "group")) {
          xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
          child = child->next;
      } else if (IS_SCHEMA(child, "notation")) {
          xmlSchemaParseNotation(ctxt, schema, child);
          child = child->next;
      } else {
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, child->parent, child,
            NULL, "((include | import | redefine | annotation)*, "
            "(((simpleType | complexType | group | attributeGroup) "
            "| element | attribute | notation), annotation*)*)");
          child = child->next;
      }
      while (IS_SCHEMA(child, "annotation")) {
          /*
          * TODO: We should add all annotations.
          */
          annot = xmlSchemaParseAnnotation(ctxt, child, 1);
          if (schema->annot == NULL)
            schema->annot = annot;
          else
            xmlSchemaFreeAnnot(annot);
          child = child->next;
      }
    }
exit:
    ctxt->ctxtType = NULL;
    if (oldErrs != ctxt->nberrors)
      res = ctxt->err;
    return(res);
exit_failure:
    return(-1);
}

static xmlSchemaSchemaRelationPtr
xmlSchemaSchemaRelationCreate(void)
{
    xmlSchemaSchemaRelationPtr ret;

    ret = (xmlSchemaSchemaRelationPtr)
      xmlMalloc(sizeof(xmlSchemaSchemaRelation));
    if (ret == NULL) {
      xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
      return(NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
    return(ret);
}

#if 0
static void
xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
{
    xmlFree(rel);
}
#endif

static void
xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
{
    xmlSchemaRedefPtr prev;

    while (redef != NULL) {
      prev = redef;
      redef = redef->next;
      xmlFree(prev);
    }
}

static void
xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
{
    /*
    * After the construction context has been freed, there will be
    * no schema graph available any more. Only the schema buckets
    * will stay alive, which are put into the "schemasImports" and
    * "includes" slots of the xmlSchema.
    */
    if (con->buckets != NULL)
      xmlSchemaItemListFree(con->buckets);
    if (con->pending != NULL)
      xmlSchemaItemListFree(con->pending);
    if (con->substGroups != NULL)
      xmlHashFree(con->substGroups,
          (xmlHashDeallocator) xmlSchemaSubstGroupFree);
    if (con->redefs != NULL)
      xmlSchemaRedefListFree(con->redefs);
    if (con->dict != NULL)
      xmlDictFree(con->dict);
    xmlFree(con);
}

static xmlSchemaConstructionCtxtPtr 
xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
{
    xmlSchemaConstructionCtxtPtr ret;

    ret = (xmlSchemaConstructionCtxtPtr)
      xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
    if (ret == NULL) {
        xmlSchemaPErrMemory(NULL,
          "allocating schema construction context", NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));

    ret->buckets = xmlSchemaItemListCreate();
    if (ret->buckets == NULL) {
      xmlSchemaPErrMemory(NULL,
          "allocating list of schema buckets", NULL);
      xmlFree(ret);
        return (NULL);
    }
    ret->pending = xmlSchemaItemListCreate();
    if (ret->pending == NULL) {
      xmlSchemaPErrMemory(NULL,
          "allocating list of pending global components", NULL);
      xmlSchemaConstructionCtxtFree(ret);
        return (NULL);
    }
    ret->dict = dict;
    xmlDictReference(dict);
    return(ret);
}

static xmlSchemaParserCtxtPtr
xmlSchemaParserCtxtCreate(void)
{
    xmlSchemaParserCtxtPtr ret;

    ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
    if (ret == NULL) {
        xmlSchemaPErrMemory(NULL, "allocating schema parser context",
                            NULL);
        return (NULL);
    }
    memset(ret, 0, sizeof(xmlSchemaParserCtxt));
    ret->type = XML_SCHEMA_CTXT_PARSER;
    ret->attrProhibs = xmlSchemaItemListCreate();
    if (ret->attrProhibs == NULL) {
      xmlFree(ret);
      return(NULL);
    }
    return(ret);
}

/**
 * xmlSchemaNewParserCtxtUseDict:
 * @URL:  the location of the schema
 * @dict: the dictionary to be used
 *
 * Create an XML Schemas parse context for that file/resource expected
 * to contain an XML Schemas file.
 *
 * Returns the parser context or NULL in case of error
 */
static xmlSchemaParserCtxtPtr
xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
{
    xmlSchemaParserCtxtPtr ret;

    ret = xmlSchemaParserCtxtCreate();
    if (ret == NULL)        
        return (NULL);
    ret->dict = dict;
    xmlDictReference(dict);    
    if (URL != NULL)
      ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
    return (ret);
}

static int
xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
{
    if (vctxt->pctxt == NULL) {
        if (vctxt->schema != NULL)
          vctxt->pctxt =
            xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
      else
          vctxt->pctxt = xmlSchemaNewParserCtxt("*");
      if (vctxt->pctxt == NULL) {
          VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
            "failed to create a temp. parser context");
          return (-1);
      }
      /* TODO: Pass user data. */
      xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
          vctxt->warning, vctxt->errCtxt);
      xmlSchemaSetParserStructuredErrors(vctxt->pctxt, vctxt->serror,
          vctxt->errCtxt);
    }
    return (0);
}

/**
 * xmlSchemaGetSchemaBucket:
 * @pctxt: the schema parser context
 * @schemaLocation: the URI of the schema document
 *
 * Returns a schema bucket if it was already parsed.
 *
 * Returns a schema bucket if it was already parsed from
 *         @schemaLocation, NULL otherwise.
 */
static xmlSchemaBucketPtr
xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
                      const xmlChar *schemaLocation)
{
    xmlSchemaBucketPtr cur;
    xmlSchemaItemListPtr list;

    list = pctxt->constructor->buckets;
    if (list->nbItems == 0)
      return(NULL);
    else {
      int i;
      for (i = 0; i < list->nbItems; i++) {
          cur = (xmlSchemaBucketPtr) list->items[i];
          /* Pointer comparison! */
          if (cur->schemaLocation == schemaLocation)
            return(cur);
      }
    }
    return(NULL);
}

static xmlSchemaBucketPtr
xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
                             const xmlChar *schemaLocation,
                             const xmlChar *targetNamespace)
{
    xmlSchemaBucketPtr cur;
    xmlSchemaItemListPtr list;

    list = pctxt->constructor->buckets;
    if (list->nbItems == 0)
      return(NULL);
    else {
      int i;
      for (i = 0; i < list->nbItems; i++) {
          cur = (xmlSchemaBucketPtr) list->items[i];
          /* Pointer comparison! */
          if ((cur->origTargetNamespace == NULL) &&
            (cur->schemaLocation == schemaLocation) &&
            (cur->targetNamespace == targetNamespace))
            return(cur);
      }
    }
    return(NULL);
}


#define IS_BAD_SCHEMA_DOC(b) \
    (((b)->doc == NULL) && ((b)->schemaLocation != NULL))

static xmlSchemaBucketPtr
xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
                         const xmlChar *targetNamespace,
                         int imported)
{
    xmlSchemaBucketPtr cur;
    xmlSchemaItemListPtr list;

    list = pctxt->constructor->buckets;
    if (list->nbItems == 0)
      return(NULL);
    else {
      int i;
      for (i = 0; i < list->nbItems; i++) {
          cur = (xmlSchemaBucketPtr) list->items[i];
          if ((! IS_BAD_SCHEMA_DOC(cur)) &&
            (cur->origTargetNamespace == targetNamespace) &&
            ((imported && cur->imported) ||
             ((!imported) && (!cur->imported))))
            return(cur);
      }
    }
    return(NULL);
}

static int
xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
                 xmlSchemaPtr schema,
                 xmlSchemaBucketPtr bucket)
{
    int oldFlags;
    xmlDocPtr oldDoc;
    xmlNodePtr node;
    int ret, oldErrs;
    xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
    
    /* 
    * Save old values; reset the *main* schema.
    * URGENT TODO: This is not good; move the per-document information
    * to the parser. Get rid of passing the main schema to the
    * parsing functions.
    */
    oldFlags = schema->flags;
    oldDoc = schema->doc;
    if (schema->flags != 0)
      xmlSchemaClearSchemaDefaults(schema);
    schema->doc = bucket->doc;    
    pctxt->schema = schema;
    /* 
    * Keep the current target namespace on the parser *not* on the
    * main schema.
    */
    pctxt->targetNamespace = bucket->targetNamespace;
    WXS_CONSTRUCTOR(pctxt)->bucket = bucket;

    if ((bucket->targetNamespace != NULL) &&
      xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
      /*
      * We are parsing the schema for schemas!
      */
      pctxt->isS4S = 1;
    }    
    /* Mark it as parsed, even if parsing fails. */
    bucket->parsed++;
    /* Compile the schema doc. */
    node = xmlDocGetRootElement(bucket->doc);
    ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
    if (ret != 0)
      goto exit;
    /* An empty schema; just get out. */
    if (node->children == NULL)
      goto exit;
    oldErrs = pctxt->nberrors;
    ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
    if (ret != 0)
      goto exit;
    /*
    * TODO: Not nice, but I'm not 100% sure we will get always an error
    * as a result of the obove functions; so better rely on pctxt->err
    * as well.
    */
    if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
      ret = pctxt->err;
      goto exit;
    }
    
exit:
    WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
    /* Restore schema values. */
    schema->doc = oldDoc;
    schema->flags = oldFlags;
    return(ret);
}

static int
xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
                 xmlSchemaPtr schema,
                 xmlSchemaBucketPtr bucket)
{
    xmlSchemaParserCtxtPtr newpctxt;
    int res = 0;

    if (bucket == NULL)
      return(0);
    if (bucket->parsed) {
      PERROR_INT("xmlSchemaParseNewDoc",
          "reparsing a schema doc");
      return(-1);
    }
    if (bucket->doc == NULL) {
      PERROR_INT("xmlSchemaParseNewDoc",
          "parsing a schema doc, but there's no doc");
      return(-1);
    }
    if (pctxt->constructor == NULL) {
      PERROR_INT("xmlSchemaParseNewDoc",
          "no constructor");
      return(-1);
    }    
    /* Create and init the temporary parser context. */
    newpctxt = xmlSchemaNewParserCtxtUseDict(
      (const char *) bucket->schemaLocation, pctxt->dict);
    if (newpctxt == NULL)
      return(-1);
    newpctxt->constructor = pctxt->constructor;
    /*
    * TODO: Can we avoid that the parser knows about the main schema? 
    * It would be better if he knows about the current schema bucket
    * only.
    */
    newpctxt->schema = schema;
    xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
      pctxt->errCtxt);
    xmlSchemaSetParserStructuredErrors(newpctxt, pctxt->serror,
      pctxt->errCtxt);
    newpctxt->counter = pctxt->counter;
    

    res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
    
    /* Channel back errors and cleanup the temporary parser context. */
    if (res != 0)
      pctxt->err = res;
    pctxt->nberrors += newpctxt->nberrors;
    pctxt->counter = newpctxt->counter;
    newpctxt->constructor = NULL;    
    /* Free the parser context. */
    xmlSchemaFreeParserCtxt(newpctxt);
    return(res);
}

static void
xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
                        xmlSchemaSchemaRelationPtr rel)
{
    xmlSchemaSchemaRelationPtr cur = bucket->relations;

    if (cur == NULL) {
      bucket->relations = rel;
      return;
    }
    while (cur->next != NULL)
      cur = cur->next;
    cur->next = rel;
}


static const xmlChar *
xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
                    xmlNodePtr ctxtNode)
{    
    /*
    * Build an absolue location URI.
    */
    if (location != NULL) {   
      if (ctxtNode == NULL)
          return(location);
      else {
          xmlChar *base, *URI;
          const xmlChar *ret = NULL;

          base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
          if (base == NULL) {
            URI = xmlBuildURI(location, ctxtNode->doc->URL);
          } else {
            URI = xmlBuildURI(location, base);
            xmlFree(base);
          }
          if (URI != NULL) {
            ret = xmlDictLookup(dict, URI, -1);
            xmlFree(URI);
            return(ret);
          }
      }
    }
    return(NULL);
}
    


/**
 * xmlSchemaAddSchemaDoc:
 * @pctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * Parse an included (and to-be-redefined) XML schema document.
 *
 * Returns 0 on success, a positive error code on errors and
 *         -1 in case of an internal or API error.
 */

static int
xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
            int type, /* import or include or redefine */
            const xmlChar *schemaLocation,
            xmlDocPtr schemaDoc,
            const char *schemaBuffer,
            int schemaBufferLen,
            xmlNodePtr invokingNode,
            const xmlChar *sourceTargetNamespace,           
            const xmlChar *importNamespace,     
            xmlSchemaBucketPtr *bucket)
{
    const xmlChar *targetNamespace = NULL;
    xmlSchemaSchemaRelationPtr relation = NULL;
    xmlDocPtr doc = NULL;
    int res = 0, err = 0, located = 0, preserveDoc = 0;
    xmlSchemaBucketPtr bkt = NULL;

    if (bucket != NULL)
      *bucket = NULL;
    
    switch (type) {
      case XML_SCHEMA_SCHEMA_IMPORT:
      case XML_SCHEMA_SCHEMA_MAIN:
          err = XML_SCHEMAP_SRC_IMPORT;
          break;
      case XML_SCHEMA_SCHEMA_INCLUDE:
          err = XML_SCHEMAP_SRC_INCLUDE;
          break;
      case XML_SCHEMA_SCHEMA_REDEFINE:
          err = XML_SCHEMAP_SRC_REDEFINE;
          break;
    }    
       

    /* Special handling for the main schema:
    * skip the location and relation logic and just parse the doc.
    * We need just a bucket to be returned in this case.
    */    
    if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
      goto doc_load;    

    /* Note that we expect the location to be an absulute URI. */ 
    if (schemaLocation != NULL) {
      bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
      if ((bkt != NULL) &&
          (pctxt->constructor->bucket == bkt)) {
          /* Report self-imports/inclusions/redefinitions. */
          
          xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
            invokingNode, NULL,
            "The schema must not import/include/redefine itself",
            NULL, NULL);
          goto exit;
      }
    }
    /*
    * Create a relation for the graph of schemas.
    */
    relation = xmlSchemaSchemaRelationCreate();
    if (relation == NULL)
      return(-1);    
    xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
      relation);
    relation->type = type;

    /*
    * Save the namespace import information.
    */
    if (WXS_IS_BUCKET_IMPMAIN(type)) {
      relation->importNamespace = importNamespace;    
      if (schemaLocation == NULL) {
          /*
          * No location; this is just an import of the namespace.
          * Note that we don't assign a bucket to the relation
          * in this case.
          */
          goto exit;
      }
      targetNamespace = importNamespace;
    }

    /* Did we already fetch the doc? */
    if (bkt != NULL) {        
      /* TODO: The following nasty cases will produce an error. */
      if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
          /* We included/redefined and then try to import a schema. */
          if (schemaLocation == NULL)
            schemaLocation = BAD_CAST "in_memory_buffer";
          xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
            invokingNode, NULL,
            "The schema document '%s' cannot be imported, since "
            "it was already included or redefined",
            schemaLocation, NULL);
          goto exit;
      } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
          /* We imported and then try to include/redefine a schema. */
          if (schemaLocation == NULL)
            schemaLocation = BAD_CAST "in_memory_buffer";
          xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
            invokingNode, NULL,
            "The schema document '%s' cannot be included or "
            "redefined, since it was already imported",
            schemaLocation, NULL);
          goto exit;
      }     
    }
      
    if (WXS_IS_BUCKET_IMPMAIN(type)) {
      /*
      * Given that the schemaLocation [attribute] is only a hint, it is open
      * to applications to ignore all but the first <import> for a given
      * namespace, regardless of the ·actual value· of schemaLocation, but
      * such a strategy risks missing useful information when new
      * schemaLocations are offered.
      *
      * We will use the first <import> that comes with a location.
      * Further <import>s *with* a location, will result in an error.
      * TODO: Better would be to just report a warning here, but
      * we'll try it this way until someone complains.
      *
      * Schema Document Location Strategy:
      * 3 Based on the namespace name, identify an existing schema document,
      * either as a resource which is an XML document or a <schema> element
      * information item, in some local schema repository;
      * 5 Attempt to resolve the namespace name to locate such a resource.
      *
      * NOTE: (3) and (5) are not supported.
      */    
      if (bkt != NULL) {
          relation->bucket = bkt;
          goto exit;
      }
      bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
          importNamespace, 1);

      if (bkt != NULL) {          
          relation->bucket = bkt;
          if (bkt->schemaLocation == NULL) {
            /* First given location of the schema; load the doc. */
            bkt->schemaLocation = schemaLocation;
          } else {
            if (!xmlStrEqual(schemaLocation,
                bkt->schemaLocation)) {
                /*
                * Additional location given; just skip it.
                * URGENT TODO: We should report a warning here.
                * res = XML_SCHEMAP_SRC_IMPORT;
                */
                if (schemaLocation == NULL)
                  schemaLocation = BAD_CAST "in_memory_buffer";

                xmlSchemaCustomWarning(ACTXT_CAST pctxt,
                  XML_SCHEMAP_WARN_SKIP_SCHEMA,
                  invokingNode, NULL,
                  "Skipping import of schema located at '%s' for the "
                  "namespace '%s', since this namespace was already "
                  "imported with the schema located at '%s'",
                  schemaLocation, importNamespace, bkt->schemaLocation);
            }
            goto exit;
          }
      }     
      /* 
      * No bucket + first location: load the doc and create a
      * bucket.
      */
    } else {
      /* <include> and <redefine> */
      if (bkt != NULL) {
                
          if ((bkt->origTargetNamespace == NULL) &&
            (bkt->targetNamespace != sourceTargetNamespace)) {
            xmlSchemaBucketPtr chamel;
            
            /*
            * Chameleon include/redefine: skip loading only if it was
            * aleady build for the targetNamespace of the including
            * schema.
            */
            /*
            * URGENT TODO: If the schema is a chameleon-include then copy
            * the components into the including schema and modify the
            * targetNamespace of those components, do nothing otherwise.
            * NOTE: This is currently worked-around by compiling the
            * chameleon for every destinct including targetNamespace; thus
            * not performant at the moment.
            * TODO: Check when the namespace in wildcards for chameleons
            * needs to be converted: before we built wildcard intersections
            * or after.
            *   Answer: after!
            */
            chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
                schemaLocation, sourceTargetNamespace);
            if (chamel != NULL) {
                /* A fitting chameleon was already parsed; NOP. */
                relation->bucket = chamel;
                goto exit;
            }
            /* 
            * We need to parse the chameleon again for a different
            * targetNamespace.
            * CHAMELEON TODO: Optimize this by only parsing the
            * chameleon once, and then copying the components to
            * the new targetNamespace.
            */
            bkt = NULL;
          } else {
            relation->bucket = bkt;
            goto exit;
          }     
      }
    }
    if ((bkt != NULL) && (bkt->doc != NULL)) {
      PERROR_INT("xmlSchemaAddSchemaDoc",
          "trying to load a schema doc, but a doc is already "
          "assigned to the schema bucket");
      goto exit_failure;
    }

doc_load:
    /*
    * Load the document.
    */
    if (schemaDoc != NULL) {
      doc = schemaDoc;
      /* Don' free this one, since it was provided by the caller. */
      preserveDoc = 1;
      /* TODO: Does the context or the doc hold the location? */
      if (schemaDoc->URL != NULL)
          schemaLocation = xmlDictLookup(pctxt->dict,
            schemaDoc->URL, -1);
        else
          schemaLocation = BAD_CAST "in_memory_buffer";
    } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
      xmlParserCtxtPtr parserCtxt;

      parserCtxt = xmlNewParserCtxt();
      if (parserCtxt == NULL) {
          xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
            "allocating a parser context", NULL);
          goto exit_failure;
      }
      if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
          /*
          * TODO: Do we have to burden the schema parser dict with all
          * the content of the schema doc?
          */
          xmlDictFree(parserCtxt->dict);
          parserCtxt->dict = pctxt->dict;
          xmlDictReference(parserCtxt->dict);
      }
      if (schemaLocation != NULL) {
          /* Parse from file. */
          doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
            NULL, SCHEMAS_PARSE_OPTIONS);
      } else if (schemaBuffer != NULL) {
          /* Parse from memory buffer. */
          doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
            NULL, NULL, SCHEMAS_PARSE_OPTIONS);
          schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
          if (doc != NULL)
            doc->URL = schemaLocation;        
      }
      /*
      * For <import>:
      * 2.1 The referent is (a fragment of) a resource which is an
      * XML document (see clause 1.1), which in turn corresponds to
      * a <schema> element information item in a well-formed information
      * set, which in turn corresponds to a valid schema.
      * TODO: (2.1) fragments of XML documents are not supported.
      *
      * 2.2 The referent is a <schema> element information item in
      * a well-formed information set, which in turn corresponds
      * to a valid schema.
      * TODO: (2.2) is not supported.
      */
      if (doc == NULL) {
          xmlErrorPtr lerr;
          lerr = xmlGetLastError();
          /*
          * Check if this a parser error, or if the document could
          * just not be located.
          * TODO: Try to find specific error codes to react only on
          * localisation failures.
          */
          if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
            /*
            * We assume a parser error here.
            */
            located = 1;
            /* TODO: Error code ?? */
            res = XML_SCHEMAP_SRC_IMPORT_2_1;
            xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
                invokingNode, NULL,
                "Failed to parse the XML resource '%s'",
                schemaLocation, NULL);
          }
      }
      xmlFreeParserCtxt(parserCtxt);
      if ((doc == NULL) && located)
          goto exit_error;
    } else {
      xmlSchemaPErr(pctxt, NULL,
          XML_SCHEMAP_NOTHING_TO_PARSE,
          "No information for parsing was provided with the "
          "given schema parser context.\n",
          NULL, NULL);
      goto exit_failure;
    }
    /*
    * Preprocess the document.
    */
    if (doc != NULL) {
      xmlNodePtr docElem = NULL;

      located = 1;      
      docElem = xmlDocGetRootElement(doc);
      if (docElem == NULL) {
          xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
            invokingNode, NULL, 
            "The document '%s' has no document element",
            schemaLocation, NULL);
          goto exit_error;
      }
      /*
      * Remove all the blank text nodes.
      */
      xmlSchemaCleanupDoc(pctxt, docElem);
      /*
      * Check the schema's top level element.
      */
      if (!IS_SCHEMA(docElem, "schema")) {
          xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
            invokingNode, NULL,
            "The XML document '%s' is not a schema document",
            schemaLocation, NULL);
          goto exit_error;
      }
      /* 
      * Note that we don't apply a type check for the
      * targetNamespace value here.
      */
      targetNamespace = xmlSchemaGetProp(pctxt, docElem,
          "targetNamespace");
    }
    
/* after_doc_loading: */
    if ((bkt == NULL) && located) {
      /* Only create a bucket if the schema was located. */
        bkt = xmlSchemaBucketCreate(pctxt, type,
          targetNamespace);
      if (bkt == NULL)
          goto exit_failure;
    }
    if (bkt != NULL) {
      bkt->schemaLocation = schemaLocation;
      bkt->located = located;
      if (doc != NULL) {
          bkt->doc = doc;
          bkt->targetNamespace = targetNamespace;
          bkt->origTargetNamespace = targetNamespace;
          if (preserveDoc)
            bkt->preserveDoc = 1;
      }
      if (WXS_IS_BUCKET_IMPMAIN(type))
          bkt->imported++;
          /*
          * Add it to the graph of schemas.
          */
      if (relation != NULL)
          relation->bucket = bkt;
    }
  
exit:
    /*
    * Return the bucket explicitely; this is needed for the
    * main schema.
    */
    if (bucket != NULL)
      *bucket = bkt;    
    return (0);

exit_error:
    if ((doc != NULL) && (! preserveDoc)) {
      xmlFreeDoc(doc);
      if (bkt != NULL)
          bkt->doc = NULL;
    }
    return(pctxt->err);

exit_failure:
    if ((doc != NULL) && (! preserveDoc)) {
      xmlFreeDoc(doc);
      if (bkt != NULL)
          bkt->doc = NULL;
    }    
    return (-1);
}

/**
 * xmlSchemaParseImport:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Import definition
 * *WARNING* this interface is highly subject to change
 *
 * Returns 0 in case of success, a positive error code if
 * not valid and -1 in case of an internal error.
 */
static int
xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
                     xmlNodePtr node)
{
    xmlNodePtr child;
    const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
    const xmlChar *thisTargetNamespace;
    xmlAttrPtr attr;
    int ret = 0;
    xmlSchemaBucketPtr bucket = NULL;

    if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
        return (-1);

    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
            xmlSchemaPIllegalAttrErr(pctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(pctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    /*
    * Extract and validate attributes.
    */
    if (xmlSchemaPValAttr(pctxt, NULL, node,
      "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
      &namespaceName) != 0) {
      xmlSchemaPSimpleTypeErr(pctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          NULL, node,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
          NULL, namespaceName, NULL, NULL, NULL);
      return (pctxt->err);
    }

    if (xmlSchemaPValAttr(pctxt, NULL, node,
      "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
      &schemaLocation) != 0) {
      xmlSchemaPSimpleTypeErr(pctxt,
          XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
          NULL, node,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
          NULL, namespaceName, NULL, NULL, NULL);
      return (pctxt->err);
    }
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        /*
         * the annotation here is simply discarded ...
       * TODO: really?
         */
        child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(pctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL,
          "(annotation?)");
    }
    /*
    * Apply additional constraints.
    *
    * Note that it is important to use the original @targetNamespace
    * (or none at all), to rule out imports of schemas _with_ a
    * @targetNamespace if the importing schema is a chameleon schema
    * (with no @targetNamespace).
    */
    thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
    if (namespaceName != NULL) {
      /*
      * 1.1 If the namespace [attribute] is present, then its ·actual value·
      * must not match the ·actual value· of the enclosing <schema>'s
      * targetNamespace [attribute].
      */
      if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_SRC_IMPORT_1_1,
            NULL, node,
            "The value of the attribute 'namespace' must not match "
            "the target namespace '%s' of the importing schema",
            thisTargetNamespace);
          return (pctxt->err);
      }
    } else {
      /*
      * 1.2 If the namespace [attribute] is not present, then the enclosing
      * <schema> must have a targetNamespace [attribute].
      */
      if (thisTargetNamespace == NULL) {
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_SRC_IMPORT_1_2,
            NULL, node,
            "The attribute 'namespace' must be existent if "
            "the importing schema has no target namespace",
            NULL);
          return (pctxt->err);
      }
    }
    /*
    * Locate and acquire the schema document.
    */
    if (schemaLocation != NULL)
      schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
          schemaLocation, node);
    ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
      schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
      namespaceName, &bucket);

    if (ret != 0)
      return(ret);

    /*
    * For <import>: "It is *not* an error for the application
    * schema reference strategy to fail."
    * So just don't parse if no schema document was found.
    * Note that we will get no bucket if the schema could not be
    * located or if there was no schemaLocation.
    */
    if ((bucket == NULL) && (schemaLocation != NULL)) {
      xmlSchemaCustomWarning(ACTXT_CAST pctxt,
          XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
          node, NULL,
          "Failed to locate a schema at location '%s'. "
          "Skipping the import", schemaLocation, NULL, NULL);
    }
    
    if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {     
      ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
    }
    
    return (ret);
}

static int
xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
                             xmlSchemaPtr schema,
                             xmlNodePtr node,
                             xmlChar **schemaLocation,
                             int type)
{
    xmlAttrPtr attr;

    if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
      (schemaLocation == NULL))
        return (-1);

    *schemaLocation = NULL;
    /*
    * Check for illegal attributes.
    * Applies for both <include> and <redefine>.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
            xmlSchemaPIllegalAttrErr(pctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(pctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
    /*
    * Preliminary step, extract the URI-Reference and make an URI
    * from the base.
    */
    /*
    * Attribute "schemaLocation" is mandatory.
    */
    attr = xmlSchemaGetPropNode(node, "schemaLocation");
    if (attr != NULL) {
        xmlChar *base = NULL;
        xmlChar *uri = NULL;

      if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
          (const xmlChar **) schemaLocation) != 0)
          goto exit_error;
      base = xmlNodeGetBase(node->doc, node);
      if (base == NULL) {
          uri = xmlBuildURI(*schemaLocation, node->doc->URL);
      } else {
          uri = xmlBuildURI(*schemaLocation, base);
          xmlFree(base);
      }
      if (uri == NULL) {
          PERROR_INT("xmlSchemaParseIncludeOrRedefine",
            "could not build an URI from the schemaLocation")
          goto exit_failure;
      }
      (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
      xmlFree(uri);
    } else {
      xmlSchemaPMissingAttrErr(pctxt,
          XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node, "schemaLocation", NULL);
      goto exit_error;
    }
    /*
    * Report self-inclusion and self-redefinition.
    */
    if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
      if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_SRC_REDEFINE,
            NULL, node,
            "The schema document '%s' cannot redefine itself.",
            *schemaLocation);     
      } else {
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_SRC_INCLUDE,
            NULL, node,
            "The schema document '%s' cannot include itself.",
            *schemaLocation);
      }
      goto exit_error;
    }
    
    return(0);
exit_error:
    return(pctxt->err);
exit_failure:
    return(-1);
}

static int
xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
                        xmlSchemaPtr schema,
                        xmlNodePtr node,
                        int type)
{
    xmlNodePtr child = NULL;
    const xmlChar *schemaLocation = NULL;
    int res = 0; /* hasRedefinitions = 0 */
    int isChameleon = 0, wasChameleon = 0;
    xmlSchemaBucketPtr bucket = NULL;

    if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
        return (-1);

    /*
    * Parse attributes. Note that the returned schemaLocation will
    * be already converted to an absolute URI.
    */
    res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
      node, (xmlChar **) (&schemaLocation), type);
    if (res != 0)
      return(res);         
    /*
    * Load and add the schema document.
    */
    res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
      NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
    if (res != 0)
      return(res);    
    /*
    * If we get no schema bucket back, then this means that the schema
    * document could not be located or was broken XML or was not
    * a schema document.
    */    
    if ((bucket == NULL) || (bucket->doc == NULL)) {
      if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
          /*
          * WARNING for <include>:
          * We will raise an error if the schema cannot be located
          * for inclusions, since the that was the feedback from the
          * schema people. I.e. the following spec piece will *not* be
          * satisfied:
          * SPEC src-include: "It is not an error for the ·actual value· of the
          * schemaLocation [attribute] to fail to resolve it all, in which
          * case no corresponding inclusion is performed.
          * So do we need a warning report here?"
          */
          res = XML_SCHEMAP_SRC_INCLUDE;
          xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
            node, NULL,
            "Failed to load the document '%s' for inclusion",
            schemaLocation, NULL);
      } else {
          /*
          * NOTE: This was changed to raise an error even if no redefinitions
          * are specified.
          *
          * SPEC src-redefine (1)
          * "If there are any element information items among the [children]
          * other than <annotation> then the ·actual value· of the
          * schemaLocation [attribute] must successfully resolve."
          * TODO: Ask the WG if a the location has always to resolve
          * here as well!
          */
          res = XML_SCHEMAP_SRC_REDEFINE;
          xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
            node, NULL,
            "Failed to load the document '%s' for redefinition",
            schemaLocation, NULL);
      }
    } else {
      /*
      * Check targetNamespace sanity before parsing the new schema.
      * TODO: Note that we won't check further content if the
      * targetNamespace was bad.
      */    
      if (bucket->origTargetNamespace != NULL) {          
          /*
          * SPEC src-include (2.1)
          * "SII has a targetNamespace [attribute], and its ·actual
          * value· is identical to the ·actual value· of the targetNamespace
          * [attribute] of SII’ (which must have such an [attribute])."
          */
          if (pctxt->targetNamespace == NULL) {
            xmlSchemaCustomErr(ACTXT_CAST pctxt,
                XML_SCHEMAP_SRC_INCLUDE,
                node, NULL,
                "The target namespace of the included/redefined schema "
                "'%s' has to be absent, since the including/redefining "
                "schema has no target namespace",
                schemaLocation, NULL);
            goto exit_error;
          } else if (!xmlStrEqual(bucket->origTargetNamespace,
            pctxt->targetNamespace)) {
            /* TODO: Change error function. */
            xmlSchemaPCustomErrExt(pctxt,
                XML_SCHEMAP_SRC_INCLUDE,
                NULL, node,
                "The target namespace '%s' of the included/redefined "
                "schema '%s' differs from '%s' of the "
                "including/redefining schema",
                bucket->origTargetNamespace, schemaLocation,
                pctxt->targetNamespace);
            goto exit_error;
          }
      } else if (pctxt->targetNamespace != NULL) {        
          /*
          * Chameleons: the original target namespace will
          * differ from the resulting namespace.
          */
          isChameleon = 1;
          if (bucket->parsed &&
            (bucket->targetNamespace != pctxt->targetNamespace)) {
            /*
            * This is a sanity check, I dunno yet if this can happen.
            */
            PERROR_INT("xmlSchemaParseIncludeOrRedefine",
                "trying to use an already parsed schema for a "
                "different targetNamespace");
            return(-1);
          }
          bucket->targetNamespace = pctxt->targetNamespace;
      }
    }    
    /*
    * Parse the schema.
    */   
    if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
      if (isChameleon) {
          /* TODO: Get rid of this flag on the schema itself. */
          if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
            schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
          } else
            wasChameleon = 1;
      }
      xmlSchemaParseNewDoc(pctxt, schema, bucket);
      /* Restore chameleon flag. */
      if (isChameleon && (!wasChameleon))
          schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
    }
    /*
    * And now for the children...
    */
    child = node->children;    
    if (type == XML_SCHEMA_SCHEMA_REDEFINE) {   
      /*
      * Parse (simpleType | complexType | group | attributeGroup))*
      */
      pctxt->redefined = bucket;
      /*
      * How to proceed if the redefined schema was not located?
      */
      pctxt->isRedefine = 1;
      while (IS_SCHEMA(child, "annotation") ||
          IS_SCHEMA(child, "simpleType") ||
          IS_SCHEMA(child, "complexType") ||
          IS_SCHEMA(child, "group") ||
          IS_SCHEMA(child, "attributeGroup")) {
          if (IS_SCHEMA(child, "annotation")) {
            /*
            * TODO: discard or not?
            */
          } else if (IS_SCHEMA(child, "simpleType")) {
            xmlSchemaParseSimpleType(pctxt, schema, child, 1);
          } else if (IS_SCHEMA(child, "complexType")) {
            xmlSchemaParseComplexType(pctxt, schema, child, 1);
            /* hasRedefinitions = 1; */
          } else if (IS_SCHEMA(child, "group")) {           
            /* hasRedefinitions = 1; */
            xmlSchemaParseModelGroupDefinition(pctxt,
                schema, child);
          } else if (IS_SCHEMA(child, "attributeGroup")) {
            /* hasRedefinitions = 1; */
            xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
                child);
          }
          child = child->next;
      }
      pctxt->redefined = NULL;
      pctxt->isRedefine = 0;
    } else {
      if (IS_SCHEMA(child, "annotation")) {
          /*
          * TODO: discard or not?
          */
          child = child->next;
      }
    }    
    if (child != NULL) {
      res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
      if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
          xmlSchemaPContentErr(pctxt, res,
            NULL, node, child, NULL,
            "(annotation | (simpleType | complexType | group | attributeGroup))*");
      } else {
           xmlSchemaPContentErr(pctxt, res,
            NULL, node, child, NULL,
            "(annotation?)");
      }     
    }       
    return(res);

exit_error:
    return(pctxt->err);
}

static int
xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
                       xmlNodePtr node)
{
    int res;
#ifndef ENABLE_REDEFINE
    TODO
    return(0);
#endif
    res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
      XML_SCHEMA_SCHEMA_REDEFINE);
    if (res != 0)
      return(res);
    return(0);
}

static int
xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
                       xmlNodePtr node)
{
    int res;

    res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
      XML_SCHEMA_SCHEMA_INCLUDE);
    if (res != 0)
      return(res);
    return(0);
}

/**
 * xmlSchemaParseModelGroup:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 * @type: the "compositor" type
 * @particleNeeded: if a a model group with a particle
 *
 * parse a XML schema Sequence definition.
 * Applies parts of:
 *   Schema Representation Constraint:
 *     Redefinition Constraints and Semantics (src-redefine)
 *     (6.1), (6.1.1), (6.1.2)
 *
 *   Schema Component Constraint: 
 *     All Group Limited (cos-all-limited) (2)
 *     TODO: Actually this should go to component-level checks,
 *     but is done here due to performance. Move it to an other layer
 *     is schema construction via an API is implemented.
 *
 * *WARNING* this interface is highly subject to change
 *
 * Returns -1 in case of error, 0 if the declaration is improper and
 *         1 in case of success.
 */
static xmlSchemaTreeItemPtr
xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                   xmlNodePtr node, xmlSchemaTypeType type,
                   int withParticle)
{
    xmlSchemaModelGroupPtr item;
    xmlSchemaParticlePtr particle = NULL;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;
    int min = 1, max = 1, isElemRef, hasRefs = 0;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);
    /*
    * Create a model group with the given compositor.
    */
    item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
    if (item == NULL)
      return (NULL);

    if (withParticle) {
      if (type == XML_SCHEMA_TYPE_ALL) {
          min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
          max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
      } else {
          /* choice + sequence */
          min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
          max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
            "(xs:nonNegativeInteger | unbounded)");
      }
      xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
      /*
      * Create a particle
      */
      particle = xmlSchemaAddParticle(ctxt, node, min, max);
      if (particle == NULL)
          return (NULL);
      particle->children = (xmlSchemaTreeItemPtr) item;
      /*
      * Check for illegal attributes.
      */
      attr = node->properties;
      while (attr != NULL) {
          if (attr->ns == NULL) {
            if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
                (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
                xmlSchemaPIllegalAttrErr(ctxt,
                  XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
            }
          } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
          attr = attr->next;
      }
    } else {
      /*
      * Check for illegal attributes.
      */
      attr = node->properties;
      while (attr != NULL) {
          if (attr->ns == NULL) {
            if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
                xmlSchemaPIllegalAttrErr(ctxt,
                  XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
            }
          } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
          attr = attr->next;
      }
    }

    /*
    * Extract and validate attributes.
    */
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
        child = child->next;
    }
    if (type == XML_SCHEMA_TYPE_ALL) {
      xmlSchemaParticlePtr part, last = NULL;

      while (IS_SCHEMA(child, "element")) {
          part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
            schema, child, &isElemRef, 0);
          /*
          * SPEC cos-all-limited (2)
          * "The {max occurs} of all the particles in the {particles}
          * of the ('all') group must be 0 or 1.
          */
          if (part != NULL) {
            if (isElemRef)
                hasRefs++;
            if (part->minOccurs > 1) {
                xmlSchemaPCustomErr(ctxt,
                  XML_SCHEMAP_COS_ALL_LIMITED,
                  NULL, child,
                  "Invalid value for minOccurs (must be 0 or 1)",
                  NULL);
                /* Reset to 1. */
                part->minOccurs = 1;
            }
            if (part->maxOccurs > 1) {
                xmlSchemaPCustomErr(ctxt,
                  XML_SCHEMAP_COS_ALL_LIMITED,
                  NULL, child,
                  "Invalid value for maxOccurs (must be 0 or 1)",
                  NULL);
                /* Reset to 1. */
                part->maxOccurs = 1;
            }
            if (last == NULL)
                item->children = (xmlSchemaTreeItemPtr) part;
            else
                last->next = (xmlSchemaTreeItemPtr) part;
            last = part;
          }
          child = child->next;
      }
      if (child != NULL) {
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "(annotation?, (annotation?, element*)");
      }
    } else {
      /* choice + sequence */
      xmlSchemaTreeItemPtr part = NULL, last = NULL;

      while ((IS_SCHEMA(child, "element")) ||
          (IS_SCHEMA(child, "group")) ||
          (IS_SCHEMA(child, "any")) ||
          (IS_SCHEMA(child, "choice")) ||
          (IS_SCHEMA(child, "sequence"))) {

          if (IS_SCHEMA(child, "element")) {
            part = (xmlSchemaTreeItemPtr)
                xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
            if (part && isElemRef)
                hasRefs++;
          } else if (IS_SCHEMA(child, "group")) {
            part =
                xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
            if (part != NULL)
                hasRefs++;
            /*
            * Handle redefinitions.
            */
            if (ctxt->isRedefine && ctxt->redef &&
                (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
                part && part->children)
            {
                if ((xmlSchemaGetQNameRefName(part->children) ==
                      ctxt->redef->refName) &&
                  (xmlSchemaGetQNameRefTargetNs(part->children) ==
                      ctxt->redef->refTargetNs))
                {
                  /*
                  * SPEC src-redefine:
                  * (6.1) "If it has a <group> among its contents at
                  * some level the ·actual value· of whose ref
                  * [attribute] is the same as the ·actual value· of
                  * its own name attribute plus target namespace, then
                  * all of the following must be true:"
                  * (6.1.1) "It must have exactly one such group."
                  */
                  if (ctxt->redefCounter != 0) {
                      xmlChar *str = NULL;

                      xmlSchemaCustomErr(ACTXT_CAST ctxt,
                        XML_SCHEMAP_SRC_REDEFINE, child, NULL,
                        "The redefining model group definition "
                        "'%s' must not contain more than one "
                        "reference to the redefined definition",
                        xmlSchemaFormatQName(&str,
                            ctxt->redef->refTargetNs,
                            ctxt->redef->refName),
                        NULL);
                      FREE_AND_NULL(str)
                      part = NULL;
                  } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
                      ((WXS_PARTICLE(part))->maxOccurs != 1))
                  {
                      xmlChar *str = NULL;
                      /*
                      * SPEC src-redefine:
                      * (6.1.2) "The ·actual value· of both that
                      * group's minOccurs and maxOccurs [attribute]
                      * must be 1 (or ·absent·).
                      */
                      xmlSchemaCustomErr(ACTXT_CAST ctxt,
                        XML_SCHEMAP_SRC_REDEFINE, child, NULL,
                        "The redefining model group definition "
                        "'%s' must not contain a reference to the "
                        "redefined definition with a "
                        "maxOccurs/minOccurs other than 1",
                        xmlSchemaFormatQName(&str,
                            ctxt->redef->refTargetNs,
                            ctxt->redef->refName),
                        NULL);
                      FREE_AND_NULL(str)
                      part = NULL;
                  }
                  ctxt->redef->reference = WXS_BASIC_CAST part;
                  ctxt->redefCounter++;
                }                            
            }
          } else if (IS_SCHEMA(child, "any")) {
            part = (xmlSchemaTreeItemPtr)
                xmlSchemaParseAny(ctxt, schema, child);
          } else if (IS_SCHEMA(child, "choice")) {
            part = xmlSchemaParseModelGroup(ctxt, schema, child,
                XML_SCHEMA_TYPE_CHOICE, 1);
          } else if (IS_SCHEMA(child, "sequence")) {
            part = xmlSchemaParseModelGroup(ctxt, schema, child,
                XML_SCHEMA_TYPE_SEQUENCE, 1);
          }
          if (part != NULL) {
            if (last == NULL)
                item->children = part;
            else
                last->next = part;
            last = part;
          }
          child = child->next;
      }
      if (child != NULL) {
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "(annotation?, (element | group | choice | sequence | any)*)");
      }
    }
    if ((max == 0) && (min == 0))
      return (NULL);
    if (hasRefs) {
      /*
      * We need to resolve references.
      */
      WXS_ADD_PENDING(ctxt, item);
    }
    if (withParticle)
      return ((xmlSchemaTreeItemPtr) particle); 
    else
      return ((xmlSchemaTreeItemPtr) item);
}

/**
 * xmlSchemaParseRestriction:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Restriction definition
 * *WARNING* this interface is highly subject to change
 *
 * Returns the type definition or NULL in case of error
 */
static xmlSchemaTypePtr
xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                          xmlNodePtr node, xmlSchemaTypeType parentType)
{
    xmlSchemaTypePtr type;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);
    /* Not a component, don't create it. */
    type = ctxt->ctxtType;
    type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;

    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    /*
    * Extract and validate attributes.
    */
    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
    /*
    * Attribute 
    */
    /*
    * Extract the base type. The "base" attribute is mandatory if inside
    * a complex type or if redefining.
    *
    * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
    * among its [children]), the simple type definition which is
    * the {content type} of the type definition ·resolved· to by
    * the ·actual value· of the base [attribute]"
    */
    if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
      &(type->baseNs), &(type->base)) == 0)
    {
      if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
          xmlSchemaPMissingAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_MISSING,
            NULL, node, "base", NULL);
      } else if ((ctxt->isRedefine) &&
          (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
      {
          if (type->base == NULL) {
            xmlSchemaPMissingAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_MISSING,
                NULL, node, "base", NULL);
          } else if ((! xmlStrEqual(type->base, type->name)) ||
            (! xmlStrEqual(type->baseNs, type->targetNamespace)))
          {
            xmlChar *str1 = NULL, *str2 = NULL;
            /*
            * REDEFINE: SPEC src-redefine (5)
            * "Within the [children], each <simpleType> must have a
            * <restriction> among its [children] ... the ·actual value· of
            * whose base [attribute] must be the same as the ·actual value·
            * of its own name attribute plus target namespace;"
            */
            xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
                NULL, node, "This is a redefinition, but the QName "
                "value '%s' of the 'base' attribute does not match the "
                "type's designation '%s'",
                xmlSchemaFormatQName(&str1, type->baseNs, type->base),
                xmlSchemaFormatQName(&str2, type->targetNamespace,
                  type->name), NULL);
            FREE_AND_NULL(str1);
            FREE_AND_NULL(str2);
            /* Avoid confusion and erase the values. */
            type->base = NULL;
            type->baseNs = NULL;
          }
      }           
    }
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      /*
      * Add the annotation to the simple type ancestor.
      */
      xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
          xmlSchemaParseAnnotation(ctxt, child, 1));
        child = child->next;
    }
    if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
      /*
      * Corresponds to <simpleType><restriction><simpleType>.
      */
      if (IS_SCHEMA(child, "simpleType")) {
          if (type->base != NULL) {
            /*
            * src-restriction-base-or-simpleType
            * Either the base [attribute] or the simpleType [child] of the
            * <restriction> element must be present, but not both.
            */
            xmlSchemaPContentErr(ctxt,
                XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
                NULL, node, child,
                "The attribute 'base' and the <simpleType> child are "
                "mutually exclusive", NULL);
          } else {
            type->baseType = (xmlSchemaTypePtr)
                xmlSchemaParseSimpleType(ctxt, schema, child, 0);
          }
          child = child->next;
      } else if (type->base == NULL) {
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
            NULL, node, child,
            "Either the attribute 'base' or a <simpleType> child "
            "must be present", NULL);
      }
    } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
      /*
      * Corresponds to <complexType><complexContent><restriction>...
      * followed by:
      *
      * Model groups <all>, <choice> and <sequence>.
      */
      if (IS_SCHEMA(child, "all")) {
          type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt, schema, child,
                XML_SCHEMA_TYPE_ALL, 1);
          child = child->next;
      } else if (IS_SCHEMA(child, "choice")) {
          type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt,
                schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
          child = child->next;
      } else if (IS_SCHEMA(child, "sequence")) {
          type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt, schema, child,
                XML_SCHEMA_TYPE_SEQUENCE, 1);
          child = child->next;
      /*
      * Model group reference <group>.
      */
      } else if (IS_SCHEMA(child, "group")) {       
          type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
          /*
          * Note that the reference will be resolved in
          * xmlSchemaResolveTypeReferences();
          */
          child = child->next;
      }
    } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
      /*
      * Corresponds to <complexType><simpleContent><restriction>...
      *
      * "1.1 the simple type definition corresponding to the <simpleType>
      * among the [children] of <restriction> if there is one;"
      */
      if (IS_SCHEMA(child, "simpleType")) {
          /*
          * We will store the to-be-restricted simple type in
          * type->contentTypeDef *temporarily*.
          */
          type->contentTypeDef = (xmlSchemaTypePtr)
            xmlSchemaParseSimpleType(ctxt, schema, child, 0);
          if ( type->contentTypeDef == NULL)
            return (NULL);
          child = child->next;
      }
    }

    if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
      (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
      xmlSchemaFacetPtr facet, lastfacet = NULL;
      /*
      * Corresponds to <complexType><simpleContent><restriction>...
      * <simpleType><restriction>...
      */

      /*
      * Add the facets to the simple type ancestor.
      */
      /*
      * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
      * Simple Type Definition Schema Representation Constraint:
      * *Single Facet Value*
      */
      while ((IS_SCHEMA(child, "minInclusive")) ||
          (IS_SCHEMA(child, "minExclusive")) ||
          (IS_SCHEMA(child, "maxInclusive")) ||
          (IS_SCHEMA(child, "maxExclusive")) ||
          (IS_SCHEMA(child, "totalDigits")) ||
          (IS_SCHEMA(child, "fractionDigits")) ||
          (IS_SCHEMA(child, "pattern")) ||
          (IS_SCHEMA(child, "enumeration")) ||
          (IS_SCHEMA(child, "whiteSpace")) ||
          (IS_SCHEMA(child, "length")) ||
          (IS_SCHEMA(child, "maxLength")) ||
          (IS_SCHEMA(child, "minLength"))) {
          facet = xmlSchemaParseFacet(ctxt, schema, child);
          if (facet != NULL) {
            if (lastfacet == NULL)
                type->facets = facet;
            else
                lastfacet->next = facet;
            lastfacet = facet;
            lastfacet->next = NULL;
          }
          child = child->next;
      }
      /*
      * Create links for derivation and validation.
      */
      if (type->facets != NULL) {
          xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;

          facet = type->facets;
          do {
            facetLink = (xmlSchemaFacetLinkPtr)
                xmlMalloc(sizeof(xmlSchemaFacetLink));
            if (facetLink == NULL) {
                xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
                xmlFree(facetLink);
                return (NULL);
            }
            facetLink->facet = facet;
            facetLink->next = NULL;
            if (lastFacetLink == NULL)
                type->facetSet = facetLink;
            else
                lastFacetLink->next = facetLink;
            lastFacetLink = facetLink;
            facet = facet->next;
          } while (facet != NULL);
      }
    }
    if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
      /*
      * Attribute uses/declarations.
      */
      if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
          (xmlSchemaItemListPtr *) &(type->attrUses),
          XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
          return(NULL);
      /*
      * Attribute wildcard.
      */
      if (IS_SCHEMA(child, "anyAttribute")) {
          type->attributeWildcard =
            xmlSchemaParseAnyAttribute(ctxt, schema, child);
          child = child->next;
      }
    }
    if (child != NULL) {
      if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "annotation?, (group | all | choice | sequence)?, "
            "((attribute | attributeGroup)*, anyAttribute?))");
      } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
           xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "(annotation?, (simpleType?, (minExclusive | minInclusive | "
            "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
            "length | minLength | maxLength | enumeration | whiteSpace | "
            "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
      } else {
          /* Simple type */
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "(annotation?, (simpleType?, (minExclusive | minInclusive | "
            "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
            "length | minLength | maxLength | enumeration | whiteSpace | "
            "pattern)*))");
      }
    }
    return (NULL);
}

/**
 * xmlSchemaParseExtension:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * Parses an <extension>, which is found inside a
 * <simpleContent> or <complexContent>.
 * *WARNING* this interface is highly subject to change.
 *
 * TODO: Returns the type definition or NULL in case of error
 */
static xmlSchemaTypePtr
xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                        xmlNodePtr node, xmlSchemaTypeType parentType)
{
    xmlSchemaTypePtr type;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);
    /* Not a component, don't create it. */
    type = ctxt->ctxtType;
    type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;

    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }

    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");

    /*
    * Attribute "base" - mandatory.
    */
    if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
      "base", &(type->baseNs), &(type->base)) == 0) &&
      (type->base == NULL)) {
      xmlSchemaPMissingAttrErr(ctxt,
          XML_SCHEMAP_S4S_ATTR_MISSING,
          NULL, node, "base", NULL);
    }
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      /*
      * Add the annotation to the type ancestor.
      */
      xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
          xmlSchemaParseAnnotation(ctxt, child, 1));
        child = child->next;
    }
    if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
      /*
      * Corresponds to <complexType><complexContent><extension>... and:
      *
      * Model groups <all>, <choice>, <sequence> and <group>.
      */
      if (IS_SCHEMA(child, "all")) {
          type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt, schema,
                child, XML_SCHEMA_TYPE_ALL, 1);
          child = child->next;
      } else if (IS_SCHEMA(child, "choice")) {
          type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt, schema,
                child, XML_SCHEMA_TYPE_CHOICE, 1);
          child = child->next;
      } else if (IS_SCHEMA(child, "sequence")) {
          type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt, schema,
            child, XML_SCHEMA_TYPE_SEQUENCE, 1);
          child = child->next;
      } else if (IS_SCHEMA(child, "group")) {
          type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
          /*
          * Note that the reference will be resolved in
          * xmlSchemaResolveTypeReferences();
          */
          child = child->next;
      }
    }
    if (child != NULL) {
      /*
      * Attribute uses/declarations.
      */
      if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
          (xmlSchemaItemListPtr *) &(type->attrUses),
          XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
          return(NULL);
      /*
      * Attribute wildcard.
      */
      if (IS_SCHEMA(child, "anyAttribute")) {
          ctxt->ctxtType->attributeWildcard =
            xmlSchemaParseAnyAttribute(ctxt, schema, child);
          child = child->next;
      }
    }
    if (child != NULL) {
      if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
          /* Complex content extension. */
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "(annotation?, ((group | all | choice | sequence)?, "
            "((attribute | attributeGroup)*, anyAttribute?)))");
      } else {
          /* Simple content extension. */
          xmlSchemaPContentErr(ctxt,
            XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
            NULL, node, child, NULL,
            "(annotation?, ((attribute | attributeGroup)*, "
            "anyAttribute?))");
      }
    }
    return (NULL);
}

/**
 * xmlSchemaParseSimpleContent:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema SimpleContent definition
 * *WARNING* this interface is highly subject to change
 *
 * Returns the type definition or NULL in case of error
 */
static int
xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
                            xmlSchemaPtr schema, xmlNodePtr node,
                      int *hasRestrictionOrExtension)
{
    xmlSchemaTypePtr type;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
      (hasRestrictionOrExtension == NULL))
        return (-1);
    *hasRestrictionOrExtension = 0;
    /* Not a component, don't create it. */
    type = ctxt->ctxtType;
    type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }

    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");

    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      /*
      * Add the annotation to the complex type ancestor.
      */
      xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
          xmlSchemaParseAnnotation(ctxt, child, 1));
        child = child->next;
    }
    if (child == NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_MISSING,
          NULL, node, NULL, NULL,
          "(annotation?, (restriction | extension))");      
    }
    if (child == NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_MISSING,
          NULL, node, NULL, NULL,
          "(annotation?, (restriction | extension))");      
    }
    if (IS_SCHEMA(child, "restriction")) {
        xmlSchemaParseRestriction(ctxt, schema, child,
          XML_SCHEMA_TYPE_SIMPLE_CONTENT);
      (*hasRestrictionOrExtension) = 1;
        child = child->next;
    } else if (IS_SCHEMA(child, "extension")) {
        xmlSchemaParseExtension(ctxt, schema, child,
          XML_SCHEMA_TYPE_SIMPLE_CONTENT);
      (*hasRestrictionOrExtension) = 1;
        child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child, NULL,
          "(annotation?, (restriction | extension))");
    }
    return (0);
}

/**
 * xmlSchemaParseComplexContent:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema ComplexContent definition
 * *WARNING* this interface is highly subject to change
 *
 * Returns the type definition or NULL in case of error
 */
static int
xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
                             xmlSchemaPtr schema, xmlNodePtr node,
                       int *hasRestrictionOrExtension)
{
    xmlSchemaTypePtr type;
    xmlNodePtr child = NULL;
    xmlAttrPtr attr;

    if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
      (hasRestrictionOrExtension == NULL))
        return (-1);
    *hasRestrictionOrExtension = 0;
    /* Not a component, don't create it. */
    type = ctxt->ctxtType;
    /*
    * Check for illegal attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
            (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
          {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }

    xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");

    /*
    * Set the 'mixed' on the complex type ancestor.
    */
    if (xmlGetBooleanProp(ctxt, node, "mixed", 0))  {
      if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
          type->flags |= XML_SCHEMAS_TYPE_MIXED;
    }
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
      /*
      * Add the annotation to the complex type ancestor.
      */
      xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
          xmlSchemaParseAnnotation(ctxt, child, 1));
        child = child->next;
    }
    if (child == NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_MISSING,
          NULL, node, NULL,
          NULL, "(annotation?, (restriction | extension))");
    }
    if (child == NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_MISSING,
          NULL, node, NULL,
          NULL, "(annotation?, (restriction | extension))");
    }
    if (IS_SCHEMA(child, "restriction")) {
        xmlSchemaParseRestriction(ctxt, schema, child,
          XML_SCHEMA_TYPE_COMPLEX_CONTENT);
      (*hasRestrictionOrExtension) = 1;
        child = child->next;
    } else if (IS_SCHEMA(child, "extension")) {
        xmlSchemaParseExtension(ctxt, schema, child,
          XML_SCHEMA_TYPE_COMPLEX_CONTENT);
      (*hasRestrictionOrExtension) = 1;
        child = child->next;
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child,
          NULL, "(annotation?, (restriction | extension))");
    }
    return (0);
}

/**
 * xmlSchemaParseComplexType:
 * @ctxt:  a schema validation context
 * @schema:  the schema being built
 * @node:  a subtree containing XML Schema informations
 *
 * parse a XML schema Complex Type definition
 * *WARNING* this interface is highly subject to change
 *
 * Returns the type definition or NULL in case of error
 */
static xmlSchemaTypePtr
xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
                          xmlNodePtr node, int topLevel)
{
    xmlSchemaTypePtr type, ctxtType;
    xmlNodePtr child = NULL;
    const xmlChar *name = NULL;
    xmlAttrPtr attr;
    const xmlChar *attrValue;
#ifdef ENABLE_NAMED_LOCALS
    char buf[40];
#endif
    int final = 0, block = 0, hasRestrictionOrExtension = 0;


    if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
        return (NULL);

    ctxtType = ctxt->ctxtType;

    if (topLevel) {
      attr = xmlSchemaGetPropNode(node, "name");
      if (attr == NULL) {
          xmlSchemaPMissingAttrErr(ctxt,
            XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
          return (NULL);
      } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
          xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
          return (NULL);
      }     
    }

    if (topLevel == 0) {
      /*
      * Parse as local complex type definition.
      */
#ifdef ENABLE_NAMED_LOCALS
        snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
      type = xmlSchemaAddType(ctxt, schema,
          XML_SCHEMA_TYPE_COMPLEX,
          xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
          ctxt->targetNamespace, node, 0);
#else
      type = xmlSchemaAddType(ctxt, schema,
          XML_SCHEMA_TYPE_COMPLEX,
          NULL, ctxt->targetNamespace, node, 0);
#endif
      if (type == NULL)
          return (NULL);
      name = type->name;
      type->node = node;
      type->type = XML_SCHEMA_TYPE_COMPLEX;
      /*
      * TODO: We need the target namespace.
      */
    } else {
      /*
      * Parse as global complex type definition.
      */
      type = xmlSchemaAddType(ctxt, schema,
          XML_SCHEMA_TYPE_COMPLEX,
          name, ctxt->targetNamespace, node, 1);
      if (type == NULL)
          return (NULL);
      type->node = node;
      type->type = XML_SCHEMA_TYPE_COMPLEX;
      type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
    }
    type->targetNamespace = ctxt->targetNamespace;
    /*
    * Handle attributes.
    */
    attr = node->properties;
    while (attr != NULL) {
      if (attr->ns == NULL) {
          if (xmlStrEqual(attr->name, BAD_CAST "id")) {
            /*
            * Attribute "id".
            */
            xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
          } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
            /*
            * Attribute "mixed".
            */
            if (xmlSchemaPGetBoolNodeValue(ctxt,
                  NULL, (xmlNodePtr) attr))
                type->flags |= XML_SCHEMAS_TYPE_MIXED;
          } else if (topLevel) {
            /*
            * Attributes of global complex type definitions.
            */
            if (xmlStrEqual(attr->name, BAD_CAST "name")) {
                /* Pass. */
            } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
                /*
                * Attribute "abstract".
                */
                if (xmlSchemaPGetBoolNodeValue(ctxt,
                      NULL, (xmlNodePtr) attr))
                  type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
            } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
                /*
                * Attribute "final".
                */
                attrValue = xmlSchemaGetNodeContent(ctxt,
                  (xmlNodePtr) attr);
                if (xmlSchemaPValAttrBlockFinal(attrValue,
                  &(type->flags),
                  -1,
                  XML_SCHEMAS_TYPE_FINAL_EXTENSION,
                  XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
                  -1, -1, -1) != 0)
                {
                  xmlSchemaPSimpleTypeErr(ctxt,
                      XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
                      NULL, (xmlNodePtr) attr, NULL,
                      "(#all | List of (extension | restriction))",
                      attrValue, NULL, NULL, NULL);
                } else 
                  final = 1;
            } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
                /*
                * Attribute "block".
                */
                attrValue = xmlSchemaGetNodeContent(ctxt,
                  (xmlNodePtr) attr);
                if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
                  -1,
                  XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
                  XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
                  -1, -1, -1) != 0) {
                  xmlSchemaPSimpleTypeErr(ctxt,
                      XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
                      NULL, (xmlNodePtr) attr, NULL,
                      "(#all | List of (extension | restriction)) ",
                      attrValue, NULL, NULL, NULL);
                } else 
                  block = 1;
            } else {
                  xmlSchemaPIllegalAttrErr(ctxt,
                      XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
            }
          } else {
            xmlSchemaPIllegalAttrErr(ctxt,
                XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
          }
      } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
          xmlSchemaPIllegalAttrErr(ctxt, 
            XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
      }
      attr = attr->next;
    }
    if (! block) {
      /*
      * Apply default "block" values.
      */
      if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
          type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
      if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
          type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
    }
    if (! final) {
      /*
      * Apply default "block" values.
      */
      if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
          type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
      if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
          type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
    }
    /*
    * And now for the children...
    */
    child = node->children;
    if (IS_SCHEMA(child, "annotation")) {
        type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
        child = child->next;
    }
    ctxt->ctxtType = type;
    if (IS_SCHEMA(child, "simpleContent")) {
      /*
      * <complexType><simpleContent>...
      * 3.4.3 : 2.2
      * Specifying mixed='true' when the <simpleContent>
      * alternative is chosen has no effect
      */
      if (type->flags & XML_SCHEMAS_TYPE_MIXED)
          type->flags ^= XML_SCHEMAS_TYPE_MIXED;
        xmlSchemaParseSimpleContent(ctxt, schema, child,
          &hasRestrictionOrExtension);
        child = child->next;
    } else if (IS_SCHEMA(child, "complexContent")) {
      /*
      * <complexType><complexContent>...
      */
      type->contentType = XML_SCHEMA_CONTENT_EMPTY;
        xmlSchemaParseComplexContent(ctxt, schema, child,
          &hasRestrictionOrExtension);
        child = child->next;
    } else {
      /*
      * E.g <complexType><sequence>... or <complexType><attribute>... etc.
      *
      * SPEC
      * "...the third alternative (neither <simpleContent> nor
      * <complexContent>) is chosen. This case is understood as shorthand
      * for complex content restricting the ·ur-type definition·, and the
      * details of the mappings should be modified as necessary.
      */
      type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
      type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
      /*
      * Parse model groups.
      */
        if (IS_SCHEMA(child, "all")) {
            type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt, schema, child,
                XML_SCHEMA_TYPE_ALL, 1);
            child = child->next;
        } else if (IS_SCHEMA(child, "choice")) {
            type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt, schema, child,
                XML_SCHEMA_TYPE_CHOICE, 1);
            child = child->next;
        } else if (IS_SCHEMA(child, "sequence")) {
            type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroup(ctxt, schema, child,
                XML_SCHEMA_TYPE_SEQUENCE, 1);
            child = child->next;
        } else if (IS_SCHEMA(child, "group")) {
            type->subtypes = (xmlSchemaTypePtr)
            xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
          /*
          * Note that the reference will be resolved in
          * xmlSchemaResolveTypeReferences();
          */
            child = child->next;
        }
      /*
      * Parse attribute decls/refs.
      */
        if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
          (xmlSchemaItemListPtr *) &(type->attrUses),
          XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
          return(NULL);
      /*
      * Parse attribute wildcard.
      */
      if (IS_SCHEMA(child, "anyAttribute")) {
          type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
          child = child->next;
      }
    }
    if (child != NULL) {
      xmlSchemaPContentErr(ctxt,
          XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
          NULL, node, child,
          NULL, "(annotation?, (simpleContent | complexContent | "
          "((group | all | choice | sequence)?, ((attribute | "
          "attributeGroup)*, anyAttribute?))))");
    }
    /*
    * REDEFINE: SPEC src-redefine (5)
    */
    if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
      xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
          NULL, node, "This is a redefinition, thus the "
          "<complexType> must have a <restriction> or <extension> "
          "grand-child", NULL);
    }
    ctxt->ctxtType = ctxtType;
    return (type);
}

/************************************************************************
 *                                                    *
 *                Validating using Schemas                  *
 *                                                    *
 ************************************************************************/

/************************************************************************
 *                                                    *
 *                Reading/Writing Schemas                   *
 *                                                    *
 ************************************************************************/

#if 0 /* Will be enabled if it is clear what options are needed. */
/**
 * xmlSchemaParserCtxtSetOptions:
 * @ctxt:   a schema parser context
 * @options: a combination of xmlSchemaParserOption
 *
 * Sets the options to be used during the parse.
 *
 * Returns 0 in case of success, -1 in case of an
 * API error.
 */
static int
xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
                        int options)

{
    int i;

    if (ctxt == NULL)
      return (-1);
    /*
    * WARNING: Change the start value if adding to the
    * xmlSchemaParseOption.
    */
    for (i = 1; i < (int) sizeof(int) * 8; i++) {
        if (options & 1<<i) {
          return (-1);
        }
    }
    ctxt->options = options;
    return (0);
}

/**
 * xmlSchemaValidCtxtGetOptions:
 * @ctxt: a schema parser context
 *
 * Returns the option combination of the parser context.
 */
static int
xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)

{
    if (ctxt == NULL)
      return (-1);
    else
      return (ctxt->options);
}
#endif

/**
 * xmlSchemaNewParserCtxt:
 * @URL:  the location of the schema
 *
 * Create an XML Schemas parse context for that file/resource expected
 * to contain an XML Schemas file.
 *
 * Returns the parser context or NULL in case of error
 */
xmlSchemaParserCtxtPtr
xmlSchemaNewParserCtxt(const char *URL)
{
    xmlSchemaParserCtxtPtr ret;

    if (URL == NULL)
        return (NULL);

    ret = xmlSchemaParserCtxtCreate();
    if (ret == NULL)
      return(NULL);
    ret->dict = xmlDictCreate();
    ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
    return (ret);
}

/**
 * xmlSchemaNewMemParserCtxt:
 * @buffer:  a pointer to a char array containing the schemas
 * @size:  the size of the array
 *
 * Create an XML Schemas parse context for that memory buffer expected
 * to contain an XML Schemas file.
 *
 * Returns the parser context or NULL in case of error
 */
xmlSchemaParserCtxtPtr
xmlSchemaNewMemParserCtxt(const char *buffer, int size)
{
    xmlSchemaParserCtxtPtr ret;

    if ((buffer == NULL) || (size <= 0))
        return (NULL);
    ret = xmlSchemaParserCtxtCreate();
    if (ret == NULL)
      return(NULL);
    ret->buffer = buffer;
    ret->size = size;
    ret->dict = xmlDictCreate();    
    return (ret);
}

/**
 * xmlSchemaNewDocParserCtxt:
 * @doc:  a preparsed document tree
 *
 * Create an XML Schemas parse context for that document.
 * NB. The document may be modified during the parsing process.
 *
 * Returns the parser context or NULL in case of error
 */
xmlSchemaParserCtxtPtr
xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
{
    xmlSchemaParserCtxtPtr ret;

    if (doc == NULL)
      return (NULL);
    ret = xmlSchemaParserCtxtCreate();
    if (ret == NULL)
      return(NULL);
    ret->doc = doc;
    ret->dict = xmlDictCreate();
    /* The application has responsibility for the document */
    ret->preserve = 1;

    return (ret);
}

/**
 * xmlSchemaFreeParserCtxt:
 * @ctxt:  the schema parser context
 *
 * Free the resources associated to the schema parser context
 */
void
xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
{
    if (ctxt == NULL)
        return;
    if (ctxt->doc != NULL && !ctxt->preserve)
        xmlFreeDoc(ctxt->doc);    
    if (ctxt->vctxt != NULL) {
      xmlSchemaFreeValidCtxt(ctxt->vctxt);
    }
    if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
      xmlSchemaConstructionCtxtFree(ctxt->constructor);
      ctxt->constructor = NULL;
      ctxt->ownsConstructor = 0;
    }
    if (ctxt->attrProhibs != NULL)
      xmlSchemaItemListFree(ctxt->attrProhibs);
    xmlDictFree(ctxt->dict);
    xmlFree(ctxt);
}

/************************************************************************
 *                                                    *
 *                Building the content models               *
 *                                                    *
 ************************************************************************/

static void
xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
      xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
{
    xmlAutomataStatePtr start, tmp;
    xmlSchemaElementPtr elemDecl, member;
    xmlSchemaSubstGroupPtr substGroup;
    int i;

    elemDecl = (xmlSchemaElementPtr) particle->children;
    /*
    * Wrap the substitution group with a CHOICE.
    */
    start = pctxt->state;
    if (end == NULL)
      end = xmlAutomataNewState(pctxt->am);
    substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
    if (substGroup == NULL) {
      xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
          XML_SCHEMAP_INTERNAL,
          "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
          "declaration is marked having a subst. group but none "
          "available.\n", elemDecl->name, NULL);
      return;
    }
    if (counter >= 0) {
      /*
      * NOTE that we put the declaration in, even if it's abstract.
      * However, an error will be raised during *validation* if an element
      * information item shall be validated against an abstract element
      * declaration.
      */
      tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
        xmlAutomataNewTransition2(pctxt->am, tmp, end,
                  elemDecl->name, elemDecl->targetNamespace, elemDecl);
      /*
      * Add subst. group members.
      */
      for (i = 0; i < substGroup->members->nbItems; i++) {
          member = (xmlSchemaElementPtr) substGroup->members->items[i];
            xmlAutomataNewTransition2(pctxt->am, tmp, end,
                           member->name, member->targetNamespace, member);
      }
    } else if (particle->maxOccurs == 1) {
      /*
      * NOTE that we put the declaration in, even if it's abstract,
      */
      xmlAutomataNewEpsilon(pctxt->am,
          xmlAutomataNewTransition2(pctxt->am,
          start, NULL,
          elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
      /*
      * Add subst. group members.
      */
      for (i = 0; i < substGroup->members->nbItems; i++) {
          member = (xmlSchemaElementPtr) substGroup->members->items[i];
          /*
          * NOTE: This fixes bug #341150. xmlAutomataNewOnceTrans2()
          *  was incorrectly used instead of xmlAutomataNewTransition2()
          *  (seems like a copy&paste bug from the XML_SCHEMA_TYPE_ALL
          *  section in xmlSchemaBuildAContentModel() ).
          * TODO: Check if xmlAutomataNewOnceTrans2() was instead 
          *  intended for the above "counter" section originally. I.e.,
          *  check xs:all with subst-groups.
          *
          * tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
          *                member->name, member->targetNamespace,
          *              1, 1, member);
          */
          tmp = xmlAutomataNewTransition2(pctxt->am, start, NULL,
            member->name, member->targetNamespace, member);     
          xmlAutomataNewEpsilon(pctxt->am, tmp, end);
      }
    } else {
      xmlAutomataStatePtr hop;
      int maxOccurs = particle->maxOccurs == UNBOUNDED ?
          UNBOUNDED : particle->maxOccurs - 1;
      int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;

      counter =
          xmlAutomataNewCounter(pctxt->am, minOccurs,
          maxOccurs);
      hop = xmlAutomataNewState(pctxt->am);

      xmlAutomataNewEpsilon(pctxt->am,
          xmlAutomataNewTransition2(pctxt->am,
          start, NULL,
          elemDecl->name, elemDecl->targetNamespace, elemDecl),
          hop);
      /*
       * Add subst. group members.
       */
      for (i = 0; i < substGroup->members->nbItems; i++) {
          member = (xmlSchemaElementPtr) substGroup->members->items[i];
          xmlAutomataNewEpsilon(pctxt->am,
            xmlAutomataNewTransition2(pctxt->am,
            start, NULL,
            member->name, member->targetNamespace, member),
            hop);
      }
      xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
      xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
    }
    if (particle->minOccurs == 0)
      xmlAutomataNewEpsilon(pctxt->am, start, end);
    pctxt->state = end;
}

static void
xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
                             xmlSchemaParticlePtr particle)
{
    if (((xmlSchemaElementPtr) particle->children)->flags &
      XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
      /*
      * Substitution groups.
      */
      xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
    } else {
      xmlSchemaElementPtr elemDecl;
      xmlAutomataStatePtr start;

      elemDecl = (xmlSchemaElementPtr) particle->children;

      if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
          return;
      if (particle->maxOccurs == 1) {
          start = ctxt->state;
          ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
                elemDecl->name, elemDecl->targetNamespace, elemDecl);
      } else if ((particle->maxOccurs >= UNBOUNDED) &&
                 (particle->minOccurs < 2)) {
          /* Special case. */
          start = ctxt->state;          
          ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
            elemDecl->name, elemDecl->targetNamespace, elemDecl);     
          ctxt->state = xmlAutomataNewTransition2(ctxt->am, ctxt->state, ctxt->state,
            elemDecl->name, elemDecl->targetNamespace, elemDecl);     
      } else {
          int counter;
          int maxOccurs = particle->maxOccurs == UNBOUNDED ?
                      UNBOUNDED : particle->maxOccurs - 1;
          int minOccurs = particle->minOccurs < 1 ?
                      0 : particle->minOccurs - 1;

          start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
          counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
          ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
            elemDecl->name, elemDecl->targetNamespace, elemDecl);
          xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
          ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
            NULL, counter);
      }
      if (particle->minOccurs == 0)
          xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
    }
}

/**
 * xmlSchemaBuildAContentModel:
 * @ctxt:  the schema parser context
 * @particle:  the particle component
 * @name:  the complex type's name whose content is being built
 *
 * Create the automaton for the {content type} of a complex type.
 *
 */
static void
xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
                      xmlSchemaParticlePtr particle)
{
    if (particle == NULL) {
      PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");        
      return;
    }
    if (particle->children == NULL) {
      /*
      * Just return in this case. A missing "term" of the particle
      * might arise due to an invalid "term" component.
      */
      return;
    }

    switch (particle->children->type) {
      case XML_SCHEMA_TYPE_ANY: {
          xmlAutomataStatePtr start, end;
          xmlSchemaWildcardPtr wild;
          xmlSchemaWildcardNsPtr ns;

          wild = (xmlSchemaWildcardPtr) particle->children;

          start = pctxt->state;
          end = xmlAutomataNewState(pctxt->am);

          if (particle->maxOccurs == 1) {
            if (wild->any == 1) {
                /*
                * We need to add both transitions:
                *
                * 1. the {"*", "*"} for elements in a namespace.
                */
                pctxt->state =
                  xmlAutomataNewTransition2(pctxt->am,
                  start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
                xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
                /*
                * 2. the {"*"} for elements in no namespace.
                */
                pctxt->state =
                  xmlAutomataNewTransition2(pctxt->am,
                  start, NULL, BAD_CAST "*", NULL, wild);
                xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);

            } else if (wild->nsSet != NULL) {
                ns = wild->nsSet;
                do {
                  pctxt->state = start;
                  pctxt->state = xmlAutomataNewTransition2(pctxt->am,
                      pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
                  xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
                  ns = ns->next;
                } while (ns != NULL);

            } else if (wild->negNsSet != NULL) {
                pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
                  start, end, BAD_CAST "*", wild->negNsSet->value,
                  wild);
            }
          } else {
            int counter;
            xmlAutomataStatePtr hop;
            int maxOccurs =
                particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
            int minOccurs =
                particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;

            counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
            hop = xmlAutomataNewState(pctxt->am);
            if (wild->any == 1) {
                pctxt->state =
                  xmlAutomataNewTransition2(pctxt->am,
                  start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
                xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
                pctxt->state =
                  xmlAutomataNewTransition2(pctxt->am,
                  start, NULL, BAD_CAST "*", NULL, wild);
                xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
            } else if (wild->nsSet != NULL) {
                ns = wild->nsSet;
                do {
                  pctxt->state =
                      xmlAutomataNewTransition2(pctxt->am,
                        start, NULL, BAD_CAST "*", ns->value, wild);
                  xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
                  ns = ns->next;
                } while (ns != NULL);

            } else if (wild->negNsSet != NULL) {
                pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
                  start, hop, BAD_CAST "*", wild->negNsSet->value,
                  wild);
            }
            xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
            xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
          }
          if (particle->minOccurs == 0) {
            xmlAutomataNewEpsilon(pctxt->am, start, end);
          }
          pctxt->state = end;
            break;
      }
        case XML_SCHEMA_TYPE_ELEMENT:
          xmlSchemaBuildContentModelForElement(pctxt, particle);
          break;
        case XML_SCHEMA_TYPE_SEQUENCE:{
                xmlSchemaTreeItemPtr sub;

                /*
                 * If max and min occurances are default (1) then
                 * simply iterate over the particles of the <sequence>.
                 */
                if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
                    sub = particle->children->children;
                    while (sub != NULL) {
                        xmlSchemaBuildAContentModel(pctxt,
                      (xmlSchemaParticlePtr) sub);
                        sub = sub->next;
                    }
                } else {
                    xmlAutomataStatePtr oldstate = pctxt->state;

                    if (particle->maxOccurs >= UNBOUNDED) {
                        if (particle->minOccurs > 1) {
                            xmlAutomataStatePtr tmp;
                            int counter;

                            pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
                        oldstate, NULL);
                            oldstate = pctxt->state;

                            counter = xmlAutomataNewCounter(pctxt->am,
                        particle->minOccurs - 1, UNBOUNDED);

                            sub = particle->children->children;
                            while (sub != NULL) {
                                xmlSchemaBuildAContentModel(pctxt,
                            (xmlSchemaParticlePtr) sub);
                                sub = sub->next;
                            }
                            tmp = pctxt->state;
                            xmlAutomataNewCountedTrans(pctxt->am, tmp,
                                                       oldstate, counter);
                            pctxt->state =
                                xmlAutomataNewCounterTrans(pctxt->am, tmp,
                                                           NULL, counter);

                        } else {
                            pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
                        oldstate, NULL);
                            oldstate = pctxt->state;

                      sub = particle->children->children;
                            while (sub != NULL) {
                                xmlSchemaBuildAContentModel(pctxt,
                            (xmlSchemaParticlePtr) sub);
                                sub = sub->next;
                            }
                            xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
                                                  oldstate);
                      /*
                       * epsilon needed to block previous trans from
                       * being allowed to enter back from another
                       * construct
                       */
                      pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
                                    pctxt->state, NULL);
                            if (particle->minOccurs == 0) {
                                xmlAutomataNewEpsilon(pctxt->am,
                            oldstate, pctxt->state);
                            }
                        }
                    } else if ((particle->maxOccurs > 1)
                               || (particle->minOccurs > 1)) {
                        xmlAutomataStatePtr tmp;
                        int counter;

                        pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
                      oldstate, NULL);
                        oldstate = pctxt->state;

                        counter = xmlAutomataNewCounter(pctxt->am,
                      particle->minOccurs - 1,
                      particle->maxOccurs - 1);

                        sub = particle->children->children;
                        while (sub != NULL) {
                            xmlSchemaBuildAContentModel(pctxt,
                        (xmlSchemaParticlePtr) sub);
                            sub = sub->next;
                        }
                        tmp = pctxt->state;
                        xmlAutomataNewCountedTrans(pctxt->am,
                      tmp, oldstate, counter);
                        pctxt->state =
                            xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
                                                       counter);
                        if (particle->minOccurs == 0) {
                            xmlAutomataNewEpsilon(pctxt->am,
                        oldstate, pctxt->state);
                        }
                    } else {
                        sub = particle->children->children;
                        while (sub != NULL) {
                            xmlSchemaBuildAContentModel(pctxt,
                        (xmlSchemaParticlePtr) sub);
                            sub = sub->next;
                        }
                        if (particle->minOccurs == 0) {
                            xmlAutomataNewEpsilon(pctxt->am, oldstate,
                                                  pctxt->state);
                        }
                    }
                }
                break;
            }
        case XML_SCHEMA_TYPE_CHOICE:{
                xmlSchemaTreeItemPtr sub;
                xmlAutomataStatePtr start, end;

                start = pctxt->state;
                end = xmlAutomataNewState(pctxt->am);

                /*
                 * iterate over the subtypes and remerge the end with an
                 * epsilon transition
                 */
                if (particle->maxOccurs == 1) {
                sub = particle->children->children;
                    while (sub != NULL) {
                        pctxt->state = start;
                        xmlSchemaBuildAContentModel(pctxt,
                      (xmlSchemaParticlePtr) sub);
                        xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
                        sub = sub->next;
                    }
                } else {
                    int counter;
                    xmlAutomataStatePtr hop, base;
                    int maxOccurs = particle->maxOccurs == UNBOUNDED ?
                        UNBOUNDED : particle->maxOccurs - 1;
                    int minOccurs =
                        particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;

                    /*
                     * use a counter to keep track of the number of transtions
                     * which went through the choice.
                     */
                    counter =
                        xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
                    hop = xmlAutomataNewState(pctxt->am);
                    base = xmlAutomataNewState(pctxt->am);

                sub = particle->children->children;
                    while (sub != NULL) {
                        pctxt->state = base;
                        xmlSchemaBuildAContentModel(pctxt,
                      (xmlSchemaParticlePtr) sub);
                        xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
                        sub = sub->next;
                    }
                    xmlAutomataNewEpsilon(pctxt->am, start, base);
                xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
                    xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
                }
                if (particle->minOccurs == 0) {
                    xmlAutomataNewEpsilon(pctxt->am, start, end);
                }
                pctxt->state = end;
                break;
            }
        case XML_SCHEMA_TYPE_ALL:{
                xmlAutomataStatePtr start;
            xmlSchemaParticlePtr sub;
            xmlSchemaElementPtr elemDecl;
                int lax;

            sub = (xmlSchemaParticlePtr) particle->children->children;
                if (sub == NULL)
                    break;
                start = pctxt->state;
                while (sub != NULL) {
                    pctxt->state = start;

                elemDecl = (xmlSchemaElementPtr) sub->children;
                if (elemDecl == NULL) {
                  PERROR_INT("xmlSchemaBuildAContentModel",
                      "<element> particle has no term");
                  return;
                };
                /*
                * NOTE: The {max occurs} of all the particles in the
                * {particles} of the group must be 0 or 1; this is
                * already ensured during the parse of the content of
                * <all>.
                */
                if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
                  int counter;

                    /*
                   * This is an abstract group, we need to share
                   * the same counter for all the element transitions
                   * derived from the group
                   */
                  counter = xmlAutomataNewCounter(pctxt->am,
                                     sub->minOccurs, sub->maxOccurs);
                  xmlSchemaBuildContentModelForSubstGroup(pctxt,
                                 sub, counter, pctxt->state);
                } else {
                  if ((sub->minOccurs == 1) &&
                      (sub->maxOccurs == 1)) {
                      xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
                                        pctxt->state,
                                        elemDecl->name,
                                        elemDecl->targetNamespace,
                                        1, 1, elemDecl);
                  } else if ((sub->minOccurs == 0) &&
                      (sub->maxOccurs == 1)) {

                      xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
                                         pctxt->state,
                                         elemDecl->name,
                                         elemDecl->targetNamespace,
                                         0,
                                         1,
                                         elemDecl);
                  }
                }
                    sub = (xmlSchemaParticlePtr) sub->next;
                }
                lax = particle->minOccurs == 0;
                pctxt->state =
                    xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
                break;
            }
      case XML_SCHEMA_TYPE_GROUP:
          /*
          * If we hit a model group definition, then this means that
          * it was empty, thus was not substituted for the containing
          * model group. Just do nothing in this case.
          * TODO: But the group should be substituted and not occur at
          * all in the content model at this point. Fix this.
          */
          break;
        default:
          xmlSchemaInternalErr2(ACTXT_CAST pctxt,
            "xmlSchemaBuildAContentModel",
            "found unexpected term of type '%s' in content model",
            WXS_ITEM_TYPE_NAME(particle->children), NULL);
            return;
    }
}

/**
 * xmlSchemaBuildContentModel:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 * @name:  the element name
 *
 * Builds the content model of the complex type.
 */
static void
xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
                     xmlSchemaParserCtxtPtr ctxt)
{
    if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
      (type->contModel != NULL) ||
      ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
      (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
      return;

#ifdef DEBUG_CONTENT
    xmlGenericError(xmlGenericErrorContext,
                    "Building content model for %s\n", name);
#endif
    ctxt->am = NULL;
    ctxt->am = xmlNewAutomata();
    if (ctxt->am == NULL) {
        xmlGenericError(xmlGenericErrorContext,
          "Cannot create automata for complex type %s\n", type->name);
        return;
    }
    ctxt->state = xmlAutomataGetInitState(ctxt->am);
    /*
    * Build the automaton.
    */
    xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
    xmlAutomataSetFinalState(ctxt->am, ctxt->state);
    type->contModel = xmlAutomataCompile(ctxt->am);
    if (type->contModel == NULL) {
        xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_INTERNAL,
          WXS_BASIC_CAST type, type->node,
          "Failed to compile the content model", NULL);
    } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
        xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_NOT_DETERMINISTIC,
          /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
          WXS_BASIC_CAST type, type->node,
          "The content model is not determinist", NULL);
    } else {
#ifdef DEBUG_CONTENT_REGEXP
        xmlGenericError(xmlGenericErrorContext,
                        "Content model of %s:\n", type->name);
        xmlRegexpPrint(stderr, type->contModel);
#endif
    }
    ctxt->state = NULL;
    xmlFreeAutomata(ctxt->am);
    ctxt->am = NULL;
}

/**
 * xmlSchemaResolveElementReferences:
 * @elem:  the schema element context
 * @ctxt:  the schema parser context
 *
 * Resolves the references of an element declaration
 * or particle, which has an element declaration as it's
 * term.
 */
static void
xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
                          xmlSchemaParserCtxtPtr ctxt)
{
    if ((ctxt == NULL) || (elemDecl == NULL) ||
      ((elemDecl != NULL) &&
      (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
        return;
    elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;

    if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
      xmlSchemaTypePtr type;

      /* (type definition) ... otherwise the type definition ·resolved·
      * to by the ·actual value· of the type [attribute] ...
      */
      type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
          elemDecl->namedTypeNs);
      if (type == NULL) {
          xmlSchemaPResCompAttrErr(ctxt,
            XML_SCHEMAP_SRC_RESOLVE,
            WXS_BASIC_CAST elemDecl, elemDecl->node,
            "type", elemDecl->namedType, elemDecl->namedTypeNs,
            XML_SCHEMA_TYPE_BASIC, "type definition");
      } else
          elemDecl->subtypes = type;
    }
    if (elemDecl->substGroup != NULL) {
      xmlSchemaElementPtr substHead;

      /*
      * FIXME TODO: Do we need a new field in _xmlSchemaElement for
      * substitutionGroup?
      */
      substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
          elemDecl->substGroupNs);
      if (substHead == NULL) {
          xmlSchemaPResCompAttrErr(ctxt,
            XML_SCHEMAP_SRC_RESOLVE,
            WXS_BASIC_CAST elemDecl, NULL,
            "substitutionGroup", elemDecl->substGroup,
            elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
      } else {
          xmlSchemaResolveElementReferences(substHead, ctxt);
          /*
          * Set the "substitution group affiliation".
          * NOTE that now we use the "refDecl" field for this.
          */
          WXS_SUBST_HEAD(elemDecl) = substHead;
          /*
          * The type definitions is set to:
          * SPEC "...the {type definition} of the element
          * declaration ·resolved· to by the ·actual value·
          * of the substitutionGroup [attribute], if present"
          */
          if (elemDecl->subtypes == NULL)
            elemDecl->subtypes = substHead->subtypes;
      }
    }
    /*
    * SPEC "The definition of anyType serves as the default type definition
    * for element declarations whose XML representation does not specify one."
    */
    if ((elemDecl->subtypes == NULL) &&
      (elemDecl->namedType == NULL) &&
      (elemDecl->substGroup == NULL))
      elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
}

/**
 * xmlSchemaResolveUnionMemberTypes:
 * @ctxt:  the schema parser context
 * @type:  the schema simple type definition
 *
 * Checks and builds the "member type definitions" property of the union
 * simple type. This handles part (1), part (2) is done in
 * xmlSchemaFinishMemberTypeDefinitionsProperty()
 *
 * Returns -1 in case of an internal error, 0 otherwise.
 */
static int
xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
                         xmlSchemaTypePtr type)
{

    xmlSchemaTypeLinkPtr link, lastLink, newLink;
    xmlSchemaTypePtr memberType;

    /*
    * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
    * define the explicit members as the type definitions ·resolved·
    * to by the items in the ·actual value· of the memberTypes [attribute],
    * if any, followed by the type definitions corresponding to the
    * <simpleType>s among the [children] of <union>, if any."
    */
    /*
    * Resolve references.
    */
    link = type->memberTypes;
    lastLink = NULL;
    while (link != NULL) {
      const xmlChar *name, *nsName;

      name = ((xmlSchemaQNameRefPtr) link->type)->name;
      nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;

      memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
      if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
          xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
            WXS_BASIC_CAST type, type->node, "memberTypes",
            name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
          /*
          * Remove the member type link.
          */
          if (lastLink == NULL)
            type->memberTypes = link->next;
          else
            lastLink->next = link->next;
          newLink = link;
          link = link->next;
          xmlFree(newLink);
      } else {
          link->type = memberType;
          lastLink = link;
          link = link->next;
      }
    }
    /*
    * Add local simple types,
    */
    memberType = type->subtypes;
    while (memberType != NULL) {
      link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
      if (link == NULL) {
          xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
          return (-1);
      }
      link->type = memberType;
      link->next = NULL;
      if (lastLink == NULL)
          type->memberTypes = link;
      else
          lastLink->next = link;
      lastLink = link;
      memberType = memberType->next;
    }
    return (0);
}

/**
 * xmlSchemaIsDerivedFromBuiltInType:
 * @ctxt:  the schema parser context
 * @type:  the type definition
 * @valType: the value type
 *
 *
 * Returns 1 if the type has the given value type, or
 * is derived from such a type.
 */
static int
xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
{
    if (type == NULL)
      return (0);
    if (WXS_IS_COMPLEX(type))
      return (0);
    if (type->type == XML_SCHEMA_TYPE_BASIC) {
      if (type->builtInType == valType)
          return(1);
      if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
          (type->builtInType == XML_SCHEMAS_ANYTYPE))
          return (0);
      return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
    }
    return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
}

#if 0
/**
 * xmlSchemaIsDerivedFromBuiltInType:
 * @ctxt:  the schema parser context
 * @type:  the type definition
 * @valType: the value type
 *
 *
 * Returns 1 if the type has the given value type, or
 * is derived from such a type.
 */
static int
xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
{
    if (type == NULL)
      return (0);
    if (WXS_IS_COMPLEX(type))
      return (0);
    if (type->type == XML_SCHEMA_TYPE_BASIC) {
      if (type->builtInType == valType)
          return(1);
      return (0);
    } else
      return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));

    return (0);
}
#endif

static xmlSchemaTypePtr
xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
{
    if (type == NULL)
      return (NULL);
    if (WXS_IS_COMPLEX(type))
      return (NULL);
    if (type->type == XML_SCHEMA_TYPE_BASIC)
      return(type);
    return(xmlSchemaQueryBuiltInType(type->subtypes));
}

/**
 * xmlSchemaGetPrimitiveType:
 * @type:  the simpleType definition
 *
 * Returns the primitive type of the given type or
 * NULL in case of error.
 */
static xmlSchemaTypePtr
xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
{

    while (type != NULL) {
      /*
      * Note that anySimpleType is actually not a primitive type
      * but we need that here.
      */
      if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
         (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
          return (type);
      type = type->baseType;
    }

    return (NULL);
}

#if 0
/**
 * xmlSchemaGetBuiltInTypeAncestor:
 * @type:  the simpleType definition
 *
 * Returns the primitive type of the given type or
 * NULL in case of error.
 */
static xmlSchemaTypePtr
xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
{
    if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
      return (0);
    while (type != NULL) {
      if (type->type == XML_SCHEMA_TYPE_BASIC)
          return (type);
      type = type->baseType;
    }

    return (NULL);
}
#endif

/**
 * xmlSchemaCloneWildcardNsConstraints:
 * @ctxt:  the schema parser context
 * @dest:  the destination wildcard
 * @source: the source wildcard
 *
 * Clones the namespace constraints of source
 * and assignes them to dest.
 * Returns -1 on internal error, 0 otherwise.
 */
static int
xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
                            xmlSchemaWildcardPtr dest,
                            xmlSchemaWildcardPtr source)
{
    xmlSchemaWildcardNsPtr cur, tmp, last;

    if ((source == NULL) || (dest == NULL))
      return(-1);
    dest->any = source->any;
    cur = source->nsSet;
    last = NULL;
    while (cur != NULL) {
      tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
      if (tmp == NULL)
          return(-1);
      tmp->value = cur->value;
      if (last == NULL)
          dest->nsSet = tmp;
      else
          last->next = tmp;
      last = tmp;
      cur = cur->next;
    }
    if (dest->negNsSet != NULL)
      xmlSchemaFreeWildcardNsSet(dest->negNsSet);
    if (source->negNsSet != NULL) {
      dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
      if (dest->negNsSet == NULL)
          return(-1);
      dest->negNsSet->value = source->negNsSet->value;
    } else
      dest->negNsSet = NULL;
    return(0);
}

/**
 * xmlSchemaUnionWildcards:
 * @ctxt:  the schema parser context
 * @completeWild:  the first wildcard
 * @curWild: the second wildcard
 *
 * Unions the namespace constraints of the given wildcards.
 * @completeWild will hold the resulting union.
 * Returns a positive error code on failure, -1 in case of an
 * internal error, 0 otherwise.
 */
static int
xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
                      xmlSchemaWildcardPtr completeWild,
                      xmlSchemaWildcardPtr curWild)
{
    xmlSchemaWildcardNsPtr cur, curB, tmp;

    /*
    * 1 If O1 and O2 are the same value, then that value must be the
    * value.
    */
    if ((completeWild->any == curWild->any) &&
      ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
      ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {

      if ((completeWild->negNsSet == NULL) ||
          (completeWild->negNsSet->value == curWild->negNsSet->value)) {

          if (completeWild->nsSet != NULL) {
            int found = 0;

            /*
            * Check equality of sets.
            */
            cur = completeWild->nsSet;
            while (cur != NULL) {
                found = 0;
                curB = curWild->nsSet;
                while (curB != NULL) {
                  if (cur->value == curB->value) {
                      found = 1;
                      break;
                  }
                  curB = curB->next;
                }
                if (!found)
                  break;
                cur = cur->next;
            }
            if (found)
                return(0);
          } else
            return(0);
      }
    }
    /*
    * 2 If either O1 or O2 is any, then any must be the value
    */
    if (completeWild->any != curWild->any) {
      if (completeWild->any == 0) {
          completeWild->any = 1;
          if (completeWild->nsSet != NULL) {
            xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
            completeWild->nsSet = NULL;
          }
          if (completeWild->negNsSet != NULL) {
            xmlFree(completeWild->negNsSet);
            completeWild->negNsSet = NULL;
          }
      }
      return (0);
    }
    /*
    * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
    * then the union of those sets must be the value.
    */
    if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
      int found;
      xmlSchemaWildcardNsPtr start;

      cur = curWild->nsSet;
      start = completeWild->nsSet;
      while (cur != NULL) {
          found = 0;
          curB = start;
          while (curB != NULL) {
            if (cur->value == curB->value) {
                found = 1;
                break;
            }
            curB = curB->next;
          }
          if (!found) {
            tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
            if (tmp == NULL)
                return (-1);
            tmp->value = cur->value;
            tmp->next = completeWild->nsSet;
            completeWild->nsSet = tmp;
          }
          cur = cur->next;
      }

      return(0);
    }
    /*
    * 4 If the two are negations of different values (namespace names
    * or ·absent·), then a pair of not and ·absent· must be the value.
    */
    if ((completeWild->negNsSet != NULL) &&
      (curWild->negNsSet != NULL) &&
      (completeWild->negNsSet->value != curWild->negNsSet->value)) {
      completeWild->negNsSet->value = NULL;

      return(0);
    }
    /*
     * 5.
     */
    if (((completeWild->negNsSet != NULL) &&
      (completeWild->negNsSet->value != NULL) &&
      (curWild->nsSet != NULL)) ||
      ((curWild->negNsSet != NULL) &&
      (curWild->negNsSet->value != NULL) &&
      (completeWild->nsSet != NULL))) {

      int nsFound, absentFound = 0;

      if (completeWild->nsSet != NULL) {
          cur = completeWild->nsSet;
          curB = curWild->negNsSet;
      } else {
          cur = curWild->nsSet;
          curB = completeWild->negNsSet;
      }
      nsFound = 0;
      while (cur != NULL) {
          if (cur->value == NULL)
            absentFound = 1;
          else if (cur->value == curB->value)
            nsFound = 1;
          if (nsFound && absentFound)
            break;
          cur = cur->next;
      }

      if (nsFound && absentFound) {
          /*
          * 5.1 If the set S includes both the negated namespace
          * name and ·absent·, then any must be the value.
          */
          completeWild->any = 1;
          if (completeWild->nsSet != NULL) {
            xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
            completeWild->nsSet = NULL;
          }
          if (completeWild->negNsSet != NULL) {
            xmlFree(completeWild->negNsSet);
            completeWild->negNsSet = NULL;
          }
      } else if (nsFound && (!absentFound)) {
          /*
          * 5.2 If the set S includes the negated namespace name
          * but not ·absent·, then a pair of not and ·absent· must
          * be the value.
          */
          if (completeWild->nsSet != NULL) {
            xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
            completeWild->nsSet = NULL;
          }
          if (completeWild->negNsSet == NULL) {
            completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
            if (completeWild->negNsSet == NULL)
                return (-1);
          }
          completeWild->negNsSet->value = NULL;
      } else if ((!nsFound) && absentFound) {
          /*
          * 5.3 If the set S includes ·absent· but not the negated
          * namespace name, then the union is not expressible.
          */
          xmlSchemaPErr(ctxt, completeWild->node,
            XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
            "The union of the wilcard is not expressible.\n",
            NULL, NULL);
          return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
      } else if ((!nsFound) && (!absentFound)) {
          /*
          * 5.4 If the set S does not include either the negated namespace
          * name or ·absent·, then whichever of O1 or O2 is a pair of not
          * and a namespace name must be the value.
          */
          if (completeWild->negNsSet == NULL) {
            if (completeWild->nsSet != NULL) {
                xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
                completeWild->nsSet = NULL;
            }
            completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
            if (completeWild->negNsSet == NULL)
                return (-1);
            completeWild->negNsSet->value = curWild->negNsSet->value;
          }
      }
      return (0);
    }
    /*
     * 6.
     */
    if (((completeWild->negNsSet != NULL) &&
      (completeWild->negNsSet->value == NULL) &&
      (curWild->nsSet != NULL)) ||
      ((curWild->negNsSet != NULL) &&
      (curWild->negNsSet->value == NULL) &&
      (completeWild->nsSet != NULL))) {

      if (completeWild->nsSet != NULL) {
          cur = completeWild->nsSet;
      } else {
          cur = curWild->nsSet;
      }
      while (cur != NULL) {
          if (cur->value == NULL) {
            /*
            * 6.1 If the set S includes ·absent·, then any must be the
            * value.
            */
            completeWild->any = 1;
            if (completeWild->nsSet != NULL) {
                xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
                completeWild->nsSet = NULL;
            }
            if (completeWild->negNsSet != NULL) {
                xmlFree(completeWild->negNsSet);
                completeWild->negNsSet = NULL;
            }
            return (0);
          }
          cur = cur->next;
      }
      if (completeWild->negNsSet == NULL) {
          /*
          * 6.2 If the set S does not include ·absent·, then a pair of not
          * and ·absent· must be the value.
          */
          if (completeWild->nsSet != NULL) {
            xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
            completeWild->nsSet = NULL;
          }
          completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
          if (completeWild->negNsSet == NULL)
            return (-1);
          completeWild->negNsSet->value = NULL;
      }
      return (0);
    }
    return (0);

}

/**
 * xmlSchemaIntersectWildcards:
 * @ctxt:  the schema parser context
 * @completeWild:  the first wildcard
 * @curWild: the second wildcard
 *
 * Intersects the namespace constraints of the given wildcards.
 * @completeWild will hold the resulting intersection.
 * Returns a positive error code on failure, -1 in case of an
 * internal error, 0 otherwise.
 */
static int
xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
                      xmlSchemaWildcardPtr completeWild,
                      xmlSchemaWildcardPtr curWild)
{
    xmlSchemaWildcardNsPtr cur, curB, prev,  tmp;

    /*
    * 1 If O1 and O2 are the same value, then that value must be the
    * value.
    */
    if ((completeWild->any == curWild->any) &&
      ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
      ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {

      if ((completeWild->negNsSet == NULL) ||
          (completeWild->negNsSet->value == curWild->negNsSet->value)) {

          if (completeWild->nsSet != NULL) {
            int found = 0;

            /*
            * Check equality of sets.
            */
            cur = completeWild->nsSet;
            while (cur != NULL) {
                found = 0;
                curB = curWild->nsSet;
                while (curB != NULL) {
                  if (cur->value == curB->value) {
                      found = 1;
                      break;
                  }
                  curB = curB->next;
                }
                if (!found)
                  break;
                cur = cur->next;
            }
            if (found)
                return(0);
          } else
            return(0);
      }
    }
    /*
    * 2 If either O1 or O2 is any, then the other must be the value.
    */
    if ((completeWild->any != curWild->any) && (completeWild->any)) {
      if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
          return(-1);
      return(0);
    }
    /*
    * 3 If either O1 or O2 is a pair of not and a value (a namespace
    * name or ·absent·) and the other is a set of (namespace names or
    * ·absent·), then that set, minus the negated value if it was in
    * the set, minus ·absent· if it was in the set, must be the value.
    */
    if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
      ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
      const xmlChar *neg;

      if (completeWild->nsSet == NULL) {
          neg = completeWild->negNsSet->value;
          if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
            return(-1);
      } else
          neg = curWild->negNsSet->value;
      /*
      * Remove absent and negated.
      */
      prev = NULL;
      cur = completeWild->nsSet;
      while (cur != NULL) {
          if (cur->value == NULL) {
            if (prev == NULL)
                completeWild->nsSet = cur->next;
            else
                prev->next = cur->next;
            xmlFree(cur);
            break;
          }
          prev = cur;
          cur = cur->next;
      }
      if (neg != NULL) {
          prev = NULL;
          cur = completeWild->nsSet;
          while (cur != NULL) {
            if (cur->value == neg) {
                if (prev == NULL)
                  completeWild->nsSet = cur->next;
                else
                  prev->next = cur->next;
                xmlFree(cur);
                break;
            }
            prev = cur;
            cur = cur->next;
          }
      }

      return(0);
    }
    /*
    * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
    * then the intersection of those sets must be the value.
    */
    if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
      int found;

      cur = completeWild->nsSet;
      prev = NULL;
      while (cur != NULL) {
          found = 0;
          curB = curWild->nsSet;
          while (curB != NULL) {
            if (cur->value == curB->value) {
                found = 1;
                break;
            }
            curB = curB->next;
          }
          if (!found) {
            if (prev == NULL)
                completeWild->nsSet = cur->next;
            else
                prev->next = cur->next;
            tmp = cur->next;
            xmlFree(cur);
            cur = tmp;
            continue;
          }
          prev = cur;
          cur = cur->next;
      }

      return(0);
    }
    /* 5 If the two are negations of different namespace names,
    * then the intersection is not expressible
    */
    if ((completeWild->negNsSet != NULL) &&
      (curWild->negNsSet != NULL) &&
      (completeWild->negNsSet->value != curWild->negNsSet->value) &&
      (completeWild->negNsSet->value != NULL) &&
      (curWild->negNsSet->value != NULL)) {

      xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
          "The intersection of the wilcard is not expressible.\n",
          NULL, NULL);
      return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
    }
    /*
    * 6 If the one is a negation of a namespace name and the other
    * is a negation of ·absent·, then the one which is the negation
    * of a namespace name must be the value.
    */
    if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
      (completeWild->negNsSet->value != curWild->negNsSet->value) &&
      (completeWild->negNsSet->value == NULL)) {
      completeWild->negNsSet->value =  curWild->negNsSet->value;
    }
    return(0);
}

/**
 * xmlSchemaIsWildcardNsConstraintSubset:
 * @ctxt:  the schema parser context
 * @sub:  the first wildcard
 * @super: the second wildcard
 *
 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
 *
 * Returns 0 if the namespace constraint of @sub is an intensional
 * subset of @super, 1 otherwise.
 */
static int
xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
                    xmlSchemaWildcardPtr super)
{
    /*
    * 1 super must be any.
    */
    if (super->any)
      return (0);
    /*
    * 2.1 sub must be a pair of not and a namespace name or ·absent·.
    * 2.2 super must be a pair of not and the same value.
    */
    if ((sub->negNsSet != NULL) &&
      (super->negNsSet != NULL) &&
      (sub->negNsSet->value == sub->negNsSet->value))
      return (0);
    /*
    * 3.1 sub must be a set whose members are either namespace names or ·absent·.
    */
    if (sub->nsSet != NULL) {
      /*
      * 3.2.1 super must be the same set or a superset thereof.
      */
      if (super->nsSet != NULL) {
          xmlSchemaWildcardNsPtr cur, curB;
          int found = 0;

          cur = sub->nsSet;
          while (cur != NULL) {
            found = 0;
            curB = super->nsSet;
            while (curB != NULL) {
                if (cur->value == curB->value) {
                  found = 1;
                  break;
                }
                curB = curB->next;
            }
            if (!found)
                return (1);
            cur = cur->next;
          }
          if (found)
            return (0);
      } else if (super->negNsSet != NULL) {
          xmlSchemaWildcardNsPtr cur;
          /*
          * 3.2.2 super must be a pair of not and a namespace name or
          * ·absent· and that value must not be in sub's set.
          */
          cur = sub->nsSet;
          while (cur != NULL) {
            if (cur->value == super->negNsSet->value)
                return (1);
            cur = cur->next;
          }
          return (0);
      }
    }
    return (1);
}

static int
xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
                             int *fixed,
                             const xmlChar **value,
                             xmlSchemaValPtr *val)
{
    *fixed = 0;
    *value = NULL;
    if (val != 0)
      *val = NULL;

    if (attruse->defValue != NULL) {          
      *value = attruse->defValue;
      if (val != NULL)
          *val = attruse->defVal;
      if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
          *fixed = 1;
      return(1);
    } else if ((attruse->attrDecl != NULL) &&
      (attruse->attrDecl->defValue != NULL)) {
      *value = attruse->attrDecl->defValue;
      if (val != NULL)
          *val = attruse->attrDecl->defVal;
      if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
          *fixed = 1;
      return(1);
    }
    return(0);
}
/**
 * xmlSchemaCheckCVCWildcardNamespace:
 * @wild:  the wildcard
 * @ns:  the namespace
 *
 * Validation Rule: Wildcard allows Namespace Name
 * (cvc-wildcard-namespace)
 *
 * Returns 0 if the given namespace matches the wildcard,
 * 1 otherwise and -1 on API errors.
 */
static int
xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
                           const xmlChar* ns)
{
    if (wild == NULL)
      return(-1);

    if (wild->any)
      return(0);
    else if (wild->nsSet != NULL) {
      xmlSchemaWildcardNsPtr cur;

      cur = wild->nsSet;
      while (cur != NULL) {
          if (xmlStrEqual(cur->value, ns))
            return(0);
          cur = cur->next;
      }
    } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
      (!xmlStrEqual(wild->negNsSet->value, ns)))
      return(0);

    return(1);
}

#define XML_SCHEMA_ACTION_DERIVE 0
#define XML_SCHEMA_ACTION_REDEFINE 1

#define WXS_ACTION_STR(a) \
((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"

/*
* Schema Component Constraint:
*   Derivation Valid (Restriction, Complex)
*   derivation-ok-restriction (2) - (4)
*
* ATTENTION:
* In XML Schema 1.1 this will be:
* Validation Rule:
*     Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
*
*/
static int
xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
                               int action,
                               xmlSchemaBasicItemPtr item,
                               xmlSchemaBasicItemPtr baseItem,
                               xmlSchemaItemListPtr uses,
                               xmlSchemaItemListPtr baseUses,
                               xmlSchemaWildcardPtr wild,
                               xmlSchemaWildcardPtr baseWild)
{        
    xmlSchemaAttributeUsePtr cur = NULL, bcur;
    int i, j, found; /* err = 0; */
    const xmlChar *bEffValue;
    int effFixed;
    
    if (uses != NULL) {
      for (i = 0; i < uses->nbItems; i++) {
          cur = uses->items[i];
          found = 0;
          if (baseUses == NULL)
            goto not_found;
          for (j = 0; j < baseUses->nbItems; j++) {
            bcur = baseUses->items[j];    
            if ((WXS_ATTRUSE_DECL_NAME(cur) ==
                  WXS_ATTRUSE_DECL_NAME(bcur)) &&
                (WXS_ATTRUSE_DECL_TNS(cur) ==
                  WXS_ATTRUSE_DECL_TNS(bcur)))
            {
                /*
                * (2.1) "If there is an attribute use in the {attribute
                * uses} of the {base type definition} (call this B) whose
                * {attribute declaration} has the same {name} and {target
                * namespace}, then  all of the following must be true:"
                */
                found = 1;
                
                if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
                  (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
                {
                  xmlChar *str = NULL;
                  /*
                  * (2.1.1) "one of the following must be true:"
                  * (2.1.1.1) "B's {required} is false."
                  * (2.1.1.2) "R's {required} is true."
                  */
                  xmlSchemaPAttrUseErr4(pctxt,
                      XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
                      WXS_ITEM_NODE(item), item, cur,
                      "The 'optional' attribute use is inconsistent "
                      "with the corresponding 'required' attribute use of "
                      "the %s %s",
                      WXS_ACTION_STR(action),
                      xmlSchemaGetComponentDesignation(&str, baseItem),
                      NULL, NULL);
                  FREE_AND_NULL(str);
                  /* err = pctxt->err; */
                } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
                  WXS_ATTRUSE_TYPEDEF(cur),
                  WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
                {
                  xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
                  
                  /*
                  * SPEC (2.1.2) "R's {attribute declaration}'s
                  * {type definition} must be validly derived from
                  * B's {type definition} given the empty set as
                  * defined in Type Derivation OK (Simple) (§3.14.6)."
                  */
                  xmlSchemaPAttrUseErr4(pctxt,
                      XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
                      WXS_ITEM_NODE(item), item, cur,
                      "The attribute declaration's %s "
                      "is not validly derived from "
                      "the corresponding %s of the "
                      "attribute declaration in the %s %s",
                      xmlSchemaGetComponentDesignation(&strA,
                        WXS_ATTRUSE_TYPEDEF(cur)),
                      xmlSchemaGetComponentDesignation(&strB,
                        WXS_ATTRUSE_TYPEDEF(bcur)),
                      WXS_ACTION_STR(action),
                      xmlSchemaGetComponentDesignation(&strC, baseItem));
                      /* xmlSchemaGetComponentDesignation(&str, baseItem), */
                  FREE_AND_NULL(strA);
                  FREE_AND_NULL(strB);
                  FREE_AND_NULL(strC);
                  /* err = pctxt->err; */
                } else {
                  /*
                  * 2.1.3 [Definition:]  Let the effective value
                  * constraint of an attribute use be its {value
                  * constraint}, if present, otherwise its {attribute
                  * declaration}'s {value constraint} .
                  */
                  xmlSchemaGetEffectiveValueConstraint(bcur,
                      &effFixed, &bEffValue, NULL);
                  /*
                  * 2.1.3 ... one of the following must be true
                  *
                  * 2.1.3.1 B's ·effective value constraint· is
                  * ·absent· or default.
                  */
                  if ((bEffValue != NULL) &&
                      (effFixed == 1)) {
                      const xmlChar *rEffValue = NULL;
                      
                      xmlSchemaGetEffectiveValueConstraint(bcur,
                        &effFixed, &rEffValue, NULL);
                      /*
                      * 2.1.3.2 R's ·effective value constraint· is
                      * fixed with the same string as B's.
                      * MAYBE TODO: Compare the computed values.
                      *       Hmm, it says "same string" so
                      *       string-equality might really be sufficient.
                      */
                      if ((effFixed == 0) ||
                        (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
                      {
                        xmlChar *str = NULL;
                        
                        xmlSchemaPAttrUseErr4(pctxt,
                            XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
                            WXS_ITEM_NODE(item), item, cur,
                            "The effective value constraint of the "
                            "attribute use is inconsistent with "
                            "its correspondent in the %s %s",
                            WXS_ACTION_STR(action),
                            xmlSchemaGetComponentDesignation(&str,
                              baseItem),
                            NULL, NULL);
                        FREE_AND_NULL(str);
                        /* err = pctxt->err; */
                      }
                  }
                }
                break;
            }
          }
not_found:  
          if (!found) {
            /*
            * (2.2) "otherwise the {base type definition} must have an
            * {attribute wildcard} and the {target namespace} of the
            * R's {attribute declaration} must be ·valid· with respect
            * to that wildcard, as defined in Wildcard allows Namespace
            * Name (§3.10.4)."
            */
            if ((baseWild == NULL) ||
                (xmlSchemaCheckCVCWildcardNamespace(baseWild,
                (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
            {
                xmlChar *str = NULL;
                
                xmlSchemaPAttrUseErr4(pctxt,
                  XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
                  WXS_ITEM_NODE(item), item, cur,
                  "Neither a matching attribute use, "
                  "nor a matching wildcard exists in the %s %s",
                  WXS_ACTION_STR(action),
                  xmlSchemaGetComponentDesignation(&str, baseItem),
                  NULL, NULL);
                FREE_AND_NULL(str);
                /* err = pctxt->err; */
            }
          }
      }
    }
    /*
    * SPEC derivation-ok-restriction (3):    
    * (3) "For each attribute use in the {attribute uses} of the {base type
    * definition} whose {required} is true, there must be an attribute
    * use with an {attribute declaration} with the same {name} and
    * {target namespace} as its {attribute declaration} in the {attribute
    * uses} of the complex type definition itself whose {required} is true.
    */
    if (baseUses != NULL) {
      for (j = 0; j < baseUses->nbItems; j++) {
          bcur = baseUses->items[j];
          if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
            continue;
          found = 0;
          if (uses != NULL) {
            for (i = 0; i < uses->nbItems; i++) {
                cur = uses->items[i];     
                if ((WXS_ATTRUSE_DECL_NAME(cur) ==
                  WXS_ATTRUSE_DECL_NAME(bcur)) &&
                  (WXS_ATTRUSE_DECL_TNS(cur) ==
                  WXS_ATTRUSE_DECL_TNS(bcur))) {
                  found = 1;
                  break;
                }
            }
          }
          if (!found) {
            xmlChar *strA = NULL, *strB = NULL;
            
            xmlSchemaCustomErr4(ACTXT_CAST pctxt,
                XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
                NULL, item,
                "A matching attribute use for the "
                "'required' %s of the %s %s is missing",
                xmlSchemaGetComponentDesignation(&strA, bcur),
                WXS_ACTION_STR(action),
                xmlSchemaGetComponentDesignation(&strB, baseItem),
                NULL);
            FREE_AND_NULL(strA);
            FREE_AND_NULL(strB);
          }
      }
    }
    /*
    * derivation-ok-restriction (4)
    */
    if (wild != NULL) {
      /*
      * (4) "If there is an {attribute wildcard}, all of the
      * following must be true:"
      */   
      if (baseWild == NULL) {
          xmlChar *str = NULL;

          /*
          * (4.1) "The {base type definition} must also have one."
          */          
          xmlSchemaCustomErr4(ACTXT_CAST pctxt,
            XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
            NULL, item,
            "The %s has an attribute wildcard, "
            "but the %s %s '%s' does not have one",
            WXS_ITEM_TYPE_NAME(item),               
            WXS_ACTION_STR(action),
            WXS_ITEM_TYPE_NAME(baseItem),
            xmlSchemaGetComponentQName(&str, baseItem));
          FREE_AND_NULL(str);
          return(pctxt->err);
      } else if ((baseWild->any == 0) &&
            xmlSchemaCheckCOSNSSubset(wild, baseWild))
      {
          xmlChar *str = NULL;
          /*
          * (4.2) "The complex type definition's {attribute wildcard}'s
          * {namespace constraint} must be a subset of the {base type
          * definition}'s {attribute wildcard}'s {namespace constraint},
          * as defined by Wildcard Subset (§3.10.6)."
          */
          xmlSchemaCustomErr4(ACTXT_CAST pctxt,
            XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
            NULL, item,
            "The attribute wildcard is not a valid "
            "subset of the wildcard in the %s %s '%s'",
            WXS_ACTION_STR(action),
            WXS_ITEM_TYPE_NAME(baseItem),
            xmlSchemaGetComponentQName(&str, baseItem),
            NULL);
          FREE_AND_NULL(str);
          return(pctxt->err);
      }
      /* 4.3 Unless the {base type definition} is the ·ur-type
      * definition·, the complex type definition's {attribute
      * wildcard}'s {process contents} must be identical to or
      * stronger than the {base type definition}'s {attribute
      * wildcard}'s {process contents}, where strict is stronger
      * than lax is stronger than skip.
      */
      if ((! WXS_IS_ANYTYPE(baseItem)) &&
          (wild->processContents < baseWild->processContents)) {
          xmlChar *str = NULL;
          xmlSchemaCustomErr4(ACTXT_CAST pctxt,
            XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
            NULL, baseItem,
            "The {process contents} of the attribute wildcard is "
            "weaker than the one in the %s %s '%s'",
            WXS_ACTION_STR(action),
            WXS_ITEM_TYPE_NAME(baseItem),
            xmlSchemaGetComponentQName(&str, baseItem),
            NULL);
          FREE_AND_NULL(str)
            return(pctxt->err);
      }
    }
    return(0);
}


static int
xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
                          xmlSchemaBasicItemPtr item,
                          xmlSchemaWildcardPtr *completeWild,
                          xmlSchemaItemListPtr list,
                          xmlSchemaItemListPtr prohibs);
/**
 * xmlSchemaFixupTypeAttributeUses:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 *
 *
 * Builds the wildcard and the attribute uses on the given complex type.
 * Returns -1 if an internal error occurs, 0 otherwise.
 *
 * ATTENTION TODO: Experimantally this uses pointer comparisons for
 * strings, so recheck this if we start to hardcode some schemata, since
 * they might not be in the same dict.
 * NOTE: It is allowed to "extend" the xs:anyType type.
 */
static int
xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
                          xmlSchemaTypePtr type)
{
    xmlSchemaTypePtr baseType = NULL;
    xmlSchemaAttributeUsePtr use;    
    xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;

    if (type->baseType == NULL) {
      PERROR_INT("xmlSchemaFixupTypeAttributeUses",
          "no base type");
        return (-1);
    }
    baseType = type->baseType;        
    if (WXS_IS_TYPE_NOT_FIXED(baseType))
      if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
          return(-1);

    uses = type->attrUses;
    baseUses = baseType->attrUses;
    /*
    * Expand attribute group references. And build the 'complete'
    * wildcard, i.e. intersect multiple wildcards.
    * Move attribute prohibitions into a separate list.
    */
    if (uses != NULL) { 
      if (WXS_IS_RESTRICTION(type)) {
          /*
          * This one will transfer all attr. prohibitions
          * into pctxt->attrProhibs.
          */
          if (xmlSchemaExpandAttributeGroupRefs(pctxt,
            WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
            pctxt->attrProhibs) == -1)
          {
            PERROR_INT("xmlSchemaFixupTypeAttributeUses",
            "failed to expand attributes");
          }
          if (pctxt->attrProhibs->nbItems != 0)
            prohibs = pctxt->attrProhibs;
      } else {
          if (xmlSchemaExpandAttributeGroupRefs(pctxt,
            WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
            NULL) == -1)
          {
            PERROR_INT("xmlSchemaFixupTypeAttributeUses",
            "failed to expand attributes");
          }
      }
    }
    /*
    * Inherit the attribute uses of the base type.
    */
    if (baseUses != NULL) {
      int i, j;
      xmlSchemaAttributeUseProhibPtr pro;

      if (WXS_IS_RESTRICTION(type)) {
          int usesCount;
          xmlSchemaAttributeUsePtr tmp; 

          if (uses != NULL)
            usesCount = uses->nbItems;
          else
            usesCount = 0;

          /* Restriction. */
          for (i = 0; i < baseUses->nbItems; i++) {
            use = baseUses->items[i];
            if (prohibs) {
                /*
                * Filter out prohibited uses.
                */
                for (j = 0; j < prohibs->nbItems; j++) {
                  pro = prohibs->items[j];
                  if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
                      (WXS_ATTRUSE_DECL_TNS(use) ==
                        pro->targetNamespace))
                  {
                      goto inherit_next;
                  }
                }
            }
            if (usesCount) {
                /*
                * Filter out existing uses.
                */
                for (j = 0; j < usesCount; j++) {
                  tmp = uses->items[j];
                  if ((WXS_ATTRUSE_DECL_NAME(use) ==
                        WXS_ATTRUSE_DECL_NAME(tmp)) &&
                      (WXS_ATTRUSE_DECL_TNS(use) ==
                        WXS_ATTRUSE_DECL_TNS(tmp)))
                  {
                      goto inherit_next;
                  }
                }
            }
            if (uses == NULL) {
                type->attrUses = xmlSchemaItemListCreate();
                if (type->attrUses == NULL)
                  goto exit_failure;
                uses = type->attrUses;
            }
            xmlSchemaItemListAddSize(uses, 2, use);
inherit_next: {}
          }
      } else {
          /* Extension. */
          for (i = 0; i < baseUses->nbItems; i++) {       
            use = baseUses->items[i];           
            if (uses == NULL) {
                type->attrUses = xmlSchemaItemListCreate();
                if (type->attrUses == NULL)
                  goto exit_failure;
                uses = type->attrUses;
            }
            xmlSchemaItemListAddSize(uses, baseUses->nbItems, use); 
          }
      }
    }
    /*
    * Shrink attr. uses.
    */
    if (uses) {
      if (uses->nbItems == 0) {
          xmlSchemaItemListFree(uses);
          type->attrUses = NULL;
      }
      /*
      * TODO: We could shrink the size of the array
      * to fit the actual number of items.
      */
    }
    /*
    * Compute the complete wildcard.
    */
    if (WXS_IS_EXTENSION(type)) {   
      if (baseType->attributeWildcard != NULL) {
          /*
          * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
          * the appropriate case among the following:"
          */
          if (type->attributeWildcard != NULL) {
            /*
            * Union the complete wildcard with the base wildcard.
            * SPEC {attribute wildcard}
            * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
            * and {annotation} are those of the ·complete wildcard·,
            * and whose {namespace constraint} is the intensional union
            * of the {namespace constraint} of the ·complete wildcard·
            * and of the ·base wildcard·, as defined in Attribute
            * Wildcard Union (§3.10.6)."
            */
            if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
                baseType->attributeWildcard) == -1)
                goto exit_failure;        
          } else {
            /*
            * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
            * then the ·base wildcard·."
            */
            type->attributeWildcard = baseType->attributeWildcard;
          }  
      } else {
          /*
          * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
          * ·complete wildcard"
          * NOOP
          */
      }
    } else {
      /*
      * SPEC {attribute wildcard}
      * (3.1) "If the <restriction> alternative is chosen, then the
      * ·complete wildcard·;"
      * NOOP
      */
    }
    
    return (0);

exit_failure:
    return(-1);
}

/**
 * xmlSchemaTypeFinalContains:
 * @schema:  the schema
 * @type:  the type definition
 * @final: the final
 *
 * Evaluates if a type definition contains the given "final".
 * This does take "finalDefault" into account as well.
 *
 * Returns 1 if the type does containt the given "final",
 * 0 otherwise.
 */
static int
xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
{
    if (type == NULL)
      return (0);
    if (type->flags & final)
      return (1);
    else
      return (0);
}

/**
 * xmlSchemaGetUnionSimpleTypeMemberTypes:
 * @type:  the Union Simple Type
 *
 * Returns a list of member types of @type if existing,
 * returns NULL otherwise.
 */
static xmlSchemaTypeLinkPtr
xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
{
    while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
      if (type->memberTypes != NULL)
          return (type->memberTypes);
      else
          type = type->baseType;
    }
    return (NULL);
}

/**
 * xmlSchemaGetParticleTotalRangeMin:
 * @particle: the particle
 *
 * Schema Component Constraint: Effective Total Range
 * (all and sequence) + (choice)
 *
 * Returns the minimun Effective Total Range.
 */
static int
xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
{
    if ((particle->children == NULL) ||
      (particle->minOccurs == 0))
      return (0);
    if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
      int min = -1, cur;
      xmlSchemaParticlePtr part =
          (xmlSchemaParticlePtr) particle->children->children;

      if (part == NULL)
          return (0);
      while (part != NULL) {
          if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
            (part->children->type == XML_SCHEMA_TYPE_ANY))
            cur = part->minOccurs;
          else
            cur = xmlSchemaGetParticleTotalRangeMin(part);
          if (cur == 0)
            return (0);
          if ((min > cur) || (min == -1))
            min = cur;
          part = (xmlSchemaParticlePtr) part->next;
      }
      return (particle->minOccurs * min);
    } else {
      /* <all> and <sequence> */
      int sum = 0;
      xmlSchemaParticlePtr part =
          (xmlSchemaParticlePtr) particle->children->children;

      if (part == NULL)
          return (0);
      do {
          if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
            (part->children->type == XML_SCHEMA_TYPE_ANY))
            sum += part->minOccurs;
          else
            sum += xmlSchemaGetParticleTotalRangeMin(part);
          part = (xmlSchemaParticlePtr) part->next;
      } while (part != NULL);
      return (particle->minOccurs * sum);
    }
}

/**
 * xmlSchemaGetParticleTotalRangeMax:
 * @particle: the particle
 *
 * Schema Component Constraint: Effective Total Range
 * (all and sequence) + (choice)
 *
 * Returns the maximum Effective Total Range.
 */
static int
xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
{
    if ((particle->children == NULL) ||
      (particle->children->children == NULL))
      return (0);
    if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
      int max = -1, cur;
      xmlSchemaParticlePtr part =
          (xmlSchemaParticlePtr) particle->children->children;

      for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
          if (part->children == NULL)
            continue;
          if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
            (part->children->type == XML_SCHEMA_TYPE_ANY))
            cur = part->maxOccurs;
          else
            cur = xmlSchemaGetParticleTotalRangeMax(part);
          if (cur == UNBOUNDED)
            return (UNBOUNDED);
          if ((max < cur) || (max == -1))
            max = cur;
      }
      /* TODO: Handle overflows? */
      return (particle->maxOccurs * max);
    } else {
      /* <all> and <sequence> */
      int sum = 0, cur;
      xmlSchemaParticlePtr part =
          (xmlSchemaParticlePtr) particle->children->children;

      for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
          if (part->children == NULL)
            continue;
          if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
            (part->children->type == XML_SCHEMA_TYPE_ANY))
            cur = part->maxOccurs;
          else
            cur = xmlSchemaGetParticleTotalRangeMax(part);
          if (cur == UNBOUNDED)
            return (UNBOUNDED);
          if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
            return (UNBOUNDED);
          sum += cur;
      }
      /* TODO: Handle overflows? */
      return (particle->maxOccurs * sum);
    }
}

/**
 * xmlSchemaIsParticleEmptiable:
 * @particle: the particle
 *
 * Schema Component Constraint: Particle Emptiable
 * Checks whether the given particle is emptiable.
 *
 * Returns 1 if emptiable, 0 otherwise.
 */
static int
xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
{
    /*
    * SPEC (1) "Its {min occurs} is 0."
    */
    if ((particle == NULL) || (particle->minOccurs == 0) ||
      (particle->children == NULL))
      return (1);
    /*
    * SPEC (2) "Its {term} is a group and the minimum part of the
    * effective total range of that group, [...] is 0."
    */
    if (WXS_IS_MODEL_GROUP(particle->children)) {
      if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
          return (1);
    }
    return (0);
}

/**
 * xmlSchemaCheckCOSSTDerivedOK:
 * @actxt: a context
 * @type:  the derived simple type definition
 * @baseType:  the base type definition
 * @subset: the subset of ('restriction', ect.)
 *
 * Schema Component Constraint:
 * Type Derivation OK (Simple) (cos-st-derived-OK)
 *
 * Checks wheter @type can be validly
 * derived from @baseType.
 *
 * Returns 0 on success, an positive error code otherwise.
 */
static int
xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
                       xmlSchemaTypePtr type,
                       xmlSchemaTypePtr baseType,
                       int subset)
{
    /*
    * 1 They are the same type definition.
    * TODO: The identy check might have to be more complex than this.
    */
    if (type == baseType)
      return (0);
    /*
    * 2.1 restriction is not in the subset, or in the {final}
    * of its own {base type definition};
    *
    * NOTE that this will be used also via "xsi:type".
    *
    * TODO: Revise this, it looks strange. How can the "type"
    * not be fixed or *in* fixing?
    */
    if (WXS_IS_TYPE_NOT_FIXED(type))
      if (xmlSchemaTypeFixup(type, actxt) == -1)
          return(-1);
    if (WXS_IS_TYPE_NOT_FIXED(baseType))
      if (xmlSchemaTypeFixup(baseType, actxt) == -1)
          return(-1);
    if ((subset & SUBSET_RESTRICTION) ||
      (xmlSchemaTypeFinalContains(type->baseType,
          XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
      return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
    }
    /* 2.2 */
    if (type->baseType == baseType) {
      /*
      * 2.2.1 D's ·base type definition· is B.
      */
      return (0);
    }
    /*
    * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
    * and is validly derived from B given the subset, as defined by this
    * constraint.
    */
    if ((! WXS_IS_ANYTYPE(type->baseType)) &&
      (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
          baseType, subset) == 0)) {
      return (0);
    }
    /*
    * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
    * definition·.
    */
    if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
      (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
      return (0);
    }
    /*
    * 2.2.4 B's {variety} is union and D is validly derived from a type
    * definition in B's {member type definitions} given the subset, as
    * defined by this constraint.
    *
    * NOTE: This seems not to involve built-in types, since there is no
    * built-in Union Simple Type.
    */
    if (WXS_IS_UNION(baseType)) {
      xmlSchemaTypeLinkPtr cur;

      cur = baseType->memberTypes;
      while (cur != NULL) {
          if (WXS_IS_TYPE_NOT_FIXED(cur->type))
            if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
                return(-1);
          if (xmlSchemaCheckCOSSTDerivedOK(actxt,
                type, cur->type, subset) == 0)
          {
            /*
            * It just has to be validly derived from at least one
            * member-type.
            */
            return (0);
          }
          cur = cur->next;
      }
    }
    return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
}

/**
 * xmlSchemaCheckTypeDefCircularInternal:
 * @pctxt:  the schema parser context
 * @ctxtType:  the type definition
 * @ancestor: an ancestor of @ctxtType
 *
 * Checks st-props-correct (2) + ct-props-correct (3).
 * Circular type definitions are not allowed.
 *
 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
 * circular, 0 otherwise.
 */
static int
xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
                     xmlSchemaTypePtr ctxtType,
                     xmlSchemaTypePtr ancestor)
{
    int ret;

    if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
      return (0);

    if (ctxtType == ancestor) {
      xmlSchemaPCustomErr(pctxt,
          XML_SCHEMAP_ST_PROPS_CORRECT_2,
          WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
          "The definition is circular", NULL);
      return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
    }
    if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
      /*
      * Avoid inifinite recursion on circular types not yet checked.
      */
      return (0);
    }
    ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
    ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
      ancestor->baseType);
    ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
    return (ret);
}

/**
 * xmlSchemaCheckTypeDefCircular:
 * @item:  the complex/simple type definition
 * @ctxt:  the parser context
 * @name:  the name
 *
 * Checks for circular type definitions.
 */
static void
xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
                        xmlSchemaParserCtxtPtr ctxt)
{
    if ((item == NULL) ||
      (item->type == XML_SCHEMA_TYPE_BASIC) ||
      (item->baseType == NULL))
      return;
    xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
      item->baseType);
}

/*
* Simple Type Definition Representation OK (src-simple-type) 4
*
* "4 Circular union type definition is disallowed. That is, if the
* <union> alternative is chosen, there must not be any entries in the
* memberTypes [attribute] at any depth which resolve to the component
* corresponding to the <simpleType>."
*
* Note that this should work on the *representation* of a component,
* thus assumes any union types in the member types not being yet
* substituted. At this stage we need the variety of the types
* to be already computed.
*/
static int
xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
                              xmlSchemaTypePtr ctxType,
                              xmlSchemaTypeLinkPtr members)
{    
    xmlSchemaTypeLinkPtr member;
    xmlSchemaTypePtr memberType;
    
    member = members;
    while (member != NULL) {
      memberType = member->type;
      while ((memberType != NULL) &&
          (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
          if (memberType == ctxType) {
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
                WXS_BASIC_CAST ctxType, NULL,
                "The union type definition is circular", NULL);
            return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
          }
          if ((WXS_IS_UNION(memberType)) &&
            ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
          {
            int res;
            memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
            res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
                ctxType,
                xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
            memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
            if (res != 0)
                return(res);
          }
          memberType = memberType->baseType;
      }
      member = member->next;
    }
    return(0);
}

static int
xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
                           xmlSchemaTypePtr type)
{
    if (! WXS_IS_UNION(type))
      return(0);
    return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
      type->memberTypes));
}

/**
 * xmlSchemaResolveTypeReferences:
 * @item:  the complex/simple type definition
 * @ctxt:  the parser context
 * @name:  the name
 *
 * Resolvese type definition references
 */
static void
xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
                   xmlSchemaParserCtxtPtr ctxt)
{
    if (typeDef == NULL)
      return;

    /*
    * Resolve the base type.
    */
    if (typeDef->baseType == NULL) {
      typeDef->baseType = xmlSchemaGetType(ctxt->schema,
          typeDef->base, typeDef->baseNs);
      if (typeDef->baseType == NULL) {
          xmlSchemaPResCompAttrErr(ctxt,
            XML_SCHEMAP_SRC_RESOLVE,
            WXS_BASIC_CAST typeDef, typeDef->node,
            "base", typeDef->base, typeDef->baseNs,
            XML_SCHEMA_TYPE_SIMPLE, NULL);
          return;
      }
    }
    if (WXS_IS_SIMPLE(typeDef)) {
      if (WXS_IS_UNION(typeDef)) {
          /*
          * Resolve the memberTypes.
          */
          xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
          return;
      } else if (WXS_IS_LIST(typeDef)) {
          /*
          * Resolve the itemType.
          */
          if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {

            typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
                typeDef->base, typeDef->baseNs);

            if ((typeDef->subtypes == NULL) ||
                (! WXS_IS_SIMPLE(typeDef->subtypes)))
            {
                typeDef->subtypes = NULL;
                xmlSchemaPResCompAttrErr(ctxt,
                  XML_SCHEMAP_SRC_RESOLVE,
                  WXS_BASIC_CAST typeDef, typeDef->node,
                  "itemType", typeDef->base, typeDef->baseNs,
                  XML_SCHEMA_TYPE_SIMPLE, NULL);
            }
          }
          return;
      }
    } 
    /*
    * The ball of letters below means, that if we have a particle
    * which has a QName-helper component as its {term}, we want
    * to resolve it...
    */
    else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
      ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
          XML_SCHEMA_TYPE_PARTICLE) &&
      (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
      ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
          XML_SCHEMA_EXTRA_QNAMEREF))
    {
      xmlSchemaQNameRefPtr ref =
          WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
      xmlSchemaModelGroupDefPtr groupDef;

      /*
      * URGENT TODO: Test this.
      */
      WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
      /*
      * Resolve the MG definition reference.
      */
      groupDef =
          WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
            ref->itemType, ref->name, ref->targetNamespace);
      if (groupDef == NULL) {
          xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
            NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
            "ref", ref->name, ref->targetNamespace, ref->itemType,
            NULL);
          /* Remove the particle. */
          WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
      } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
          /* Remove the particle. */
          WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
      else {
          /*
          * Assign the MG definition's {model group} to the
          * particle's {term}.
          */
          WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
          
          if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
            /*
            * SPEC cos-all-limited (1.2)
            * "1.2 the {term} property of a particle with
            * {max occurs}=1 which is part of a pair which constitutes
            * the {content type} of a complex type definition."
            */
            if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
                xmlSchemaCustomErr(ACTXT_CAST ctxt,
                  /* TODO: error code */
                  XML_SCHEMAP_COS_ALL_LIMITED,
                  WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
                  "The particle's {max occurs} must be 1, since the "
                  "reference resolves to an 'all' model group",
                  NULL, NULL);
            }
          }
      }
    }
}



/**
 * xmlSchemaCheckSTPropsCorrect:
 * @ctxt:  the schema parser context
 * @type:  the simple type definition
 *
 * Checks st-props-correct.
 *
 * Returns 0 if the properties are correct,
 * if not, a positive error code and -1 on internal
 * errors.
 */
static int
xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
                       xmlSchemaTypePtr type)
{
    xmlSchemaTypePtr baseType = type->baseType;
    xmlChar *str = NULL;

    /* STATE: error funcs converted. */
    /*
    * Schema Component Constraint: Simple Type Definition Properties Correct
    *
    * NOTE: This is somehow redundant, since we actually built a simple type
    * to have all the needed information; this acts as an self test.
    */
    /* Base type: If the datatype has been ·derived· by ·restriction·
    * then the Simple Type Definition component from which it is ·derived·,
    * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
    */
    if (baseType == NULL) {
      /*
      * TODO: Think about: "modulo the impact of Missing
      * Sub-components (§5.3)."
      */
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_ST_PROPS_CORRECT_1,
          WXS_BASIC_CAST type, NULL,
          "No base type existent", NULL);
      return (XML_SCHEMAP_ST_PROPS_CORRECT_1);

    }
    if (! WXS_IS_SIMPLE(baseType)) {
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_ST_PROPS_CORRECT_1,
          WXS_BASIC_CAST type, NULL,
          "The base type '%s' is not a simple type",
          xmlSchemaGetComponentQName(&str, baseType));
      FREE_AND_NULL(str)
      return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
    }
    if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
       (WXS_IS_RESTRICTION(type) == 0) &&
       (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_ST_PROPS_CORRECT_1,
          WXS_BASIC_CAST type, NULL,
          "A type, derived by list or union, must have"
          "the simple ur-type definition as base type, not '%s'",
          xmlSchemaGetComponentQName(&str, baseType));
      FREE_AND_NULL(str)
      return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
    }
    /*
    * Variety: One of {atomic, list, union}.
    */
    if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
      (! WXS_IS_LIST(type))) {
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_ST_PROPS_CORRECT_1,
          WXS_BASIC_CAST type, NULL,
          "The variety is absent", NULL);
      return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
    }
    /* TODO: Finish this. Hmm, is this finished? */

    /*
    * 3 The {final} of the {base type definition} must not contain restriction.
    */
    if (xmlSchemaTypeFinalContains(baseType,
      XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_ST_PROPS_CORRECT_3,
          WXS_BASIC_CAST type, NULL,
          "The 'final' of its base type '%s' must not contain "
          "'restriction'",
          xmlSchemaGetComponentQName(&str, baseType));
      FREE_AND_NULL(str)
      return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
    }

    /*
    * 2 All simple type definitions must be derived ultimately from the ·simple
    * ur-type definition (so· circular definitions are disallowed). That is, it
    * must be possible to reach a built-in primitive datatype or the ·simple
    * ur-type definition· by repeatedly following the {base type definition}.
    *
    * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
    */
    return (0);
}

/**
 * xmlSchemaCheckCOSSTRestricts:
 * @ctxt:  the schema parser context
 * @type:  the simple type definition
 *
 * Schema Component Constraint:
 * Derivation Valid (Restriction, Simple) (cos-st-restricts)

 * Checks if the given @type (simpleType) is derived validly by restriction.
 * STATUS:
 *
 * Returns -1 on internal errors, 0 if the type is validly derived,
 * a positive error code otherwise.
 */
static int
xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
                       xmlSchemaTypePtr type)
{
    xmlChar *str = NULL;

    if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
      PERROR_INT("xmlSchemaCheckCOSSTRestricts",
          "given type is not a user-derived simpleType");
      return (-1);
    }

    if (WXS_IS_ATOMIC(type)) {
      xmlSchemaTypePtr primitive;
      /*
      * 1.1 The {base type definition} must be an atomic simple
      * type definition or a built-in primitive datatype.
      */
      if (! WXS_IS_ATOMIC(type->baseType)) {
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
            WXS_BASIC_CAST type, NULL,
            "The base type '%s' is not an atomic simple type",
            xmlSchemaGetComponentQName(&str, type->baseType));
          FREE_AND_NULL(str)
          return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
      }
      /* 1.2 The {final} of the {base type definition} must not contain
      * restriction.
      */
      /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
      if (xmlSchemaTypeFinalContains(type->baseType,
          XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
            WXS_BASIC_CAST type, NULL,
            "The final of its base type '%s' must not contain 'restriction'",
            xmlSchemaGetComponentQName(&str, type->baseType));
          FREE_AND_NULL(str)
          return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
      }

      /*
      * 1.3.1 DF must be an allowed constraining facet for the {primitive
      * type definition}, as specified in the appropriate subsection of 3.2
      * Primitive datatypes.
      */
      if (type->facets != NULL) {
          xmlSchemaFacetPtr facet;
          int ok = 1;

          primitive = xmlSchemaGetPrimitiveType(type);
          if (primitive == NULL) {
            PERROR_INT("xmlSchemaCheckCOSSTRestricts",
                "failed to get primitive type");
            return (-1);
          }
          facet = type->facets;
          do {
            if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
                ok = 0;
                xmlSchemaPIllegalFacetAtomicErr(pctxt,
                  XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
                  type, primitive, facet);
            }
            facet = facet->next;
          } while (facet != NULL);
          if (ok == 0)
            return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
      }
      /*
      * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
      * of the {base type definition} (call this BF),then the DF's {value}
      * must be a valid restriction of BF's {value} as defined in
      * [XML Schemas: Datatypes]."
      *
      * NOTE (1.3.2) Facet derivation constraints are currently handled in
      * xmlSchemaDeriveAndValidateFacets()
      */
    } else if (WXS_IS_LIST(type)) {
      xmlSchemaTypePtr itemType = NULL;

      itemType = type->subtypes;
      if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
          PERROR_INT("xmlSchemaCheckCOSSTRestricts",
            "failed to evaluate the item type");
          return (-1);
      }
      if (WXS_IS_TYPE_NOT_FIXED(itemType))
          xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
      /*
      * 2.1 The {item type definition} must have a {variety} of atomic or
      * union (in which case all the {member type definitions}
      * must be atomic).
      */
      if ((! WXS_IS_ATOMIC(itemType)) &&
          (! WXS_IS_UNION(itemType))) {
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
            WXS_BASIC_CAST type, NULL,
            "The item type '%s' does not have a variety of atomic or union",
            xmlSchemaGetComponentQName(&str, itemType));
          FREE_AND_NULL(str)
          return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
      } else if (WXS_IS_UNION(itemType)) {
          xmlSchemaTypeLinkPtr member;

          member = itemType->memberTypes;
          while (member != NULL) {
            if (! WXS_IS_ATOMIC(member->type)) {
                xmlSchemaPCustomErr(pctxt,
                  XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
                  WXS_BASIC_CAST type, NULL,
                  "The item type is a union type, but the "
                  "member type '%s' of this item type is not atomic",
                  xmlSchemaGetComponentQName(&str, member->type));
                FREE_AND_NULL(str)
                return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
            }
            member = member->next;
          }
      }

      if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
          xmlSchemaFacetPtr facet;
          /*
          * This is the case if we have: <simpleType><list ..
          */
          /*
          * 2.3.1
          * 2.3.1.1 The {final} of the {item type definition} must not
          * contain list.
          */
          if (xmlSchemaTypeFinalContains(itemType,
            XML_SCHEMAS_TYPE_FINAL_LIST)) {
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
                WXS_BASIC_CAST type, NULL,
                "The final of its item type '%s' must not contain 'list'",
                xmlSchemaGetComponentQName(&str, itemType));
            FREE_AND_NULL(str)
            return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
          }
          /*
          * 2.3.1.2 The {facets} must only contain the whiteSpace
          * facet component.
          * OPTIMIZE TODO: the S4S already disallows any facet
          * to be specified.
          */
          if (type->facets != NULL) {
            facet = type->facets;
            do {
                if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
                  xmlSchemaPIllegalFacetListUnionErr(pctxt,
                      XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
                      type, facet);
                  return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
                }
                facet = facet->next;
            } while (facet != NULL);
          }
          /*
          * MAYBE TODO: (Hmm, not really) Datatypes states:
          * A ·list· datatype can be ·derived· from an ·atomic· datatype
          * whose ·lexical space· allows space (such as string or anyURI)or
          * a ·union· datatype any of whose {member type definitions}'s
          * ·lexical space· allows space.
          */
      } else {
          /*
          * This is the case if we have: <simpleType><restriction ...
          * I.e. the variety of "list" is inherited.
          */
          /*
          * 2.3.2
          * 2.3.2.1 The {base type definition} must have a {variety} of list.
          */
          if (! WXS_IS_LIST(type->baseType)) {
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
                WXS_BASIC_CAST type, NULL,
                "The base type '%s' must be a list type",
                xmlSchemaGetComponentQName(&str, type->baseType));
            FREE_AND_NULL(str)
            return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
          }
          /*
          * 2.3.2.2 The {final} of the {base type definition} must not
          * contain restriction.
          */
          if (xmlSchemaTypeFinalContains(type->baseType,
            XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
                WXS_BASIC_CAST type, NULL,
                "The 'final' of the base type '%s' must not contain 'restriction'",
                xmlSchemaGetComponentQName(&str, type->baseType));
            FREE_AND_NULL(str)
            return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
          }
          /*
          * 2.3.2.3 The {item type definition} must be validly derived
          * from the {base type definition}'s {item type definition} given
          * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
          */
          {
            xmlSchemaTypePtr baseItemType;

            baseItemType = type->baseType->subtypes;
            if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
                PERROR_INT("xmlSchemaCheckCOSSTRestricts",
                  "failed to eval the item type of a base type");
                return (-1);
            }
            if ((itemType != baseItemType) &&
                (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
                  baseItemType, 0) != 0)) {
                xmlChar *strBIT = NULL, *strBT = NULL;
                xmlSchemaPCustomErrExt(pctxt,
                  XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
                  WXS_BASIC_CAST type, NULL,
                  "The item type '%s' is not validly derived from "
                  "the item type '%s' of the base type '%s'",
                  xmlSchemaGetComponentQName(&str, itemType),
                  xmlSchemaGetComponentQName(&strBIT, baseItemType),
                  xmlSchemaGetComponentQName(&strBT, type->baseType));

                FREE_AND_NULL(str)
                FREE_AND_NULL(strBIT)
                FREE_AND_NULL(strBT)
                return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
            }
          }

          if (type->facets != NULL) {
            xmlSchemaFacetPtr facet;
            int ok = 1;
            /*
            * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
            * and enumeration facet components are allowed among the {facets}.
            */
            facet = type->facets;
            do {
                switch (facet->type) {
                  case XML_SCHEMA_FACET_LENGTH:
                  case XML_SCHEMA_FACET_MINLENGTH:
                  case XML_SCHEMA_FACET_MAXLENGTH:
                  case XML_SCHEMA_FACET_WHITESPACE:
                      /*
                      * TODO: 2.5.1.2 List datatypes
                      * The value of ·whiteSpace· is fixed to the value collapse.
                      */
                  case XML_SCHEMA_FACET_PATTERN:
                  case XML_SCHEMA_FACET_ENUMERATION:
                      break;
                  default: {
                      xmlSchemaPIllegalFacetListUnionErr(pctxt,
                        XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
                        type, facet);
                      /*
                      * We could return, but it's nicer to report all
                      * invalid facets.
                      */
                      ok = 0;
                  }
                }
                facet = facet->next;
            } while (facet != NULL);
            if (ok == 0)
                return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
            /*
            * SPEC (2.3.2.5) (same as 1.3.2)
            *
            * NOTE (2.3.2.5) This is currently done in
            * xmlSchemaDeriveAndValidateFacets()
            */
          }
      }
    } else if (WXS_IS_UNION(type)) {
      /*
      * 3.1 The {member type definitions} must all have {variety} of
      * atomic or list.
      */
      xmlSchemaTypeLinkPtr member;

      member = type->memberTypes;
      while (member != NULL) {
          if (WXS_IS_TYPE_NOT_FIXED(member->type))
            xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);

          if ((! WXS_IS_ATOMIC(member->type)) &&
            (! WXS_IS_LIST(member->type))) {
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
                WXS_BASIC_CAST type, NULL,
                "The member type '%s' is neither an atomic, nor a list type",
                xmlSchemaGetComponentQName(&str, member->type));
            FREE_AND_NULL(str)
            return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
          }
          member = member->next;
      }
      /*
      * 3.3.1 If the {base type definition} is the ·simple ur-type
      * definition·
      */
      if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
          /*
          * 3.3.1.1 All of the {member type definitions} must have a
          * {final} which does not contain union.
          */
          member = type->memberTypes;
          while (member != NULL) {
            if (xmlSchemaTypeFinalContains(member->type,
                XML_SCHEMAS_TYPE_FINAL_UNION)) {
                xmlSchemaPCustomErr(pctxt,
                  XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
                  WXS_BASIC_CAST type, NULL,
                  "The 'final' of member type '%s' contains 'union'",
                  xmlSchemaGetComponentQName(&str, member->type));
                FREE_AND_NULL(str)
                return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
            }
            member = member->next;
          }
          /*
          * 3.3.1.2 The {facets} must be empty.
          */
          if (type->facetSet != NULL) {
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
                WXS_BASIC_CAST type, NULL,
                "No facets allowed", NULL);
            return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
          }
      } else {
          /*
          * 3.3.2.1 The {base type definition} must have a {variety} of union.
          * I.e. the variety of "list" is inherited.
          */
          if (! WXS_IS_UNION(type->baseType)) {
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
                WXS_BASIC_CAST type, NULL,
                "The base type '%s' is not a union type",
                xmlSchemaGetComponentQName(&str, type->baseType));
            FREE_AND_NULL(str)
            return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
          }
          /*
          * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
          */
          if (xmlSchemaTypeFinalContains(type->baseType,
            XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
                WXS_BASIC_CAST type, NULL,
                "The 'final' of its base type '%s' must not contain 'restriction'",
                xmlSchemaGetComponentQName(&str, type->baseType));
            FREE_AND_NULL(str)
            return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
          }
          /*
          * 3.3.2.3 The {member type definitions}, in order, must be validly
          * derived from the corresponding type definitions in the {base
          * type definition}'s {member type definitions} given the empty set,
          * as defined in Type Derivation OK (Simple) (§3.14.6).
          */
          {
            xmlSchemaTypeLinkPtr baseMember;

            /*
            * OPTIMIZE: if the type is restricting, it has no local defined
            * member types and inherits the member types of the base type;
            * thus a check for equality can be skipped.
            */
            /*
            * Even worse: I cannot see a scenario where a restricting
            * union simple type can have other member types as the member
            * types of it's base type. This check seems not necessary with
            * respect to the derivation process in libxml2.
            * But necessary if constructing types with an API.
            */
            if (type->memberTypes != NULL) {
                member = type->memberTypes;
                baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
                if ((member == NULL) && (baseMember != NULL)) {
                  PERROR_INT("xmlSchemaCheckCOSSTRestricts",
                      "different number of member types in base");
                }
                while (member != NULL) {
                  if (baseMember == NULL) {
                      PERROR_INT("xmlSchemaCheckCOSSTRestricts",
                      "different number of member types in base");
                  } else if ((member->type != baseMember->type) &&
                      (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
                        member->type, baseMember->type, 0) != 0)) {
                      xmlChar *strBMT = NULL, *strBT = NULL;

                      xmlSchemaPCustomErrExt(pctxt,
                        XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
                        WXS_BASIC_CAST type, NULL,
                        "The member type %s is not validly "
                        "derived from its corresponding member "
                        "type %s of the base type %s",
                        xmlSchemaGetComponentQName(&str, member->type),
                        xmlSchemaGetComponentQName(&strBMT, baseMember->type),
                        xmlSchemaGetComponentQName(&strBT, type->baseType));
                      FREE_AND_NULL(str)
                      FREE_AND_NULL(strBMT)
                      FREE_AND_NULL(strBT)
                      return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
                  }
                  member = member->next;
                  baseMember = baseMember->next;
                }
            }
          }
          /*
          * 3.3.2.4 Only pattern and enumeration facet components are
          * allowed among the {facets}.
          */
          if (type->facets != NULL) {
            xmlSchemaFacetPtr facet;
            int ok = 1;

            facet = type->facets;
            do {
                if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
                  (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
                  xmlSchemaPIllegalFacetListUnionErr(pctxt,
                        XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
                        type, facet);
                  ok = 0;
                }
                facet = facet->next;
            } while (facet != NULL);
            if (ok == 0)
                return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);

          }
          /*
          * SPEC (3.3.2.5) (same as 1.3.2)
          *
          * NOTE (3.3.2.5) This is currently done in
          * xmlSchemaDeriveAndValidateFacets()
          */
      }
    }

    return (0);
}

/**
 * xmlSchemaCheckSRCSimpleType:
 * @ctxt:  the schema parser context
 * @type:  the simple type definition
 *
 * Checks crc-simple-type constraints. 
 *
 * Returns 0 if the constraints are satisfied,
 * if not a positive error code and -1 on internal
 * errors.
 */
#if 0
static int
xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
                      xmlSchemaTypePtr type)
{
    /*
    * src-simple-type.1 The corresponding simple type definition, if any,
    * must satisfy the conditions set out in Constraints on Simple Type
    * Definition Schema Components (§3.14.6).
    */    
    if (WXS_IS_RESTRICTION(type)) {
      /*
      * src-simple-type.2 "If the <restriction> alternative is chosen,
      * either it must have a base [attribute] or a <simpleType> among its
      * [children], but not both."
      * NOTE: This is checked in the parse function of <restriction>.
      */
      /*
      * 
      */
    } else if (WXS_IS_LIST(type)) {
      /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
      * an itemType [attribute] or a <simpleType> among its [children],
      * but not both."
      *
      * NOTE: This is checked in the parse function of <list>.
      */
    } else if (WXS_IS_UNION(type)) {      
      /* 
      * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
      */
    }
    return (0);
}
#endif

static int
xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
{
   if (ctxt->vctxt == NULL) {
      ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
      if (ctxt->vctxt == NULL) {
          xmlSchemaPErr(ctxt, NULL,
            XML_SCHEMAP_INTERNAL,
            "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
            "failed to create a temp. validation context.\n",
            NULL, NULL);
          return (-1);
      }
      /* TODO: Pass user data. */
      xmlSchemaSetValidErrors(ctxt->vctxt,
          ctxt->error, ctxt->warning, ctxt->errCtxt);
      xmlSchemaSetValidStructuredErrors(ctxt->vctxt,
          ctxt->serror, ctxt->errCtxt);
    }
    return (0);
}

static int
xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
                       xmlNodePtr node,
                       xmlSchemaTypePtr type,
                       const xmlChar *value,
                       xmlSchemaValPtr *retVal,
                       int fireErrors,
                       int normalize,
                       int isNormalized);

/**
 * xmlSchemaParseCheckCOSValidDefault:
 * @pctxt:  the schema parser context
 * @type:  the simple type definition
 * @value: the default value
 * @node: an optional node (the holder of the value)
 *
 * Schema Component Constraint: Element Default Valid (Immediate)
 * (cos-valid-default)
 * This will be used by the parser only. For the validator there's
 * an other version.
 *
 * Returns 0 if the constraints are satisfied,
 * if not, a positive error code and -1 on internal
 * errors.
 */
static int
xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
                           xmlNodePtr node,
                           xmlSchemaTypePtr type,
                           const xmlChar *value,
                           xmlSchemaValPtr *val)
{
    int ret = 0;

    /*
    * cos-valid-default:
    * Schema Component Constraint: Element Default Valid (Immediate)
    * For a string to be a valid default with respect to a type
    * definition the appropriate case among the following must be true:
    */
    if WXS_IS_COMPLEX(type) {
      /*
      * Complex type.
      *
      * SPEC (2.1) "its {content type} must be a simple type definition
      * or mixed."
      * SPEC (2.2.2) "If the {content type} is mixed, then the {content
      * type}'s particle must be ·emptiable· as defined by
      * Particle Emptiable (§3.9.6)."
      */
      if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
          ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
          /* NOTE that this covers (2.2.2) as well. */
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
            WXS_BASIC_CAST type, type->node,
            "For a string to be a valid default, the type definition "
            "must be a simple type or a complex type with mixed content "
            "and a particle emptiable", NULL);
          return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
      }
    }
    /*
    * 1 If the type definition is a simple type definition, then the string
    * must be ·valid· with respect to that definition as defined by String
    * Valid (§3.14.4).
    *
    * AND
    *
    * 2.2.1 If the {content type} is a simple type definition, then the
    * string must be ·valid· with respect to that simple type definition
    * as defined by String Valid (§3.14.4).
    */
    if (WXS_IS_SIMPLE(type))
      ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
          type, value, val, 1, 1, 0);
    else if (WXS_HAS_SIMPLE_CONTENT(type))
      ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
          type->contentTypeDef, value, val, 1, 1, 0);
    else
      return (ret);

    if (ret < 0) {
      PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
          "calling xmlSchemaVCheckCVCSimpleType()");
    }

    return (ret);
}

/**
 * xmlSchemaCheckCTPropsCorrect:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 *
 *.(4.6) Constraints on Complex Type Definition Schema Components
 * Schema Component Constraint:
 * Complex Type Definition Properties Correct (ct-props-correct)
 * STATUS: (seems) complete
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
                       xmlSchemaTypePtr type)
{
    /*
    * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
    *
    * SPEC (1) "The values of the properties of a complex type definition must
    * be as described in the property tableau in The Complex Type Definition
    * Schema Component (§3.4.1), modulo the impact of Missing
    * Sub-components (§5.3)."
    */
    if ((type->baseType != NULL) &&
      (WXS_IS_SIMPLE(type->baseType)) &&
      (WXS_IS_EXTENSION(type) == 0)) {
      /*
      * SPEC (2) "If the {base type definition} is a simple type definition,
      * the {derivation method} must be extension."
      */
      xmlSchemaCustomErr(ACTXT_CAST pctxt,
          XML_SCHEMAP_SRC_CT_1,
          NULL, WXS_BASIC_CAST type, 
          "If the base type is a simple type, the derivation method must be "
          "'extension'", NULL, NULL);
      return (XML_SCHEMAP_SRC_CT_1);
    }
    /*
    * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
    * definition·. That is, it must be possible to reach the ·ur-type
    * definition by repeatedly following the {base type definition}."
    *
    * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
    */
    /*
    * NOTE that (4) and (5) need the following:
    *   - attribute uses need to be already inherited (apply attr. prohibitions)
    *   - attribute group references need to be expanded already
    *   - simple types need to be typefixed already
    */    
    if (type->attrUses &&
      (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
    {
      xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
      xmlSchemaAttributeUsePtr use, tmp;
      int i, j, hasId = 0;

      for (i = uses->nbItems -1; i >= 0; i--) {
          use = uses->items[i];
          
          /* 
          * SPEC ct-props-correct
          * (4) "Two distinct attribute declarations in the
          * {attribute uses} must not have identical {name}s and
          * {target namespace}s."
          */
          if (i > 0) {
            for (j = i -1; j >= 0; j--) {
                tmp = uses->items[j];
                if ((WXS_ATTRUSE_DECL_NAME(use) ==
                  WXS_ATTRUSE_DECL_NAME(tmp)) &&
                  (WXS_ATTRUSE_DECL_TNS(use) ==
                  WXS_ATTRUSE_DECL_TNS(tmp)))
                {
                  xmlChar *str = NULL;

                  xmlSchemaCustomErr(ACTXT_CAST pctxt,
                      XML_SCHEMAP_AG_PROPS_CORRECT,
                      NULL, WXS_BASIC_CAST type,
                      "Duplicate %s",
                      xmlSchemaGetComponentDesignation(&str, use),
                      NULL);
                  FREE_AND_NULL(str);
                  /*
                  * Remove the duplicate.
                  */
                  if (xmlSchemaItemListRemove(uses, i) == -1)
                      goto exit_failure;
                  goto next_use;
                }
            }
          }
          /*
          * SPEC ct-props-correct
          * (5) "Two distinct attribute declarations in the
          * {attribute uses} must not have {type definition}s which
          * are or are derived from ID."
          */
          if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
            if (xmlSchemaIsDerivedFromBuiltInType(
                WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
            {           
                if (hasId) {
                  xmlChar *str = NULL;
                  
                  xmlSchemaCustomErr(ACTXT_CAST pctxt,
                      XML_SCHEMAP_AG_PROPS_CORRECT,
                      NULL, WXS_BASIC_CAST type,
                      "There must not exist more than one attribute "
                      "declaration of type 'xs:ID' "
                      "(or derived from 'xs:ID'). The %s violates this "
                      "constraint",
                      xmlSchemaGetComponentDesignation(&str, use),
                      NULL);
                  FREE_AND_NULL(str);
                  if (xmlSchemaItemListRemove(uses, i) == -1)
                      goto exit_failure;
                }
                
                hasId = 1;
            }
          }
next_use: {}
      }
    }
    return (0);
exit_failure:
    return(-1);
}

static int
xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
                   xmlSchemaTypePtr typeB)
{
    /*
    * TODO: This should implement component-identity
    * in the future.
    */
    if ((typeA == NULL) || (typeB == NULL))
      return (0);
    return (typeA == typeB);
}

/**
 * xmlSchemaCheckCOSCTDerivedOK:
 * @ctxt:  the schema parser context
 * @type:  the to-be derived complex type definition
 * @baseType:  the base complex type definition
 * @set: the given set
 *
 * Schema Component Constraint:
 * Type Derivation OK (Complex) (cos-ct-derived-ok)
 *
 * STATUS: completed
 *
 * Returns 0 if the constraints are satisfied, or 1
 * if not.
 */
static int
xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
                       xmlSchemaTypePtr type,
                       xmlSchemaTypePtr baseType,
                       int set)
{
    int equal = xmlSchemaAreEqualTypes(type, baseType);
    /* TODO: Error codes. */
    /*
    * SPEC "For a complex type definition (call it D, for derived)
    * to be validly derived from a type definition (call this
    * B, for base) given a subset of {extension, restriction}
    * all of the following must be true:"
    */
    if (! equal) {
      /*
      * SPEC (1) "If B and D are not the same type definition, then the
      * {derivation method} of D must not be in the subset."
      */
      if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
          ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
          return (1);
    } else {
      /*
      * SPEC (2.1) "B and D must be the same type definition."
      */
      return (0);
    }
    /*
    * SPEC (2.2) "B must be D's {base type definition}."
    */
    if (type->baseType == baseType)
      return (0);
    /*
    * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
    * definition·."
    */
    if (WXS_IS_ANYTYPE(type->baseType))
      return (1);

    if (WXS_IS_COMPLEX(type->baseType)) {
      /*
      * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
      * must be validly derived from B given the subset as defined by this
      * constraint."
      */
      return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
          baseType, set));
    } else {
      /*
      * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
      * must be validly derived from B given the subset as defined in Type
      * Derivation OK (Simple) (§3.14.6).
      */
      return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
          baseType, set));
    }
}

/**
 * xmlSchemaCheckCOSDerivedOK:
 * @type:  the derived simple type definition
 * @baseType:  the base type definition
 *
 * Calls:
 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
 *
 * Checks wheter @type can be validly derived from @baseType.
 *
 * Returns 0 on success, an positive error code otherwise.
 */
static int
xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
                     xmlSchemaTypePtr type,
                     xmlSchemaTypePtr baseType,
                     int set)
{
    if (WXS_IS_SIMPLE(type))
      return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
    else
      return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
}

/**
 * xmlSchemaCheckCOSCTExtends:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 *
 * (3.4.6) Constraints on Complex Type Definition Schema Components
 * Schema Component Constraint:
 * Derivation Valid (Extension) (cos-ct-extends)
 *
 * STATUS:
 *   missing:
 *     (1.5)
 *     (1.4.3.2.2.2) "Particle Valid (Extension)"
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
                     xmlSchemaTypePtr type)
{
    xmlSchemaTypePtr base = type->baseType;
    /*
    * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
    * temporarily only.
    */
    /*
    * SPEC (1) "If the {base type definition} is a complex type definition,
    * then all of the following must be true:"
    */
    if (WXS_IS_COMPLEX(base)) {     
      /*
      * SPEC (1.1) "The {final} of the {base type definition} must not
      * contain extension."
      */
      if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_COS_CT_EXTENDS_1_1,
            WXS_BASIC_CAST type, NULL,
            "The 'final' of the base type definition "
            "contains 'extension'", NULL);
          return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
      }
      
      /*
      * ATTENTION: The constrains (1.2) and (1.3) are not applied,
      * since they are automatically satisfied through the
      * inheriting mechanism.
      * Note that even if redefining components, the inheriting mechanism
      * is used.
      */
#if 0
      /*
      * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
      * uses}
      * of the complex type definition itself, that is, for every attribute
      * use in the {attribute uses} of the {base type definition}, there
      * must be an attribute use in the {attribute uses} of the complex
      * type definition itself whose {attribute declaration} has the same
      * {name}, {target namespace} and {type definition} as its attribute
      * declaration"
      */
      if (base->attrUses != NULL) {
          int i, j, found;
          xmlSchemaAttributeUsePtr use, buse;

          for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
            buse = (WXS_LIST_CAST base->attrUses)->items[i];
            found = 0;
            if (type->attrUses != NULL) {
                use = (WXS_LIST_CAST type->attrUses)->items[j];
                for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
                {
                  if ((WXS_ATTRUSE_DECL_NAME(use) ==
                        WXS_ATTRUSE_DECL_NAME(buse)) &&
                      (WXS_ATTRUSE_DECL_TNS(use) ==
                        WXS_ATTRUSE_DECL_TNS(buse)) &&
                      (WXS_ATTRUSE_TYPEDEF(use) ==
                        WXS_ATTRUSE_TYPEDEF(buse))
                  {
                      found = 1;
                      break;
                  }
                }
            }
            if (! found) {
                xmlChar *str = NULL;
            
                xmlSchemaCustomErr(ACTXT_CAST ctxt,
                  XML_SCHEMAP_COS_CT_EXTENDS_1_2,
                  NULL, WXS_BASIC_CAST type,
                  /* 
                  * TODO: The report does not indicate that also the
                  * type needs to be the same.
                  */
                  "This type is missing a matching correspondent "
                  "for its {base type}'s %s in its {attribute uses}",
                  xmlSchemaGetComponentDesignation(&str,
                      buse->children),
                  NULL);
                FREE_AND_NULL(str)
            }
          }
      }
      /*
      * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
      * definition must also have one, and the base type definition's
      * {attribute  wildcard}'s {namespace constraint} must be a subset
      * of the complex  type definition's {attribute wildcard}'s {namespace
      * constraint}, as defined by Wildcard Subset (§3.10.6)."
      */
  
      /*
      * MAYBE TODO: Enable if ever needed. But this will be needed only
      * if created the type via a schema construction API.
      */
      if (base->attributeWildcard != NULL) {
          if (type->attributeWilcard == NULL) {
            xmlChar *str = NULL;
            
            xmlSchemaCustomErr(ACTXT_CAST pctxt,
                XML_SCHEMAP_COS_CT_EXTENDS_1_3,
                NULL, type,
                "The base %s has an attribute wildcard, "
                "but this type is missing an attribute wildcard",
                xmlSchemaGetComponentDesignation(&str, base));
            FREE_AND_NULL(str)

          } else if (xmlSchemaCheckCOSNSSubset(
            base->attributeWildcard, type->attributeWildcard))
          {
            xmlChar *str = NULL;
            
            xmlSchemaCustomErr(ACTXT_CAST pctxt,
                XML_SCHEMAP_COS_CT_EXTENDS_1_3,
                NULL, type,
                "The attribute wildcard is not a valid "
                "superset of the one in the base %s",
                xmlSchemaGetComponentDesignation(&str, base));
            FREE_AND_NULL(str)
          }
      }
#endif
      /*
      * SPEC (1.4) "One of the following must be true:"
      */
      if ((type->contentTypeDef != NULL) &&
          (type->contentTypeDef == base->contentTypeDef)) {
          /*
          * SPEC (1.4.1) "The {content type} of the {base type definition}
          * and the {content type} of the complex type definition itself
          * must be the same simple type definition"
          * PASS
          */
      } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
          (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
          /*
          * SPEC (1.4.2) "The {content type} of both the {base type
          * definition} and the complex type definition itself must
          * be empty."
          * PASS
          */
      } else {
          /*
          * SPEC (1.4.3) "All of the following must be true:"
          */
          if (type->subtypes == NULL) {
            /*
            * SPEC 1.4.3.1 The {content type} of the complex type
            * definition itself must specify a particle.
            */
            xmlSchemaPCustomErr(ctxt,
                XML_SCHEMAP_COS_CT_EXTENDS_1_1,
                WXS_BASIC_CAST type, NULL,
                "The content type must specify a particle", NULL);
            return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
          }
          /*
          * SPEC (1.4.3.2) "One of the following must be true:"
          */
          if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
            /*
            * SPEC (1.4.3.2.1) "The {content type} of the {base type
            * definition} must be empty.
            * PASS
            */
          } else {
            /*
            * SPEC (1.4.3.2.2) "All of the following must be true:"
            */
            if ((type->contentType != base->contentType) ||
                ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
                (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
                /*
                * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
                * or both must be element-only."
                */
                xmlSchemaPCustomErr(ctxt,
                  XML_SCHEMAP_COS_CT_EXTENDS_1_1,
                  WXS_BASIC_CAST type, NULL,
                  "The content type of both, the type and its base "
                  "type, must either 'mixed' or 'element-only'", NULL);
                return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
            }
            /*
            * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
            * complex type definition must be a ·valid extension·
            * of the {base type definition}'s particle, as defined
            * in Particle Valid (Extension) (§3.9.6)."
            *
            * NOTE that we won't check "Particle Valid (Extension)",
            * since it is ensured by the derivation process in
            * xmlSchemaTypeFixup(). We need to implement this when heading
            * for a construction API
            * TODO: !! This is needed to be checked if redefining a type !!
            */
          }
          /*
          * URGENT TODO (1.5)
          */
      }
    } else {
      /*
      * SPEC (2) "If the {base type definition} is a simple type definition,
      * then all of the following must be true:"
      */
      if (type->contentTypeDef != base) {
          /*
          * SPEC (2.1) "The {content type} must be the same simple type
          * definition."
          */
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_COS_CT_EXTENDS_1_1,
            WXS_BASIC_CAST type, NULL,
            "The content type must be the simple base type", NULL);
          return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
      }
      if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
          /*
          * SPEC (2.2) "The {final} of the {base type definition} must not
          * contain extension"
          * NOTE that this is the same as (1.1).
          */
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_COS_CT_EXTENDS_1_1,
            WXS_BASIC_CAST type, NULL,
            "The 'final' of the base type definition "
            "contains 'extension'", NULL);
          return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
      }
    }
    return (0);
}

/**
 * xmlSchemaCheckDerivationOKRestriction:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 *
 * (3.4.6) Constraints on Complex Type Definition Schema Components
 * Schema Component Constraint:
 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
 *
 * STATUS:
 *   missing:
 *     (5.4.2) ???
 *
 * ATTENTION:
 * In XML Schema 1.1 this will be:
 * Validation Rule: Checking complex type subsumption
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
                              xmlSchemaTypePtr type)
{
    xmlSchemaTypePtr base;

    /*
    * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
    * temporarily only.
    */
    base = type->baseType;
    if (! WXS_IS_COMPLEX(base)) {
      xmlSchemaCustomErr(ACTXT_CAST ctxt,     
          XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
          type->node, WXS_BASIC_CAST type,
          "The base type must be a complex type", NULL, NULL);
      return(ctxt->err);
    }
    if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
      /*
      * SPEC (1) "The {base type definition} must be a complex type
      * definition whose {final} does not contain restriction."
      */
      xmlSchemaCustomErr(ACTXT_CAST ctxt,     
          XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
          type->node, WXS_BASIC_CAST type,
          "The 'final' of the base type definition "
          "contains 'restriction'", NULL, NULL);
      return (ctxt->err);
    }
    /*
    * SPEC (2), (3) and (4)
    * Those are handled in a separate function, since the
    * same constraints are needed for redefinition of
    * attribute groups as well.
    */
    if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
      XML_SCHEMA_ACTION_DERIVE,
      WXS_BASIC_CAST type, WXS_BASIC_CAST base,
      type->attrUses, base->attrUses,
      type->attributeWildcard,
      base->attributeWildcard) == -1)
    {
      return(-1);
    }
    /*
    * SPEC (5) "One of the following must be true:"
    */
    if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
      /*
      * SPEC (5.1) "The {base type definition} must be the
      * ·ur-type definition·."
      * PASS
      */
    } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
          (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
      /*
      * SPEC (5.2.1) "The {content type} of the complex type definition
      * must be a simple type definition"
      *
      * SPEC (5.2.2) "One of the following must be true:"
      */
      if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
          (base->contentType == XML_SCHEMA_CONTENT_BASIC))
      {
          int err;
          /*
          * SPEC (5.2.2.1) "The {content type} of the {base type
          * definition} must be a simple type definition from which
          * the {content type} is validly derived given the empty
          * set as defined in Type Derivation OK (Simple) (§3.14.6)."
          *
          * ATTENTION TODO: This seems not needed if the type implicitely
          * derived from the base type.
          * 
          */
          err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
            type->contentTypeDef, base->contentTypeDef, 0);
          if (err != 0) {
            xmlChar *strA = NULL, *strB = NULL;

            if (err == -1)
                return(-1);
            xmlSchemaCustomErr(ACTXT_CAST ctxt,
                XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
                NULL, WXS_BASIC_CAST type,
                "The {content type} %s is not validly derived from the "
                "base type's {content type} %s",
                xmlSchemaGetComponentDesignation(&strA,
                  type->contentTypeDef),
                xmlSchemaGetComponentDesignation(&strB,
                  base->contentTypeDef));
            FREE_AND_NULL(strA);
            FREE_AND_NULL(strB);
            return(ctxt->err);
          }
      } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
          (xmlSchemaIsParticleEmptiable(
            (xmlSchemaParticlePtr) base->subtypes))) {
          /*
          * SPEC (5.2.2.2) "The {base type definition} must be mixed
          * and have a particle which is ·emptiable· as defined in
          * Particle Emptiable (§3.9.6)."
          * PASS
          */
      } else {
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
            WXS_BASIC_CAST type, NULL,
            "The content type of the base type must be either "
            "a simple type or 'mixed' and an emptiable particle", NULL);
          return (ctxt->err);
      }
    } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
      /*
      * SPEC (5.3.1) "The {content type} of the complex type itself must
      * be empty"
      */
      if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
          /*
          * SPEC (5.3.2.1) "The {content type} of the {base type
          * definition} must also be empty."
          * PASS
          */
      } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
          (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
          xmlSchemaIsParticleEmptiable(
            (xmlSchemaParticlePtr) base->subtypes)) {
          /*
          * SPEC (5.3.2.2) "The {content type} of the {base type
          * definition} must be elementOnly or mixed and have a particle
          * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
          * PASS
          */
      } else {
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
            WXS_BASIC_CAST type, NULL,
            "The content type of the base type must be either "
            "empty or 'mixed' (or 'elements-only') and an emptiable "
            "particle", NULL);
          return (ctxt->err);
      }
    } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
      WXS_HAS_MIXED_CONTENT(type)) {
      /*
      * SPEC (5.4.1.1) "The {content type} of the complex type definition
      * itself must be element-only"
      */     
      if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
          /*
          * SPEC (5.4.1.2) "The {content type} of the complex type
          * definition itself and of the {base type definition} must be
          * mixed"
          */
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
            WXS_BASIC_CAST type, NULL,
            "If the content type is 'mixed', then the content type of the "
            "base type must also be 'mixed'", NULL);
          return (ctxt->err);
      }
      /*
      * SPEC (5.4.2) "The particle of the complex type definition itself
      * must be a ·valid restriction· of the particle of the {content
      * type} of the {base type definition} as defined in Particle Valid
      * (Restriction) (§3.9.6).
      *
      * URGENT TODO: (5.4.2)
      */
    } else {
      xmlSchemaPCustomErr(ctxt,
          XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
          WXS_BASIC_CAST type, NULL,
          "The type is not a valid restriction of its base type", NULL);
      return (ctxt->err);
    }
    return (0);
}

/**
 * xmlSchemaCheckCTComponent:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 *
 * (3.4.6) Constraints on Complex Type Definition Schema Components
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
                    xmlSchemaTypePtr type)
{
    int ret;
    /*
    * Complex Type Definition Properties Correct
    */
    ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
    if (ret != 0)
      return (ret);
    if (WXS_IS_EXTENSION(type))
      ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
    else
      ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
    return (ret);
}

/**
 * xmlSchemaCheckSRCCT:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 *
 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
 * Schema Representation Constraint:
 * Complex Type Definition Representation OK (src-ct)
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
                xmlSchemaTypePtr type)
{
    xmlSchemaTypePtr base;
    int ret = 0;

    /*
    * TODO: Adjust the error codes here, as I used
    * XML_SCHEMAP_SRC_CT_1 only yet.
    */
    base = type->baseType;
    if (! WXS_HAS_SIMPLE_CONTENT(type)) {
      /*
      * 1 If the <complexContent> alternative is chosen, the type definition
      * ·resolved· to by the ·actual value· of the base [attribute]
      * must be a complex type definition;
      */
      if (! WXS_IS_COMPLEX(base)) {
          xmlChar *str = NULL;
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_SRC_CT_1,
            WXS_BASIC_CAST type, type->node,
            "If using <complexContent>, the base type is expected to be "
            "a complex type. The base type '%s' is a simple type",
            xmlSchemaFormatQName(&str, base->targetNamespace,
            base->name));
          FREE_AND_NULL(str)
          return (XML_SCHEMAP_SRC_CT_1);
      }
    } else {
      /*
      * SPEC
      * 2 If the <simpleContent> alternative is chosen, all of the
      * following must be true:
      * 2.1 The type definition ·resolved· to by the ·actual value· of the
      * base [attribute] must be one of the following:
      */
      if (WXS_IS_SIMPLE(base)) {
          if (WXS_IS_EXTENSION(type) == 0) {
            xmlChar *str = NULL;
            /*
            * 2.1.3 only if the <extension> alternative is also
            * chosen, a simple type definition.
            */
            /* TODO: Change error code to ..._SRC_CT_2_1_3. */
            xmlSchemaPCustomErr(ctxt,
                XML_SCHEMAP_SRC_CT_1,
                WXS_BASIC_CAST type, NULL,
                "If using <simpleContent> and <restriction>, the base "
                "type must be a complex type. The base type '%s' is "
                "a simple type",
                xmlSchemaFormatQName(&str, base->targetNamespace,
                  base->name));
            FREE_AND_NULL(str)
            return (XML_SCHEMAP_SRC_CT_1);
          }
      } else {
          /* Base type is a complex type. */
          if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
            (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
            /*
            * 2.1.1 a complex type definition whose {content type} is a
            * simple type definition;
            * PASS
            */
            if (base->contentTypeDef == NULL) {
                xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
                  WXS_BASIC_CAST type, NULL,
                  "Internal error: xmlSchemaCheckSRCCT, "
                  "'%s', base type has no content type",
                  type->name);
                return (-1);
            }
          } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
            (WXS_IS_RESTRICTION(type))) {

            /*
            * 2.1.2 only if the <restriction> alternative is also
            * chosen, a complex type definition whose {content type}
            * is mixed and a particle emptiable.
            */
            if (! xmlSchemaIsParticleEmptiable(
                (xmlSchemaParticlePtr) base->subtypes)) {
                ret = XML_SCHEMAP_SRC_CT_1;
            } else 
                /*
                * Attention: at this point the <simpleType> child is in
                * ->contentTypeDef (put there during parsing).
                */                
                if (type->contentTypeDef == NULL) {
                xmlChar *str = NULL;
                /*
                * 2.2 If clause 2.1.2 above is satisfied, then there
                * must be a <simpleType> among the [children] of
                * <restriction>.
                */
                /* TODO: Change error code to ..._SRC_CT_2_2. */
                xmlSchemaPCustomErr(ctxt,
                  XML_SCHEMAP_SRC_CT_1,
                  WXS_BASIC_CAST type, NULL,
                  "A <simpleType> is expected among the children "
                  "of <restriction>, if <simpleContent> is used and "
                  "the base type '%s' is a complex type",
                  xmlSchemaFormatQName(&str, base->targetNamespace,
                  base->name));
                FREE_AND_NULL(str)
                return (XML_SCHEMAP_SRC_CT_1);
            }
          } else {
            ret = XML_SCHEMAP_SRC_CT_1;
          }
      }
      if (ret > 0) {
          xmlChar *str = NULL;
          if (WXS_IS_RESTRICTION(type)) {
            xmlSchemaPCustomErr(ctxt,
                XML_SCHEMAP_SRC_CT_1,
                WXS_BASIC_CAST type, NULL,
                "If <simpleContent> and <restriction> is used, the "
                "base type must be a simple type or a complex type with "
                "mixed content and particle emptiable. The base type "
                "'%s' is none of those",
                xmlSchemaFormatQName(&str, base->targetNamespace,
                base->name));
          } else {
            xmlSchemaPCustomErr(ctxt,
                XML_SCHEMAP_SRC_CT_1,
                WXS_BASIC_CAST type, NULL,
                "If <simpleContent> and <extension> is used, the "
                "base type must be a simple type. The base type '%s' "
                "is a complex type",
                xmlSchemaFormatQName(&str, base->targetNamespace,
                base->name));
          }
          FREE_AND_NULL(str)
      }
    }
    /*
    * SPEC (3) "The corresponding complex type definition component must
    * satisfy the conditions set out in Constraints on Complex Type
    * Definition Schema Components (§3.4.6);"
    * NOTE (3) will be done in xmlSchemaTypeFixup().
    */
    /*
    * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
    * above for {attribute wildcard} is satisfied, the intensional
    * intersection must be expressible, as defined in Attribute Wildcard
    * Intersection (§3.10.6).
    * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
    */
    return (ret);
}

#ifdef ENABLE_PARTICLE_RESTRICTION
/**
 * xmlSchemaCheckParticleRangeOK:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 *
 * (3.9.6) Constraints on Particle Schema Components
 * Schema Component Constraint:
 * Occurrence Range OK (range-ok)
 *
 * STATUS: complete
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
                        int bmin, int bmax)
{
    if (rmin < bmin)
      return (1);
    if ((bmax != UNBOUNDED) &&
      (rmax > bmax))
      return (1);
    return (0);
}

/**
 * xmlSchemaCheckRCaseNameAndTypeOK:
 * @ctxt:  the schema parser context
 * @r: the restricting element declaration particle
 * @b: the base element declaration particle
 *
 * (3.9.6) Constraints on Particle Schema Components
 * Schema Component Constraint:
 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
 * (rcase-NameAndTypeOK)
 *
 * STATUS:
 *   MISSING (3.2.3)
 *   CLARIFY: (3.2.2)
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
                         xmlSchemaParticlePtr r,
                         xmlSchemaParticlePtr b)
{
    xmlSchemaElementPtr elemR, elemB;

    /* TODO: Error codes (rcase-NameAndTypeOK). */
    elemR = (xmlSchemaElementPtr) r->children;
    elemB = (xmlSchemaElementPtr) b->children;
    /*
    * SPEC (1) "The declarations' {name}s and {target namespace}s are
    * the same."
    */
    if ((elemR != elemB) &&
      ((! xmlStrEqual(elemR->name, elemB->name)) ||
      (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
      return (1);
    /*
    * SPEC (2) "R's occurrence range is a valid restriction of B's
    * occurrence range as defined by Occurrence Range OK (§3.9.6)."
    */
    if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
          b->minOccurs, b->maxOccurs) != 0)
      return (1);
    /*
    * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
    * {scope} are global."
    */
    if (elemR == elemB)
      return (0);
    /*
    * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
    */
    if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
      (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
       return (1);
    /*
    * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
    * or is not fixed, or R's declaration's {value constraint} is fixed
    * with the same value."
    */
    if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
      ((elemR->value == NULL) ||
       ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
       /* TODO: Equality of the initial value or normalized or canonical? */
       (! xmlStrEqual(elemR->value, elemB->value))))
       return (1);
    /*
    * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
    * definitions} is a subset of B's declaration's {identity-constraint
    * definitions}, if any."
    */
    if (elemB->idcs != NULL) {
      /* TODO */
    }
    /*
    * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
    * superset of B's declaration's {disallowed substitutions}."
    */
    if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
       ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
      ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
       ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
      ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
       ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
       return (1);
    /*
    * SPEC (3.2.5) "R's {type definition} is validly derived given
    * {extension, list, union} from B's {type definition}"
    *
    * BADSPEC TODO: What's the point of adding "list" and "union" to the
    * set, if the corresponding constraints handle "restriction" and
    * "extension" only?
    *
    */
    {
      int set = 0;

      set |= SUBSET_EXTENSION;
      set |= SUBSET_LIST;
      set |= SUBSET_UNION;
      if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
          elemB->subtypes, set) != 0)
          return (1);
    }
    return (0);
}

/**
 * xmlSchemaCheckRCaseNSCompat:
 * @ctxt:  the schema parser context
 * @r: the restricting element declaration particle
 * @b: the base wildcard particle
 *
 * (3.9.6) Constraints on Particle Schema Components
 * Schema Component Constraint:
 * Particle Derivation OK (Elt:Any -- NSCompat)
 * (rcase-NSCompat)
 *
 * STATUS: complete
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
                      xmlSchemaParticlePtr r,
                      xmlSchemaParticlePtr b)
{
    /* TODO:Error codes (rcase-NSCompat). */
    /*
    * SPEC "For an element declaration particle to be a ·valid restriction·
    * of a wildcard particle all of the following must be true:"
    *
    * SPEC (1) "The element declaration's {target namespace} is ·valid·
    * with respect to the wildcard's {namespace constraint} as defined by
    * Wildcard allows Namespace Name (§3.10.4)."
    */
    if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
      ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
      return (1);
    /*
    * SPEC (2) "R's occurrence range is a valid restriction of B's
    * occurrence range as defined by Occurrence Range OK (§3.9.6)."
    */
    if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
          b->minOccurs, b->maxOccurs) != 0)
      return (1);

    return (0);
}

/**
 * xmlSchemaCheckRCaseRecurseAsIfGroup:
 * @ctxt:  the schema parser context
 * @r: the restricting element declaration particle
 * @b: the base model group particle
 *
 * (3.9.6) Constraints on Particle Schema Components
 * Schema Component Constraint:
 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
 * (rcase-RecurseAsIfGroup)
 *
 * STATUS: TODO
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
                            xmlSchemaParticlePtr r,
                            xmlSchemaParticlePtr b)
{
    /* TODO: Error codes (rcase-RecurseAsIfGroup). */
    TODO
    return (0);
}

/**
 * xmlSchemaCheckRCaseNSSubset:
 * @ctxt:  the schema parser context
 * @r: the restricting wildcard particle
 * @b: the base wildcard particle
 *
 * (3.9.6) Constraints on Particle Schema Components
 * Schema Component Constraint:
 * Particle Derivation OK (Any:Any -- NSSubset)
 * (rcase-NSSubset)
 *
 * STATUS: complete
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
                            xmlSchemaParticlePtr r,
                            xmlSchemaParticlePtr b,
                            int isAnyTypeBase)
{
    /* TODO: Error codes (rcase-NSSubset). */
    /*
    * SPEC (1) "R's occurrence range is a valid restriction of B's
    * occurrence range as defined by Occurrence Range OK (§3.9.6)."
    */
    if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
          b->minOccurs, b->maxOccurs))
      return (1);
    /*
    * SPEC (2) "R's {namespace constraint} must be an intensional subset
    * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
    */
    if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
      (xmlSchemaWildcardPtr) b->children))
      return (1);
    /*
    * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
    * definition·, R's {process contents} must be identical to or stronger
    * than B's {process contents}, where strict is stronger than lax is
    * stronger than skip."
    */
    if (! isAnyTypeBase) {
      if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
          ((xmlSchemaWildcardPtr) b->children)->processContents)
          return (1);
    }

    return (0);
}

/**
 * xmlSchemaCheckCOSParticleRestrict:
 * @ctxt:  the schema parser context
 * @type:  the complex type definition
 *
 * (3.9.6) Constraints on Particle Schema Components
 * Schema Component Constraint:
 * Particle Valid (Restriction) (cos-particle-restrict)
 *
 * STATUS: TODO
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
                          xmlSchemaParticlePtr r,
                          xmlSchemaParticlePtr b)
{
    int ret = 0;

    /*part = WXS_TYPE_PARTICLE(type);
    basePart = WXS_TYPE_PARTICLE(base);
    */

    TODO

    /*
    * SPEC (1) "They are the same particle."
    */
    if (r == b)
      return (0);


    return (0);
}

/**
 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
 * @ctxt:  the schema parser context
 * @r: the model group particle
 * @b: the base wildcard particle
 *
 * (3.9.6) Constraints on Particle Schema Components
 * Schema Component Constraint:
 * Particle Derivation OK (All/Choice/Sequence:Any --
 *                         NSRecurseCheckCardinality)
 * (rcase-NSRecurseCheckCardinality)
 *
 * STATUS: TODO: subst-groups
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
                                   xmlSchemaParticlePtr r,
                                   xmlSchemaParticlePtr b)
{
    xmlSchemaParticlePtr part;
    /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
    if ((r->children == NULL) || (r->children->children == NULL))
      return (-1);
    /*
    * SPEC "For a group particle to be a ·valid restriction· of a
    * wildcard particle..."
    *
    * SPEC (1) "Every member of the {particles} of the group is a ·valid
    * restriction· of the wildcard as defined by
    * Particle Valid (Restriction) (§3.9.6)."
    */
    part = (xmlSchemaParticlePtr) r->children->children;
    do {
      if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
          return (1);
      part = (xmlSchemaParticlePtr) part->next;
    } while (part != NULL);
    /*
    * SPEC (2) "The effective total range of the group [...] is a
    * valid restriction of B's occurrence range as defined by
    * Occurrence Range OK (§3.9.6)."
    */
    if (xmlSchemaCheckParticleRangeOK(
          xmlSchemaGetParticleTotalRangeMin(r),
          xmlSchemaGetParticleTotalRangeMax(r),
          b->minOccurs, b->maxOccurs) != 0)
      return (1);
    return (0);
}

/**
 * xmlSchemaCheckRCaseRecurse:
 * @ctxt:  the schema parser context
 * @r: the <all> or <sequence> model group particle
 * @b: the base <all> or <sequence> model group particle
 *
 * (3.9.6) Constraints on Particle Schema Components
 * Schema Component Constraint:
 * Particle Derivation OK (All:All,Sequence:Sequence --
                           Recurse)
 * (rcase-Recurse)
 *
 * STATUS:  ?
 * TODO: subst-groups
 *
 * Returns 0 if the constraints are satisfied, a positive
 * error code if not and -1 if an internal error occured.
 */
static int
xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
                     xmlSchemaParticlePtr r,
                     xmlSchemaParticlePtr b)
{
    /* xmlSchemaParticlePtr part; */
    /* TODO: Error codes (rcase-Recurse). */
    if ((r->children == NULL) || (b->children == NULL) ||
      (r->children->type != b->children->type))
      return (-1);
    /*
    * SPEC "For an all or sequence group particle to be a ·valid
    * restriction· of another group particle with the same {compositor}..."
    *
    * SPEC (1) "R's occurrence range is a valid restriction of B's
    * occurrence range as defined by Occurrence Range OK (§3.9.6)."
    */
    if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
          b->minOccurs, b->maxOccurs))
      return (1);


    return (0);
}

#endif

#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
    xmlSchemaPCustomErrExt(pctxt,      \
      XML_SCHEMAP_INVALID_FACET_VALUE, \
      WXS_BASIC_CAST fac1, fac1->node, \
      "It is an error for both '%s' and '%s' to be specified on the "\
      "same type definition", \
      BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
      BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);

#define FACET_RESTR_ERR(fac1, msg) \
    xmlSchemaPCustomErr(pctxt,      \
      XML_SCHEMAP_INVALID_FACET_VALUE, \
      WXS_BASIC_CAST fac1, fac1->node, \
      msg, NULL);

#define FACET_RESTR_FIXED_ERR(fac) \
    xmlSchemaPCustomErr(pctxt, \
      XML_SCHEMAP_INVALID_FACET_VALUE, \
      WXS_BASIC_CAST fac, fac->node, \
      "The base type's facet is 'fixed', thus the value must not " \
      "differ", NULL);

static void
xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
                  xmlSchemaFacetPtr facet1,
                  xmlSchemaFacetPtr facet2,
                  int lessGreater,
                  int orEqual,
                  int ofBase)
{
    xmlChar *msg = NULL;

    msg = xmlStrdup(BAD_CAST "'");
    msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
    msg = xmlStrcat(msg, BAD_CAST "' has to be");
    if (lessGreater == 0)
      msg = xmlStrcat(msg, BAD_CAST " equal to");
    if (lessGreater == 1)
      msg = xmlStrcat(msg, BAD_CAST " greater than");
    else
      msg = xmlStrcat(msg, BAD_CAST " less than");

    if (orEqual)
      msg = xmlStrcat(msg, BAD_CAST " or equal to");
    msg = xmlStrcat(msg, BAD_CAST " '");
    msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
    if (ofBase)
      msg = xmlStrcat(msg, BAD_CAST "' of the base type");
    else
      msg = xmlStrcat(msg, BAD_CAST "'");

    xmlSchemaPCustomErr(pctxt,
      XML_SCHEMAP_INVALID_FACET_VALUE,
      WXS_BASIC_CAST facet1, NULL,
      (const char *) msg, NULL);

    if (msg != NULL)
      xmlFree(msg);
}

/*
* xmlSchemaDeriveAndValidateFacets:
*
* Schema Component Constraint: Simple Type Restriction (Facets)
* (st-restrict-facets)
*/
static int
xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
                         xmlSchemaTypePtr type)
{
    xmlSchemaTypePtr base = type->baseType;
    xmlSchemaFacetLinkPtr link, cur, last = NULL;
    xmlSchemaFacetPtr facet, bfacet,
      flength = NULL, ftotdig = NULL, ffracdig = NULL,
      fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
      fmininc = NULL, fmaxinc = NULL,
      fminexc = NULL, fmaxexc = NULL,
      bflength = NULL, bftotdig = NULL, bffracdig = NULL,
      bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
      bfmininc = NULL, bfmaxinc = NULL,
      bfminexc = NULL, bfmaxexc = NULL;
    int res; /* err = 0, fixedErr; */

    /*
    * SPEC st-restrict-facets 1:
    * "The {variety} of R is the same as that of B."    
    */
    /*
    * SPEC st-restrict-facets 2:
    * "If {variety} is atomic, the {primitive type definition}
    * of R is the same as that of B."
    *
    * NOTE: we leave 1 & 2 out for now, since this will be
    * satisfied by the derivation process.
    * CONSTRUCTION TODO: Maybe needed if using a construction API.
    */
    /*
    * SPEC st-restrict-facets 3:
    * "The {facets} of R are the union of S and the {facets}
    * of B, eliminating duplicates. To eliminate duplicates,
    * when a facet of the same kind occurs in both S and the
    * {facets} of B, the one in the {facets} of B is not
    * included, with the exception of enumeration and pattern
    * facets, for which multiple occurrences with distinct values
    * are allowed."
    */

    if ((type->facetSet == NULL) && (base->facetSet == NULL))
      return (0);

    last = type->facetSet;
    if (last != NULL)
      while (last->next != NULL)
          last = last->next;

    for (cur = type->facetSet; cur != NULL; cur = cur->next) {
      facet = cur->facet;
      switch (facet->type) {
          case XML_SCHEMA_FACET_LENGTH:
            flength = facet; break;
          case XML_SCHEMA_FACET_MINLENGTH:
            fminlen = facet; break;
          case XML_SCHEMA_FACET_MININCLUSIVE:
            fmininc = facet; break;
          case XML_SCHEMA_FACET_MINEXCLUSIVE:
            fminexc = facet; break;
          case XML_SCHEMA_FACET_MAXLENGTH:
            fmaxlen = facet; break;
          case XML_SCHEMA_FACET_MAXINCLUSIVE:
            fmaxinc = facet; break;
          case XML_SCHEMA_FACET_MAXEXCLUSIVE:
            fmaxexc = facet; break;
          case XML_SCHEMA_FACET_TOTALDIGITS:
            ftotdig = facet; break;
          case XML_SCHEMA_FACET_FRACTIONDIGITS:
            ffracdig = facet; break;
          default:
            break;
      }
    }
    for (cur = base->facetSet; cur != NULL; cur = cur->next) {
      facet = cur->facet;
      switch (facet->type) {
          case XML_SCHEMA_FACET_LENGTH:
            bflength = facet; break;
          case XML_SCHEMA_FACET_MINLENGTH:
            bfminlen = facet; break;
          case XML_SCHEMA_FACET_MININCLUSIVE:
            bfmininc = facet; break;
          case XML_SCHEMA_FACET_MINEXCLUSIVE:
            bfminexc = facet; break;
          case XML_SCHEMA_FACET_MAXLENGTH:
            bfmaxlen = facet; break;
          case XML_SCHEMA_FACET_MAXINCLUSIVE:
            bfmaxinc = facet; break;
          case XML_SCHEMA_FACET_MAXEXCLUSIVE:
            bfmaxexc = facet; break;
          case XML_SCHEMA_FACET_TOTALDIGITS:
            bftotdig = facet; break;
          case XML_SCHEMA_FACET_FRACTIONDIGITS:
            bffracdig = facet; break;
          default:
            break;
      }
    }
    /*
    * length and minLength or maxLength (2.2) + (3.2)
    */
    if (flength && (fminlen || fmaxlen)) {
      FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
          "either of 'minLength' or 'maxLength' to be specified on "
          "the same type definition")
    }
    /*
    * Mutual exclusions in the same derivation step.
    */
    if ((fmaxinc) && (fmaxexc)) {
      /*
      * SCC "maxInclusive and maxExclusive"
      */
      FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
    }
    if ((fmininc) && (fminexc)) {
      /*
      * SCC "minInclusive and minExclusive"
      */
      FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
    }

    if (flength && bflength) {
      /*
      * SCC "length valid restriction"
      * The values have to be equal.
      */
      res = xmlSchemaCompareValues(flength->val, bflength->val);
      if (res == -2)
          goto internal_error;
      if (res != 0)
          xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
      if ((res != 0) && (bflength->fixed)) {
          FACET_RESTR_FIXED_ERR(flength)
      }

    }
    if (fminlen && bfminlen) {
      /*
      * SCC "minLength valid restriction"
      * minLength >= BASE minLength
      */
      res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
      if (res == -2)
          goto internal_error;
      if (res == -1)
          xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
      if ((res != 0) && (bfminlen->fixed)) {
          FACET_RESTR_FIXED_ERR(fminlen)
      }
    }
    if (fmaxlen && bfmaxlen) {
      /*
      * SCC "maxLength valid restriction"
      * maxLength <= BASE minLength
      */
      res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
      if (res == -2)
          goto internal_error;
      if (res == 1)
          xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
      if ((res != 0) && (bfmaxlen->fixed)) {
          FACET_RESTR_FIXED_ERR(fmaxlen)
      }
    }
    /*
    * SCC "length and minLength or maxLength"
    */
    if (! flength)
      flength = bflength;
    if (flength) {
      if (! fminlen)
          flength = bflength;
      if (fminlen) {
          /* (1.1) length >= minLength */
          res = xmlSchemaCompareValues(flength->val, fminlen->val);
          if (res == -2)
            goto internal_error;
          if (res == -1)
            xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
      }
      if (! fmaxlen)
          fmaxlen = bfmaxlen;
      if (fmaxlen) {
          /* (2.1) length <= maxLength */
          res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
          if (res == -2)
            goto internal_error;
          if (res == 1)
            xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
      }
    }
    if (fmaxinc) {
      /*
      * "maxInclusive"
      */
      if (fmininc) {
          /* SCC "maxInclusive >= minInclusive" */
          res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
          if (res == -2)
            goto internal_error;
          if (res == -1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
          }
      }
      /*
      * SCC "maxInclusive valid restriction"
      */
      if (bfmaxinc) {
          /* maxInclusive <= BASE maxInclusive */
          res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
          if (res == -2)
            goto internal_error;
          if (res == 1)
            xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
          if ((res != 0) && (bfmaxinc->fixed)) {
            FACET_RESTR_FIXED_ERR(fmaxinc)
          }
      }
      if (bfmaxexc) {
          /* maxInclusive < BASE maxExclusive */
          res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
          if (res == -2)
            goto internal_error;
          if (res != -1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
          }
      }
      if (bfmininc) {
          /* maxInclusive >= BASE minInclusive */
          res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
          if (res == -2)
            goto internal_error;
          if (res == -1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
          }
      }
      if (bfminexc) {
          /* maxInclusive > BASE minExclusive */
          res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
          if (res == -2)
            goto internal_error;
          if (res != 1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
          }
      }
    }
    if (fmaxexc) {
      /*
      * "maxExclusive >= minExclusive"
      */
      if (fminexc) {
          res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
          if (res == -2)
            goto internal_error;
          if (res == -1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
          }
      }
      /*
      * "maxExclusive valid restriction"
      */
      if (bfmaxexc) {
          /* maxExclusive <= BASE maxExclusive */
          res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
          if (res == -2)
            goto internal_error;
          if (res == 1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
          }
          if ((res != 0) && (bfmaxexc->fixed)) {
            FACET_RESTR_FIXED_ERR(fmaxexc)
          }
      }
      if (bfmaxinc) {
          /* maxExclusive <= BASE maxInclusive */
          res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
          if (res == -2)
            goto internal_error;
          if (res == 1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
          }
      }
      if (bfmininc) {
          /* maxExclusive > BASE minInclusive */
          res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
          if (res == -2)
            goto internal_error;
          if (res != 1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
          }
      }
      if (bfminexc) {
          /* maxExclusive > BASE minExclusive */
          res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
          if (res == -2)
            goto internal_error;
          if (res != 1) {
            xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
          }
      }
    }
    if (fminexc) {
      /*
      * "minExclusive < maxInclusive"
      */
      if (fmaxinc) {
          res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
          if (res == -2)
            goto internal_error;
          if (res != -1) {
            xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
          }
      }
      /*
      * "minExclusive valid restriction"
      */
      if (bfminexc) {
          /* minExclusive >= BASE minExclusive */
          res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
          if (res == -2)
            goto internal_error;
          if (res == -1) {
            xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
          }
          if ((res != 0) && (bfminexc->fixed)) {
            FACET_RESTR_FIXED_ERR(fminexc)
          }
      }
      if (bfmaxinc) {
          /* minExclusive <= BASE maxInclusive */
          res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
          if (res == -2)
            goto internal_error;
          if (res == 1) {
            xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
          }
      }
      if (bfmininc) {
          /* minExclusive >= BASE minInclusive */
          res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
          if (res == -2)
            goto internal_error;
          if (res == -1) {
            xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
          }
      }
      if (bfmaxexc) {
          /* minExclusive < BASE maxExclusive */
          res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
          if (res == -2)
            goto internal_error;
          if (res != -1) {
            xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
          }
      }
    }
    if (fmininc) {
      /*
      * "minInclusive < maxExclusive"
      */
      if (fmaxexc) {
          res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
          if (res == -2)
            goto internal_error;
          if (res != -1) {
            xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
          }
      }
      /*
      * "minExclusive valid restriction"
      */
      if (bfmininc) {
          /* minInclusive >= BASE minInclusive */
          res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
          if (res == -2)
            goto internal_error;
          if (res == -1) {
            xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
          }
          if ((res != 0) && (bfmininc->fixed)) {
            FACET_RESTR_FIXED_ERR(fmininc)
          }
      }
      if (bfmaxinc) {
          /* minInclusive <= BASE maxInclusive */
          res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
          if (res == -2)
            goto internal_error;
          if (res == 1) {
            xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
          }
      }
      if (bfminexc) {
          /* minInclusive > BASE minExclusive */
          res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
          if (res == -2)
            goto internal_error;
          if (res != 1)
            xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
      }
      if (bfmaxexc) {
          /* minInclusive < BASE maxExclusive */
          res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
          if (res == -2)
            goto internal_error;
          if (res != -1)
            xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
      }
    }
    if (ftotdig && bftotdig) {
      /*
      * SCC " totalDigits valid restriction"
      * totalDigits <= BASE totalDigits
      */
      res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
      if (res == -2)
          goto internal_error;
      if (res == 1)
          xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
          -1, 1, 1);
      if ((res != 0) && (bftotdig->fixed)) {
          FACET_RESTR_FIXED_ERR(ftotdig)
      }
    }
    if (ffracdig && bffracdig) {
      /*
      * SCC  "fractionDigits valid restriction"
      * fractionDigits <= BASE fractionDigits
      */
      res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
      if (res == -2)
          goto internal_error;
      if (res == 1)
          xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
          -1, 1, 1);
      if ((res != 0) && (bffracdig->fixed)) {
          FACET_RESTR_FIXED_ERR(ffracdig)
      }
    }
    /*
    * SCC "fractionDigits less than or equal to totalDigits"
    */
    if (! ftotdig)
      ftotdig = bftotdig;
    if (! ffracdig)
      ffracdig = bffracdig;
    if (ftotdig && ffracdig) {
      res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
      if (res == -2)
          goto internal_error;
      if (res == 1)
          xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
            -1, 1, 0);
    }
    /*
    * *Enumerations* won' be added here, since only the first set
    * of enumerations in the ancestor-or-self axis is used
    * for validation, plus we need to use the base type of those
    * enumerations for whitespace.
    *
    * *Patterns*: won't be add here, since they are ORed at
    * type level and ANDed at ancestor level. This will
    * happed during validation by walking the base axis
    * of the type.
    */
    for (cur = base->facetSet; cur != NULL; cur = cur->next) {
      bfacet = cur->facet;
      /*
      * Special handling of enumerations and patterns.
      * TODO: hmm, they should not appear in the set, so remove this.
      */
      if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
          (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
          continue;
      /*
      * Search for a duplicate facet in the current type.
      */
      link = type->facetSet;
      /* err = 0; */
      /* fixedErr = 0; */
      while (link != NULL) {
          facet = link->facet;
          if (facet->type == bfacet->type) {
            switch (facet->type) {
                case XML_SCHEMA_FACET_WHITESPACE:
                  /*
                  * The whitespace must be stronger.
                  */
                  if (facet->whitespace < bfacet->whitespace) {
                      FACET_RESTR_ERR(flength,
                        "The 'whitespace' value has to be equal to "
                        "or stronger than the 'whitespace' value of "
                        "the base type")
                  }
                  if ((bfacet->fixed) &&
                      (facet->whitespace != bfacet->whitespace)) {
                      FACET_RESTR_FIXED_ERR(facet)
                  }
                  break;
                default:
                  break;
            }
            /* Duplicate found. */
            break;
          }
          link = link->next;
      }
      /*
      * If no duplicate was found: add the base types's facet
      * to the set.
      */
      if (link == NULL) {
          link = (xmlSchemaFacetLinkPtr)
            xmlMalloc(sizeof(xmlSchemaFacetLink));
          if (link == NULL) {
            xmlSchemaPErrMemory(pctxt,
                "deriving facets, creating a facet link", NULL);
            return (-1);
          }
          link->facet = cur->facet;
          link->next = NULL;
          if (last == NULL)
            type->facetSet = link;
          else
            last->next = link;
          last = link;
      }

    }

    return (0);
internal_error:
    PERROR_INT("xmlSchemaDeriveAndValidateFacets",
      "an error occured");
    return (-1);
}

static int
xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
                                   xmlSchemaTypePtr type)
{
    xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
    /*
    * The actual value is then formed by replacing any union type
    * definition in the ·explicit members· with the members of their
    * {member type definitions}, in order.
    *
    * TODO: There's a bug entry at
    * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
    * which indicates that we'll keep the union types the future.
    */
    link = type->memberTypes;
    while (link != NULL) {

      if (WXS_IS_TYPE_NOT_FIXED(link->type))
          xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);

      if (WXS_IS_UNION(link->type)) {
          subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
          if (subLink != NULL) {
            link->type = subLink->type;
            if (subLink->next != NULL) {
                lastLink = link->next;
                subLink = subLink->next;
                prevLink = link;
                while (subLink != NULL) {
                  newLink = (xmlSchemaTypeLinkPtr)
                      xmlMalloc(sizeof(xmlSchemaTypeLink));
                  if (newLink == NULL) {
                      xmlSchemaPErrMemory(pctxt, "allocating a type link",
                        NULL);
                      return (-1);
                  }
                  newLink->type = subLink->type;
                  prevLink->next = newLink;
                  prevLink = newLink;
                  newLink->next = lastLink;

                  subLink = subLink->next;
                }
            }
          }
      }
      link = link->next;
    }
    return (0);
}

static void
xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
{       
    int has = 0, needVal = 0, normVal = 0;

    has     = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
    if (has) {
      needVal = (type->baseType->flags &
          XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
      normVal = (type->baseType->flags &
          XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
    }
    if (type->facets != NULL) {
      xmlSchemaFacetPtr fac;
      
      for (fac = type->facets; fac != NULL; fac = fac->next) {
          switch (fac->type) {
            case XML_SCHEMA_FACET_WHITESPACE:
                break;
            case XML_SCHEMA_FACET_PATTERN:
                normVal = 1;
                has = 1;
                break;
            case XML_SCHEMA_FACET_ENUMERATION:
                needVal = 1;
                normVal = 1;
                has = 1;
                break;
            default:
                has = 1;
                break;
          }
      }     
    }
    if (normVal)
      type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
    if (needVal)
      type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
    if (has)
      type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;

    if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
      xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
      /*
      * OPTIMIZE VAL TODO: Some facets need a computed value.
      */
      if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
          (prim->builtInType != XML_SCHEMAS_STRING)) {
          type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
      }     
    }       
}

static int
xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
{
    
    
    /*
    * Evaluate the whitespace-facet value.
    */    
    if (WXS_IS_LIST(type)) {
      type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
      return (0);
    } else if (WXS_IS_UNION(type))
      return (0);
    
    if (type->facetSet != NULL) {
      xmlSchemaFacetLinkPtr lin;

      for (lin = type->facetSet; lin != NULL; lin = lin->next) {
          if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
            switch (lin->facet->whitespace) {
            case XML_SCHEMAS_FACET_PRESERVE:
                type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
                break;
            case XML_SCHEMAS_FACET_REPLACE:
                type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
                break;
            case XML_SCHEMAS_FACET_COLLAPSE:
                type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
                break;
            default:
                return (-1);
            }
            return (0);
          }
      }
    }
    /*
    * For all ·atomic· datatypes other than string (and types ·derived· 
    * by ·restriction· from it) the value of whiteSpace is fixed to 
    * collapse
    */
    {
      xmlSchemaTypePtr anc;

      for (anc = type->baseType; anc != NULL && 
            anc->builtInType != XML_SCHEMAS_ANYTYPE;
            anc = anc->baseType) {

          if (anc->type == XML_SCHEMA_TYPE_BASIC) {
            if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {         
                type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;

            } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
                (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {                
                type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;

            } else
                type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
            break;
          }
      }
    }
    return (0);
}

static int
xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
                    xmlSchemaTypePtr type)
{
    if (type->type != XML_SCHEMA_TYPE_SIMPLE)
      return(0);
    if (! WXS_IS_TYPE_NOT_FIXED_1(type))
      return(0);
    type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;

    if (WXS_IS_LIST(type)) {
      /*
      * Corresponds to <simpleType><list>...
      */
      if (type->subtypes == NULL) {
          /*
          * This one is really needed, so get out.
          */
          PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
            "list type has no item-type assigned");
          return(-1);
      }
    } else if (WXS_IS_UNION(type)) {
      /*
      * Corresponds to <simpleType><union>...
      */    
      if (type->memberTypes == NULL) {
          /*
          * This one is really needed, so get out.
          */
          PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
            "union type has no member-types assigned");
          return(-1);
      }         
    } else {    
      /*
      * Corresponds to <simpleType><restriction>...
      */
      if (type->baseType == NULL) {
          PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
            "type has no base-type assigned");
          return(-1);
      }
      if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
          if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
            return(-1);
      /*
      * Variety
      * If the <restriction> alternative is chosen, then the
      * {variety} of the {base type definition}.
      */
      if (WXS_IS_ATOMIC(type->baseType))
          type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
      else if (WXS_IS_LIST(type->baseType)) {
          type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
          /*
          * Inherit the itemType.
          */
          type->subtypes = type->baseType->subtypes;
      } else if (WXS_IS_UNION(type->baseType)) {
          type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
          /*
          * NOTE that we won't assign the memberTypes of the base,
          * since this will make trouble when freeing them; we will
          * use a lookup function to access them instead.
          */
      }
    }
    return(0);
}

#ifdef DEBUG_TYPE
static void
xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
                   xmlSchemaTypePtr type)
{
    if (type->node != NULL) {
        xmlGenericError(xmlGenericErrorContext,
                        "Type of %s : %s:%d :", name,
                        type->node->doc->URL,
                        xmlGetLineNo(type->node));
    } else {
        xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
    }
    if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
      switch (type->contentType) {
          case XML_SCHEMA_CONTENT_SIMPLE:
            xmlGenericError(xmlGenericErrorContext, "simple\n");
            break;
          case XML_SCHEMA_CONTENT_ELEMENTS:
            xmlGenericError(xmlGenericErrorContext, "elements\n");
            break;
          case XML_SCHEMA_CONTENT_UNKNOWN:
            xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
            break;
          case XML_SCHEMA_CONTENT_EMPTY:
            xmlGenericError(xmlGenericErrorContext, "empty\n");
            break;
          case XML_SCHEMA_CONTENT_MIXED:
            if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
                type->subtypes))
                xmlGenericError(xmlGenericErrorContext,
                  "mixed as emptiable particle\n");
            else
                xmlGenericError(xmlGenericErrorContext, "mixed\n");
            break;
            /* Removed, since not used. */
            /*
            case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
            xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
            break;
            */
          case XML_SCHEMA_CONTENT_BASIC:
            xmlGenericError(xmlGenericErrorContext, "basic\n");
            break;
          default:
            xmlGenericError(xmlGenericErrorContext,
                "not registered !!!\n");
            break;
      }
    }
}
#endif

/*
* 3.14.6 Constraints on Simple Type Definition Schema Components
*/
static int
xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
                         xmlSchemaTypePtr type)
{
    int res, olderrs = pctxt->nberrors;

    if (type->type != XML_SCHEMA_TYPE_SIMPLE)
      return(-1);

    if (! WXS_IS_TYPE_NOT_FIXED(type))
      return(0);

    type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
    type->contentType = XML_SCHEMA_CONTENT_SIMPLE;

    if (type->baseType == NULL) {
      PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
          "missing baseType");
      goto exit_failure;
    }
    if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
      xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
    /* 
    * If a member type of a union is a union itself, we need to substitute
    * that member type for its member types.
    * NOTE that this might change in WXS 1.1; i.e. we will keep the union
    * types in WXS 1.1.
    */
    if ((type->memberTypes != NULL) &&
      (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
      return(-1);        
    /*
    * SPEC src-simple-type 1 
    * "The corresponding simple type definition, if any, must satisfy
    * the conditions set out in Constraints on Simple Type Definition
    * Schema Components (§3.14.6)."
    */
    /*
    * Schema Component Constraint: Simple Type Definition Properties Correct
    * (st-props-correct)
    */
    res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
    HFAILURE HERROR
    /* 
    * Schema Component Constraint: Derivation Valid (Restriction, Simple)
    * (cos-st-restricts)
    */
    res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
    HFAILURE HERROR
    /*
    * TODO: Removed the error report, since it got annoying to get an
    * extra error report, if anything failed until now.
    * Enable this if needed.
    *
    * xmlSchemaPErr(ctxt, type->node,
    *    XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
    *    "Simple type '%s' does not satisfy the constraints "
    *    "on simple type definitions.\n",
    *    type->name, NULL);
    */
    /*
    * Schema Component Constraint: Simple Type Restriction (Facets)
    * (st-restrict-facets)
    */
    res = xmlSchemaCheckFacetValues(type, pctxt);
    HFAILURE HERROR
    if ((type->facetSet != NULL) ||
      (type->baseType->facetSet != NULL)) {
      res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
      HFAILURE HERROR
    }
    /*
    * Whitespace value.
    */
    res = xmlSchemaTypeFixupWhitespace(type);
    HFAILURE HERROR
    xmlSchemaTypeFixupOptimFacets(type);    

exit_error:
#ifdef DEBUG_TYPE
    xmlSchemaDebugFixedType(pctxt, type);
#endif
    if (olderrs != pctxt->nberrors)
      return(pctxt->err);
    return(0);

exit_failure:
#ifdef DEBUG_TYPE
    xmlSchemaDebugFixedType(pctxt, type);
#endif
    return(-1);
}

static int
xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
                    xmlSchemaTypePtr type)
{
    int res = 0, olderrs = pctxt->nberrors;
    xmlSchemaTypePtr baseType = type->baseType;

    if (! WXS_IS_TYPE_NOT_FIXED(type))
      return(0);
    type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
    if (baseType == NULL) {
      PERROR_INT("xmlSchemaFixupComplexType",
          "missing baseType");
      goto exit_failure;
    }    
    /*
    * Fixup the base type.
    */
    if (WXS_IS_TYPE_NOT_FIXED(baseType))
      xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
    if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
      /*
      * Skip fixup if the base type is invalid.
      * TODO: Generate a warning!
      */
      return(0);
    } 
    /*
    * This basically checks if the base type can be derived.
    */
    res = xmlSchemaCheckSRCCT(pctxt, type);
    HFAILURE HERROR   
    /*
    * Fixup the content type.
    */
    if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
      /*
      * Corresponds to <complexType><simpleContent>...
      */
      if ((WXS_IS_COMPLEX(baseType)) &&
          (baseType->contentTypeDef != NULL) &&
          (WXS_IS_RESTRICTION(type))) {
          xmlSchemaTypePtr contentBase, content;
#ifdef ENABLE_NAMED_LOCALS
          char buf[30];
          const xmlChar *tmpname;
#endif
          /*
          * SPEC (1) If <restriction> + base type is <complexType>,
          * "whose own {content type} is a simple type..."
          */
          if (type->contentTypeDef != NULL) {
            /*
            * SPEC (1.1) "the simple type definition corresponding to the
            * <simpleType> among the [children] of <restriction> if there
            * is one;"
            * Note that this "<simpleType> among the [children]" was put
            * into ->contentTypeDef during parsing.
            */
            contentBase = type->contentTypeDef;
            type->contentTypeDef = NULL;
          } else {
            /*
            * (1.2) "...otherwise (<restriction> has no <simpleType>
            * among its [children]), the simple type definition which
            * is the {content type} of the ... base type."
            */
            contentBase = baseType->contentTypeDef;
          }
          /*
          * SPEC
          * "... a simple type definition which restricts the simple
          * type definition identified in clause 1.1 or clause 1.2
          * with a set of facet components"
          *
          * Create the anonymous simple type, which will be the content
          * type of the complex type.
          */
#ifdef ENABLE_NAMED_LOCALS
          snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
          tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
          content = xmlSchemaAddType(pctxt, pctxt->schema,
            XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
            type->node, 0);
#else
          content = xmlSchemaAddType(pctxt, pctxt->schema,
            XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
            type->node, 0);
#endif
          if (content == NULL)
            goto exit_failure;
          /*
          * We will use the same node as for the <complexType>
          * to have it somehow anchored in the schema doc.
          */
          content->type = XML_SCHEMA_TYPE_SIMPLE;
          content->baseType = contentBase;
          /*
          * Move the facets, previously anchored on the
          * complexType during parsing.
          */
          content->facets = type->facets;
          type->facets = NULL;
          content->facetSet = type->facetSet;
          type->facetSet = NULL;
          
          type->contentTypeDef = content;
          if (WXS_IS_TYPE_NOT_FIXED(contentBase))
            xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
          /*
          * Fixup the newly created type. We don't need to check
          * for circularity here.
          */
          res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
          HFAILURE HERROR 
          res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
          HFAILURE HERROR 
            
      } else if ((WXS_IS_COMPLEX(baseType)) &&
          (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
          (WXS_IS_RESTRICTION(type))) {
          /*
          * SPEC (2) If <restriction> + base is a mixed <complexType> with
          * an emptiable particle, then a simple type definition which
          * restricts the <restriction>'s <simpleType> child.
          */
          if ((type->contentTypeDef == NULL) ||
            (type->contentTypeDef->baseType == NULL)) {
            /*
            * TODO: Check if this ever happens.
            */
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_INTERNAL,
                WXS_BASIC_CAST type, NULL,
                "Internal error: xmlSchemaTypeFixup, "
                "complex type '%s': the <simpleContent><restriction> "
                "is missing a <simpleType> child, but was not catched "
                "by xmlSchemaCheckSRCCT()", type->name);
            goto exit_failure;
          }
      } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
          /*
          * SPEC (3) If <extension> + base is <complexType> with
          * <simpleType> content, "...then the {content type} of that
          * complex type definition"
          */
          if (baseType->contentTypeDef == NULL) {
            /*
            * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
            * should have catched this already.
            */
            xmlSchemaPCustomErr(pctxt,
                XML_SCHEMAP_INTERNAL,
                WXS_BASIC_CAST type, NULL,
                "Internal error: xmlSchemaTypeFixup, "
                "complex type '%s': the <extension>ed base type is "
                "a complex type with no simple content type",
                type->name);
            goto exit_failure;
          }
          type->contentTypeDef = baseType->contentTypeDef;
      } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
          /*
          * SPEC (4) <extension> + base is <simpleType>
          * "... then that simple type definition"
          */
          type->contentTypeDef = baseType;
      } else {
          /*
          * TODO: Check if this ever happens.
          */
          xmlSchemaPCustomErr(pctxt,
            XML_SCHEMAP_INTERNAL,
            WXS_BASIC_CAST type, NULL,
            "Internal error: xmlSchemaTypeFixup, "
            "complex type '%s' with <simpleContent>: unhandled "
            "derivation case", type->name);
          goto exit_failure;
      }
    } else {
      int dummySequence = 0;
      xmlSchemaParticlePtr particle =
          (xmlSchemaParticlePtr) type->subtypes;
      /*
      * Corresponds to <complexType><complexContent>...
      *
      * NOTE that the effective mixed was already set during parsing of
      * <complexType> and <complexContent>; its flag value is
      * XML_SCHEMAS_TYPE_MIXED.
      *
      * Compute the "effective content":
      * (2.1.1) + (2.1.2) + (2.1.3)
      */
      if ((particle == NULL) ||
          ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
          ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
          (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
          ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
          (particle->minOccurs == 0))) &&
          ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
          if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
            /*
            * SPEC (2.1.4) "If the ·effective mixed· is true, then
            * a particle whose properties are as follows:..."
            *
            * Empty sequence model group with
            * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
            * NOTE that we sill assign it the <complexType> node to
            * somehow anchor it in the doc.
            */
            if ((particle == NULL) ||
                (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
                /*
                * Create the particle.
                */
                particle = xmlSchemaAddParticle(pctxt,
                  type->node, 1, 1);
                if (particle == NULL)
                  goto exit_failure;
                /*
                * Create the model group.
                */ /* URGENT TODO: avoid adding to pending items. */
                particle->children = (xmlSchemaTreeItemPtr)
                  xmlSchemaAddModelGroup(pctxt, pctxt->schema,
                  XML_SCHEMA_TYPE_SEQUENCE, type->node);
                if (particle->children == NULL)
                  goto exit_failure;
                
                type->subtypes = (xmlSchemaTypePtr) particle;
            }
            dummySequence = 1;
            type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
          } else {
            /*
            * SPEC (2.1.5) "otherwise empty"
            */
            type->contentType = XML_SCHEMA_CONTENT_EMPTY;
          }
      } else {
          /*
          * SPEC (2.2) "otherwise the particle corresponding to the
          * <all>, <choice>, <group> or <sequence> among the
          * [children]."
          */
          type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
      }
      /*
      * Compute the "content type".
      */
      if (WXS_IS_RESTRICTION(type)) {
          /*
          * SPEC (3.1) "If <restriction>..."
          * (3.1.1) + (3.1.2) */
          if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
            if (type->flags & XML_SCHEMAS_TYPE_MIXED)
                type->contentType = XML_SCHEMA_CONTENT_MIXED;
          }
      } else {
          /*
          * SPEC (3.2) "If <extension>..."
          */
          if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
            /*
            * SPEC (3.2.1)
            * "If the ·effective content· is empty, then the
            *  {content type} of the [...] base ..."
            */
            type->contentType = baseType->contentType;
            type->subtypes = baseType->subtypes;
            /*
            * Fixes bug #347316:
            * This is the case when the base type has a simple
            * type definition as content.
            */
            type->contentTypeDef = baseType->contentTypeDef;
            /*
            * NOTE that the effective mixed is ignored here.
            */
          } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
            /*
            * SPEC (3.2.2)
            */
            if (type->flags & XML_SCHEMAS_TYPE_MIXED)
                type->contentType = XML_SCHEMA_CONTENT_MIXED;
          } else {
            /*
            * SPEC (3.2.3)
            */
            if (type->flags & XML_SCHEMAS_TYPE_MIXED)
                type->contentType = XML_SCHEMA_CONTENT_MIXED;
                /*
                * "A model group whose {compositor} is sequence and whose
                * {particles} are..."
                */
            if ((WXS_TYPE_PARTICLE(type) != NULL) &&
                (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
                ((WXS_TYPE_PARTICLE_TERM(type))->type ==
                  XML_SCHEMA_TYPE_ALL))
            {
                /*
                * SPEC cos-all-limited (1)
                */
                xmlSchemaCustomErr(ACTXT_CAST pctxt,
                  /* TODO: error code */
                  XML_SCHEMAP_COS_ALL_LIMITED,
                  WXS_ITEM_NODE(type), NULL,
                  "The type has an 'all' model group in its "
                  "{content type} and thus cannot be derived from "
                  "a non-empty type, since this would produce a "
                  "'sequence' model group containing the 'all' "
                  "model group; 'all' model groups are not "
                  "allowed to appear inside other model groups",
                  NULL, NULL);

            } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
                (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
                ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
                  XML_SCHEMA_TYPE_ALL))
            {
                /*
                * SPEC cos-all-limited (1)
                */
                xmlSchemaCustomErr(ACTXT_CAST pctxt,
                  /* TODO: error code */
                  XML_SCHEMAP_COS_ALL_LIMITED,
                  WXS_ITEM_NODE(type), NULL,
                  "A type cannot be derived by extension from a type "
                  "which has an 'all' model group in its "
                  "{content type}, since this would produce a "
                  "'sequence' model group containing the 'all' "
                  "model group; 'all' model groups are not "
                  "allowed to appear inside other model groups",
                  NULL, NULL);

            } else if (! dummySequence) {
                xmlSchemaTreeItemPtr effectiveContent =
                  (xmlSchemaTreeItemPtr) type->subtypes;
                /*
                * Create the particle.
                */
                particle = xmlSchemaAddParticle(pctxt,
                  type->node, 1, 1);
                if (particle == NULL)
                  goto exit_failure;
                /*
                * Create the "sequence" model group.
                */
                particle->children = (xmlSchemaTreeItemPtr)
                  xmlSchemaAddModelGroup(pctxt, pctxt->schema,
                  XML_SCHEMA_TYPE_SEQUENCE, type->node);
                if (particle->children == NULL)
                  goto exit_failure;
                WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
                /*
                * SPEC "the particle of the {content type} of
                * the ... base ..."
                * Create a duplicate of the base type's particle
                * and assign its "term" to it.
                */
                particle->children->children =
                  (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
                  type->node,
                  ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
                  ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
                if (particle->children->children == NULL)
                  goto exit_failure;
                particle = (xmlSchemaParticlePtr)
                  particle->children->children;
                particle->children =
                  ((xmlSchemaParticlePtr) baseType->subtypes)->children;
                /*
                * SPEC "followed by the ·effective content·."
                */
                particle->next = effectiveContent;
                /*
                * This all will result in:
                * new-particle
                *   --> new-sequence(
                *         new-particle
                *           --> base-model,
                *         this-particle
                *         --> this-model
                *     )
                */
            } else {
                /*
                * This is the case when there is already an empty
                * <sequence> with minOccurs==maxOccurs==1.
                * Just add the base types's content type.
                * NOTE that, although we miss to add an intermediate
                * <sequence>, this should produce no difference to
                * neither the regex compilation of the content model,
                * nor to the complex type contraints.
                */
                particle->children->children =
                  (xmlSchemaTreeItemPtr) baseType->subtypes;
            }
          }
      }
    }
    /*
    * Now fixup attribute uses:
    *   - expand attr. group references
    *     - intersect attribute wildcards
    *   - inherit attribute uses of the base type
    *   - inherit or union attr. wildcards if extending
    *   - apply attr. use prohibitions if restricting
    */
    res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
    HFAILURE HERROR
    /*
    * Apply the complex type component constraints; this will not
    * check attributes, since this is done in
    * xmlSchemaFixupTypeAttributeUses().
    */
    res = xmlSchemaCheckCTComponent(pctxt, type);
    HFAILURE HERROR

#ifdef DEBUG_TYPE
    xmlSchemaDebugFixedType(pctxt, type);
#endif
    if (olderrs != pctxt->nberrors)
      return(pctxt->err);
    else
      return(0);

exit_error:
    type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
#ifdef DEBUG_TYPE
    xmlSchemaDebugFixedType(pctxt, type);
#endif
    return(pctxt->err);

exit_failure:
    type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
#ifdef DEBUG_TYPE
    xmlSchemaDebugFixedType(pctxt, type);
#endif
    return(-1);
}


/**
 * xmlSchemaTypeFixup:
 * @typeDecl:  the schema type definition
 * @ctxt:  the schema parser context
 *
 * Fixes the content model of the type.
 * URGENT TODO: We need an int result!
 */
static int
xmlSchemaTypeFixup(xmlSchemaTypePtr type,
                   xmlSchemaAbstractCtxtPtr actxt)
{
    if (type == NULL)
        return(0);
    if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
      AERROR_INT("xmlSchemaTypeFixup",
          "this function needs a parser context");
      return(-1);
    }
    if (! WXS_IS_TYPE_NOT_FIXED(type))
      return(0);
    if (type->type == XML_SCHEMA_TYPE_COMPLEX)
      return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
    else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
      return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
    return(0);
}

/**
 * xmlSchemaCheckFacet:
 * @facet:  the facet
 * @typeDecl:  the schema type definition
 * @pctxt:  the schema parser context or NULL
 * @name: the optional name of the type
 *
 * Checks and computes the values of facets.
 *
 * Returns 0 if valid, a positive error code if not valid and
 *         -1 in case of an internal or API error.
 */
int
xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
                    xmlSchemaTypePtr typeDecl,
                    xmlSchemaParserCtxtPtr pctxt,
                const xmlChar * name ATTRIBUTE_UNUSED)
{
    int ret = 0, ctxtGiven;

    if ((facet == NULL) || (typeDecl == NULL))
        return(-1);
    /*
    * TODO: will the parser context be given if used from
    * the relaxNG module?
    */
    if (pctxt == NULL)
      ctxtGiven = 0;
    else
      ctxtGiven = 1;

    switch (facet->type) {
        case XML_SCHEMA_FACET_MININCLUSIVE:
        case XML_SCHEMA_FACET_MINEXCLUSIVE:
        case XML_SCHEMA_FACET_MAXINCLUSIVE:
        case XML_SCHEMA_FACET_MAXEXCLUSIVE:
      case XML_SCHEMA_FACET_ENUMERATION: {
                /*
                 * Okay we need to validate the value
                 * at that point.
                 */
            xmlSchemaTypePtr base;

            /* 4.3.5.5 Constraints on enumeration Schema Components
            * Schema Component Constraint: enumeration valid restriction
            * It is an ·error· if any member of {value} is not in the
            * ·value space· of {base type definition}.
            *
            * minInclusive, maxInclusive, minExclusive, maxExclusive:
            * The value ·must· be in the
            * ·value space· of the ·base type·.
            */
            /*
            * This function is intended to deliver a compiled value
            * on the facet. In this implementation of XML Schemata the
            * type holding a facet, won't be a built-in type.
            * Thus to ensure that other API
            * calls (relaxng) do work, if the given type is a built-in
            * type, we will assume that the given built-in type *is
            * already* the base type.
            */
            if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
                base = typeDecl->baseType;
                if (base == NULL) {
                  PERROR_INT("xmlSchemaCheckFacet",
                      "a type user derived type has no base type");
                  return (-1);
                }
            } else
                base = typeDecl;
                       
            if (! ctxtGiven) {
                /*
                * A context is needed if called from RelaxNG.
                */                
                pctxt = xmlSchemaNewParserCtxt("*");
                if (pctxt == NULL)
                  return (-1);
            }
            /*
            * NOTE: This call does not check the content nodes,
            * since they are not available:
            * facet->node is just the node holding the facet
            * definition, *not* the attribute holding the *value*
            * of the facet.
            */          
            ret = xmlSchemaVCheckCVCSimpleType(
                ACTXT_CAST pctxt, facet->node, base,
                facet->value, &(facet->val), 1, 1, 0);
                if (ret != 0) {
                if (ret < 0) {
                  /* No error message for RelaxNG. */
                  if (ctxtGiven) {                  
                      xmlSchemaCustomErr(ACTXT_CAST pctxt,
                        XML_SCHEMAP_INTERNAL, facet->node, NULL,
                        "Internal error: xmlSchemaCheckFacet, " 
                        "failed to validate the value '%s' of the "
                        "facet '%s' against the base type",
                        facet->value, xmlSchemaFacetTypeToString(facet->type));
                  }
                  goto internal_error;
                }
                ret = XML_SCHEMAP_INVALID_FACET_VALUE;
                /* No error message for RelaxNG. */
                if (ctxtGiven) {
                  xmlChar *str = NULL;

                  xmlSchemaCustomErr(ACTXT_CAST pctxt,
                      ret, facet->node, WXS_BASIC_CAST facet,
                      "The value '%s' of the facet does not validate "
                      "against the base type '%s'",
                      facet->value,
                      xmlSchemaFormatQName(&str,
                        base->targetNamespace, base->name));
                  FREE_AND_NULL(str);
                }
                goto exit;
                } else if (facet->val == NULL) {
                if (ctxtGiven) {
                  PERROR_INT("xmlSchemaCheckFacet",
                      "value was not computed");
                }
                TODO
            }
                break;
            }
        case XML_SCHEMA_FACET_PATTERN:
            facet->regexp = xmlRegexpCompile(facet->value);
            if (facet->regexp == NULL) {
            ret = XML_SCHEMAP_REGEXP_INVALID;
            /* No error message for RelaxNG. */
            if (ctxtGiven) {
                xmlSchemaCustomErr(ACTXT_CAST pctxt,
                  ret, facet->node, WXS_BASIC_CAST typeDecl,
                  "The value '%s' of the facet 'pattern' is not a "
                  "valid regular expression",
                  facet->value, NULL);
            }
            }
            break;
        case XML_SCHEMA_FACET_TOTALDIGITS:
        case XML_SCHEMA_FACET_FRACTIONDIGITS:
        case XML_SCHEMA_FACET_LENGTH:
        case XML_SCHEMA_FACET_MAXLENGTH:
        case XML_SCHEMA_FACET_MINLENGTH:

          if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
            ret = xmlSchemaValidatePredefinedType(
                xmlSchemaGetBuiltInType(XML_SCHEMAS_PINTEGER),
                facet->value, &(facet->val));
          } else {
            ret = xmlSchemaValidatePredefinedType(
                xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
                facet->value, &(facet->val));
          }
          if (ret != 0) {
            if (ret < 0) {
                /* No error message for RelaxNG. */
                if (ctxtGiven) {
                  PERROR_INT("xmlSchemaCheckFacet",
                      "validating facet value");
                }
                goto internal_error;
            }
            ret = XML_SCHEMAP_INVALID_FACET_VALUE;
            /* No error message for RelaxNG. */
            if (ctxtGiven) {
                /* error code */
                xmlSchemaCustomErr4(ACTXT_CAST pctxt,
                  ret, facet->node, WXS_BASIC_CAST typeDecl,
                  "The value '%s' of the facet '%s' is not a valid '%s'",                 
                  facet->value,
                  xmlSchemaFacetTypeToString(facet->type),
                  (facet->type != XML_SCHEMA_FACET_TOTALDIGITS) ? 
                      BAD_CAST "nonNegativeInteger" :
                      BAD_CAST "positiveInteger",
                  NULL);
            }
          }
          break;
            
        case XML_SCHEMA_FACET_WHITESPACE:{
                if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
                    facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
                } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
                    facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
                } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
                    facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
                } else {
                ret = XML_SCHEMAP_INVALID_FACET_VALUE;
                    /* No error message for RelaxNG. */
                if (ctxtGiven) {
                  /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
                  xmlSchemaCustomErr(ACTXT_CAST pctxt,
                      ret, facet->node, WXS_BASIC_CAST typeDecl,
                      "The value '%s' of the facet 'whitespace' is not "
                      "valid", facet->value, NULL);
                    }
                }
            }
        default:
            break;
    }
exit:
    if ((! ctxtGiven) && (pctxt != NULL))
      xmlSchemaFreeParserCtxt(pctxt);
    return (ret);
internal_error:
    if ((! ctxtGiven) && (pctxt != NULL))
      xmlSchemaFreeParserCtxt(pctxt);
    return (-1);
}

/**
 * xmlSchemaCheckFacetValues:
 * @typeDecl:  the schema type definition
 * @ctxt:  the schema parser context
 *
 * Checks the default values types, especially for facets
 */
static int
xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
                    xmlSchemaParserCtxtPtr pctxt)
{
    int res, olderrs = pctxt->nberrors;
    const xmlChar *name = typeDecl->name;
    /*
    * NOTE: It is intended to use the facets list, instead
    * of facetSet.
    */
    if (typeDecl->facets != NULL) {
      xmlSchemaFacetPtr facet = typeDecl->facets;

      /*
      * Temporarily assign the "schema" to the validation context
      * of the parser context. This is needed for NOTATION validation.
      */
      if (pctxt->vctxt == NULL) {
          if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
            return(-1);
      }
      pctxt->vctxt->schema = pctxt->schema;
      while (facet != NULL) {
          res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
          HFAILURE
          facet = facet->next;
      }
      pctxt->vctxt->schema = NULL;
    }
    if (olderrs != pctxt->nberrors)
      return(pctxt->err);
    return(0);
exit_failure:
    return(-1);
}

/**
 * xmlSchemaGetCircModelGrDefRef:
 * @ctxtMGroup: the searched model group
 * @selfMGroup: the second searched model group
 * @particle: the first particle
 *
 * This one is intended to be used by
 * xmlSchemaCheckGroupDefCircular only.
 *
 * Returns the particle with the circular model group definition reference,
 * otherwise NULL.
 */
static xmlSchemaTreeItemPtr
xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
                        xmlSchemaTreeItemPtr particle)
{
    xmlSchemaTreeItemPtr circ = NULL;
    xmlSchemaTreeItemPtr term;
    xmlSchemaModelGroupDefPtr gdef;

    for (; particle != NULL; particle = particle->next) {
      term = particle->children;
      if (term == NULL)
          continue;
      switch (term->type) {
          case XML_SCHEMA_TYPE_GROUP:
            gdef = (xmlSchemaModelGroupDefPtr) term;
            if (gdef == groupDef)
                return (particle);
            /*
            * Mark this model group definition to avoid infinite
            * recursion on circular references not yet examined.
            */
            if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
                continue;
            if (gdef->children != NULL) {
                gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
                circ = xmlSchemaGetCircModelGrDefRef(groupDef,
                  gdef->children->children);
                gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
                if (circ != NULL)
                  return (circ);
            }
            break;
          case XML_SCHEMA_TYPE_SEQUENCE:
          case XML_SCHEMA_TYPE_CHOICE:
          case XML_SCHEMA_TYPE_ALL:
            circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
            if (circ != NULL)
                return (circ);
            break;
          default:
            break;
      }
    }
    return (NULL);
}

/**
 * xmlSchemaCheckGroupDefCircular:
 * @item:  the model group definition
 * @ctxt:  the parser context
 * @name:  the name
 *
 * Checks for circular references to model group definitions.
 */
static void
xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
                         xmlSchemaParserCtxtPtr ctxt)
{
    /*
    * Schema Component Constraint: Model Group Correct
    * 2 Circular groups are disallowed. That is, within the {particles}
    * of a group there must not be at any depth a particle whose {term}
    * is the group itself.
    */
    if ((item == NULL) ||
      (item->type != XML_SCHEMA_TYPE_GROUP) ||
      (item->children == NULL))
      return;
    {
      xmlSchemaTreeItemPtr circ;

      circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
      if (circ != NULL) {
          xmlChar *str = NULL;
          /*
          * TODO: The error report is not adequate: this constraint
          * is defined for model groups but not definitions, but since
          * there cannot be any circular model groups without a model group
          * definition (if not using a construction API), we check those
          * defintions only.
          */
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_MG_PROPS_CORRECT_2,
            NULL, WXS_ITEM_NODE(circ),
            "Circular reference to the model group definition '%s' "
            "defined", xmlSchemaFormatQName(&str,
                item->targetNamespace, item->name));
          FREE_AND_NULL(str)
          /*
          * NOTE: We will cut the reference to avoid further
          * confusion of the processor. This is a fatal error.
          */
          circ->children = NULL;
      }
    }
}

/**
 * xmlSchemaModelGroupToModelGroupDefFixup:
 * @ctxt:  the parser context
 * @mg:  the model group
 *
 * Assigns the model group of model group definitions to the "term"
 * of the referencing particle.
 * In xmlSchemaResolveModelGroupParticleReferences the model group
 * definitions were assigned to the "term", since needed for the
 * circularity check.
 *
 * Schema Component Constraint:
 *     All Group Limited (cos-all-limited) (1.2)
 */
static void
xmlSchemaModelGroupToModelGroupDefFixup(
    xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
    xmlSchemaModelGroupPtr mg)
{
    xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);

    while (particle != NULL) {
      if ((WXS_PARTICLE_TERM(particle) == NULL) ||
          ((WXS_PARTICLE_TERM(particle))->type !=
            XML_SCHEMA_TYPE_GROUP))
      {
          particle = WXS_PTC_CAST particle->next;
          continue;
      } 
      if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
          /*
          * TODO: Remove the particle.
          */
          WXS_PARTICLE_TERM(particle) = NULL;
          particle = WXS_PTC_CAST particle->next;
          continue;
      }
      /*
      * Assign the model group to the {term} of the particle.
      */
      WXS_PARTICLE_TERM(particle) =
          WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));

      particle = WXS_PTC_CAST particle->next;
    }
}

/**
 * xmlSchemaCheckAttrGroupCircularRecur:
 * @ctxtGr: the searched attribute group
 * @attr: the current attribute list to be processed
 *
 * This one is intended to be used by
 * xmlSchemaCheckAttrGroupCircular only.
 *
 * Returns the circular attribute grou reference, otherwise NULL.
 */
static xmlSchemaQNameRefPtr
xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
                             xmlSchemaItemListPtr list)
{
    xmlSchemaAttributeGroupPtr gr;
    xmlSchemaQNameRefPtr ref, circ;
    int i;
    /*
    * We will search for an attribute group reference which
    * references the context attribute group.
    */
    for (i = 0; i < list->nbItems; i++) {
      ref = list->items[i];
      if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
          (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
          (ref->item != NULL))
      {
          gr = WXS_ATTR_GROUP_CAST ref->item;
          if (gr == ctxtGr)
            return(ref);
          if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
            continue;       
          /*
          * Mark as visited to avoid infinite recursion on
          * circular references not yet examined.
          */
          if ((gr->attrUses) &&
            (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
          {
            gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
            circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
                (xmlSchemaItemListPtr) gr->attrUses);       
            gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
            if (circ != NULL)
                return (circ);
          }
          
      }
    }
    return (NULL);
}

/**
 * xmlSchemaCheckAttrGroupCircular:
 * attrGr:  the attribute group definition
 * @ctxt:  the parser context
 * @name:  the name
 *
 * Checks for circular references of attribute groups.
 */
static int
xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
                        xmlSchemaParserCtxtPtr ctxt)
{
    /*
    * Schema Representation Constraint:
    * Attribute Group Definition Representation OK
    * 3 Circular group reference is disallowed outside <redefine>.
    * That is, unless this element information item's parent is
    * <redefine>, then among the [children], if any, there must
    * not be an <attributeGroup> with ref [attribute] which resolves
    * to the component corresponding to this <attributeGroup>. Indirect
    * circularity is also ruled out. That is, when QName resolution
    * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
    * any <attributeGroup>s with a ref [attribute] among the [children],
    * it must not be the case that a ·QName· is encountered at any depth
    * which resolves to the component corresponding to this <attributeGroup>.
    */
    if (attrGr->attrUses == NULL)
      return(0);
    else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
      return(0);
    else {
      xmlSchemaQNameRefPtr circ;
      
      circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
          (xmlSchemaItemListPtr) attrGr->attrUses);   
      if (circ != NULL) {
          xmlChar *str = NULL;
          /*
          * TODO: Report the referenced attr group as QName.
          */
          xmlSchemaPCustomErr(ctxt,
            XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
            NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
            "Circular reference to the attribute group '%s' "
            "defined", xmlSchemaGetComponentQName(&str, attrGr));
          FREE_AND_NULL(str);
          /*
          * NOTE: We will cut the reference to avoid further
          * confusion of the processor.
          * BADSPEC TODO: The spec should define how to process in this case.<