Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added subcommand nextexpected to the info method of schema commands.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | schema
Files: files | file ages | folders
SHA3-256: c9fd675e9f9e6ff1955c298ab21b337864c58904fb6ba71649d61f4d7a0286b7
User & Date: rolf 2019-10-09 16:40:12
Context
2019-10-11
00:29
Added a few tests to document the behaviour in case of validation against forward defined elements or refs. check-in: bb898f3778 user: rolf tags: schema
2019-10-10
01:50
Started info subcommand pastexpected. check-in: 78b0aac051 user: rolf tags: wip
2019-10-09
16:40
Added subcommand nextexpected to the info method of schema commands. check-in: c9fd675e9f user: rolf tags: schema
16:25
Settled for a list of unique possible next content (which special entries for any and text) in unspecific order as result of the nextexpected subcommand of the info method. Closed-Leaf check-in: 35c7ad95de user: rolf tags: wip
2019-08-23
22:56
Added documentation to few commands, implemented earlier. check-in: 8fce09e002 user: rolf tags: schema
Changes

Changes to doc/schema.xml.

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
...
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
...
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
    particle specified by the command is expected. The valid values
    for a <m>quant</m> argument are:</p>

    <optlist>
      <optdef>
        <optname>!</optname>
        <desc>The content particle must occur exactly once in valid
        documents. This is the default, if a quantifier is
        omitted.</desc>
      </optdef>
    
      <optdef>
        <optname>?</optname>
        <desc>The content particle must occur at most once in valid
        documents.</desc>
      </optdef>
................................................................................
    case of the mixed command and to ! for all other commands.</p>
  </section>

  <section>
    <title>Text constraint scripts</title>

    <p>Text - parsed character data, as XML calles it - must sometimes
    have to be of a certain kind, must comply to some rules etc to be
    valid. The text constraint script arguments to the text,
    attribute, nsattribute and deftext commands allow the following
    text constraint commands to check text for certain properties.</p>

    <p>The text constraint commands are:</p>

    <commandlist>
      <commanddef>
        <command><cmd>integer</cmd> <m>?(xsd|tcl)?</m></command>
        <desc>This text constraint match if the text value could be
................................................................................
        is <m>xsd</m> then the constraint match if the value is a
        valid xsd:integer. Without argument <m>xsd</m> is the
        default.</desc>
      </commanddef>

      <commanddef>
        <command><cmd>fixed</cmd> <m>value</m></command>
        <desc>The text constraint only matches if the text value is
        string equal to the given value.</desc>
      </commanddef>
      
      <commanddef>
        <command><cmd>tcl</cmd> <m>tclcmd</m> <m>?arg arg ...?</m></command>
        <desc>Evaluates the tcl script <m>tclcmd arg arg ... </m> and
        the text to validate appended to the argument list. The return
................................................................................
            as last argument. This call must return a valid tcl list,
            which elements are tested..</dd>
        </dl>
        <p>The default in case no split type argument is given is
        <m>whitespace</m>.</p></desc>
      </commanddef>
      <commanddef>
        <command><cmd>id <m>?keySpace?</m></cmd></command>
        <desc>This text constraint command marks the text as a
        document wide ID (to be referenced by an idref). Every ID
        value within a document must be unique. It isn't an error if
        the ID isn't actually referenced within the document. The
        optional argument <m>keySpace</m> does all this for a named
        key space. The key space "" (the empty sting) is another key
        space as the <m>id</m> command without keySpace
        argument.</desc>
      </commanddef>
      <commanddef>
        <command><cmd>idref <m>?keySpace?</m></cmd></command>
        <desc>This text constraint command expects the text to be a
        reference to an ID within the document. The referenced ID may
        be later in the document, that the reference. Several
        references within the document to one ID are possible.</desc>
      </commanddef>
      <commanddef>
        <command><cmd>base64</cmd></command>







<
|







 







|
|
|
|







 







|







 







|










|







369
370
371
372
373
374
375

376
377
378
379
380
381
382
383
...
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
...
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
...
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
    particle specified by the command is expected. The valid values
    for a <m>quant</m> argument are:</p>

    <optlist>
      <optdef>
        <optname>!</optname>
        <desc>The content particle must occur exactly once in valid

        documents.</desc>
      </optdef>
    
      <optdef>
        <optname>?</optname>
        <desc>The content particle must occur at most once in valid
        documents.</desc>
      </optdef>
................................................................................
    case of the mixed command and to ! for all other commands.</p>
  </section>

  <section>
    <title>Text constraint scripts</title>

    <p>Text - parsed character data, as XML calles it - must sometimes
    be of a certain kind, must comply to some rules etc to be valid.
    The text constraint script arguments to the text, attribute,
    nsattribute and deftext commands allow the following text
    constraint commands to check text for certain properties.</p>

    <p>The text constraint commands are:</p>

    <commandlist>
      <commanddef>
        <command><cmd>integer</cmd> <m>?(xsd|tcl)?</m></command>
        <desc>This text constraint match if the text value could be
................................................................................
        is <m>xsd</m> then the constraint match if the value is a
        valid xsd:integer. Without argument <m>xsd</m> is the
        default.</desc>
      </commanddef>

      <commanddef>
        <command><cmd>fixed</cmd> <m>value</m></command>
        <desc>The text constraint only match if the text value is
        string equal to the given value.</desc>
      </commanddef>
      
      <commanddef>
        <command><cmd>tcl</cmd> <m>tclcmd</m> <m>?arg arg ...?</m></command>
        <desc>Evaluates the tcl script <m>tclcmd arg arg ... </m> and
        the text to validate appended to the argument list. The return
................................................................................
            as last argument. This call must return a valid tcl list,
            which elements are tested..</dd>
        </dl>
        <p>The default in case no split type argument is given is
        <m>whitespace</m>.</p></desc>
      </commanddef>
      <commanddef>
        <command><cmd>id</cmd> <m>?keySpace?</m></command>
        <desc>This text constraint command marks the text as a
        document wide ID (to be referenced by an idref). Every ID
        value within a document must be unique. It isn't an error if
        the ID isn't actually referenced within the document. The
        optional argument <m>keySpace</m> does all this for a named
        key space. The key space "" (the empty sting) is another key
        space as the <m>id</m> command without keySpace
        argument.</desc>
      </commanddef>
      <commanddef>
        <command><cmd>idref</cmd> <m>?keySpace?</m></command>
        <desc>This text constraint command expects the text to be a
        reference to an ID within the document. The referenced ID may
        be later in the document, that the reference. Several
        references within the document to one ID are possible.</desc>
      </commanddef>
      <commanddef>
        <command><cmd>base64</cmd></command>

Changes to generic/schema.c.

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

137
138
139
140
141
142
143
...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
...
453
454
455
456
457
458
459



460
461
462
463
464
465
466
...
743
744
745
746
747
748
749






















750
751
752
753

754
755
756
757
758
759
760
761
762
763
764
765
766

767
768
769
770
771
772
773
774










775
776
777
778
779
780
781
...
894
895
896
897
898
899
900


901
902
903
904
905
906
907
908

909





910
911
912


913
914
915
916
917
918
919
...
966
967
968
969
970
971
972

973
974
975
976
977
978
979
980
981
...
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
....
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051

1052
1053
1054
1055
1056
1057
1058
....
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
....
1315
1316
1317
1318
1319
1320
1321

1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
....
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
....
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
....
1765
1766
1767
1768
1769
1770
1771

1772
1773
1774
1775
1776
1777
1778
....
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
....
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
....
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
....
2716
2717
2718
2719
2720
2721
2722
2723














































































2724












































































































































2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737




2738
2739
2740
2741

2742
2743
2744

2745
2746
2747
2748
2749
2750
2751
....
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
....
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
....
2836
2837
2838
2839
2840
2841
2842

2843






2844



















































































2845
2846
2847
2848
2849
2850
2851
....
2991
2992
2993
2994
2995
2996
2997


2998
2999
3000
3001
3002
3003
3004
....
3192
3193
3194
3195
3196
3197
3198
3199
3200

3201
3202
3203
3204
3205
3206
3207
....
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
....
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
#if defined(DEBUG) || defined(DDEBUG)
static char *Schema_CP_Type2str[] = {
    "ANY",
    "NAME",
    "CHOICE",
    "INTERLEAVE",
    "PATTERN",
    "TEXT"
    "VIRTUAL",
    "KEYSPACE_START",
    "KEYSPACE_END"
};
static char *Schema_Quant_Type2str[] = {
    "ONE",
    "OPT",
    "REP",
    "PLUS",
    "NM"

};
#endif


#ifndef TCL_THREADS
  static SchemaData *activeSchemaData = 0;
# define GETASI activeSchemaData
................................................................................
        se->activeChild = ac + 1;                 \
        se->hasMatched = 0;                       \
    } else {                                      \
        se->activeChild = ac;                     \
        se->hasMatched = 1;                       \
    }

#define serializeElementName(rObj, cp)                  \
    rObj = Tcl_NewObj();                                \
    if (cp->namespace) {                                \
        Tcl_SetStringObj (rObj, cp->namespace, -1);     \
        Tcl_AppendToObj (rObj, ":", 1);                 \
    }                                                   \
    Tcl_AppendToObj (rObj, cp->name, -1);

#define S(str)  str, sizeof (str) -1

static SchemaCP*
initSchemaCP (
    Schema_CP_Type type,
    void *namespace,
    char *name
................................................................................
                FREE (pattern->attrs[i]);
            }
            FREE (pattern->attrs);
        }
        freedomKeyConstraints (pattern->domKeys);
        break;
    }



    FREE (pattern);
}

static SchemaData*
initSchemaData (
    Tcl_Obj *cmdNameObj)
{
................................................................................
    }
    if (savedCP) {
        sdata->cp = savedCP;
        sdata->contentSize = savedContenSize;
    }
}























static void
pushToStack (
    SchemaData *sdata,
    SchemaCP *pattern

    )
{
    SchemaValidationStack *stackElm, *se;

    DBG(fprintf(stderr, "push to Stack:\n");serializeCP(pattern));
    if (sdata->stackPool) {
        stackElm = sdata->stackPool;
        sdata->stackPool = stackElm->down;
    } else {
        stackElm = TMALLOC (SchemaValidationStack);
    }
    memset (stackElm, 0, sizeof (SchemaValidationStack));
    se = sdata->stack;

    stackElm->down = se;
    stackElm->pattern = pattern;
    if (pattern->type == SCHEMA_CTYPE_INTERLEAVE) {
        stackElm->interleaveState = MALLOC (sizeof (int) * pattern->nc);
        memset (stackElm->interleaveState, 0, sizeof (int) * pattern->nc);
    }
    sdata->stack = stackElm;
}











static void
popStack (
    SchemaData *sdata
    )
{
    SchemaValidationStack *se;
................................................................................
        if (!(sc->constraint) (interp, sc->constraintData, text)) {
            return 0;
        }
    }
    return 1;
}



static int
evalVirtual (
    Tcl_Interp *interp,
    SchemaData *sdata,
    SchemaCP *cp
    )
{
    int rc;







    cp->content[cp->nc-1] = (SchemaCP *) sdata->self;
    rc = Tcl_EvalObjv (interp, cp->nc, (Tcl_Obj **) cp->content,
                       TCL_EVAL_GLOBAL);


    if (rc != TCL_OK) {
        sdata->evalError = 1;
        return 0;
    }
    return 1;
}

................................................................................

            case SCHEMA_CTYPE_NAME:
                DBG(fprintf (stderr, "name: %s ns: %s candidate name: %s "
                             "candidate ns: %s\n", name, namespace,
                             candidate->name, candidate->namespace));
                if (candidate->name == name
                    && candidate->namespace == namespace) {

                    updateStack (se, cp, ac);
                    pushToStack (sdata, candidate);
                    return 1;
                }
                break;

            case SCHEMA_CTYPE_CHOICE:
                for (i = 0; i < candidate->nc; i++) {
                    icp = candidate->content[i];
................................................................................
                        updateStack (se, cp, ac);
                        sdata->skipDeep = 1;
                        return 1;

                    case SCHEMA_CTYPE_NAME:
                        if (icp->name == name
                            && icp->namespace == namespace) {
                            pushToStack (sdata, icp);
                            updateStack (se, cp, ac);
                            return 1;
                        }
                        break;

                    case SCHEMA_CTYPE_CHOICE:
                        Tcl_Panic ("MIXED or CHOICE child of MIXED or CHOICE");

                    case SCHEMA_CTYPE_INTERLEAVE:
                    case SCHEMA_CTYPE_PATTERN:
                        pushToStack (sdata, icp);
                        rc = matchElementStart (interp, sdata, name, namespace);
                        if (rc == 1) {
                            updateStack (se, cp, ac);
                            return 1;
                        }
                        popStack (sdata);
                        if (rc == -1) mayskip = 1;
................................................................................
                    }
                    if (!mayskip && mayMiss (candidate->quants[i]))
                        mayskip = 1;
                }
                break;

            case SCHEMA_CTYPE_VIRTUAL:
                if (evalVirtual (interp, sdata, candidate)) {
                    /* Virtual contraints are always quant ONE, so
                     * that the virtual constraints are called while
                     * looking if an element can end. Therefor we use
                     * here the already present mayskip mechanism to
                     * try further, after calling the tcl script. */
                    mayskip = 1;
                    break;
                }
                else return 0;

            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                pushToStack (sdata, candidate);
                rc = matchElementStart (interp, sdata, name, namespace);
                if (rc == 1) {
                    updateStack (se, cp, ac);
                    return 1;
                }
                popStack (sdata);

                break;

            case SCHEMA_CTYPE_KEYSPACE_END:
                candidate->keySpace->active--;
                if (!candidate->keySpace->active) {
                    if (candidate->keySpace->unknownIDrefs) {
                        if (!recover (interp, sdata, S("UNKNOWN_KEYREF"),
................................................................................
                se->hasMatched = 1;
                se->interleaveState[i] = 1;
                return 1;

            case SCHEMA_CTYPE_NAME:
                if (icp->name == name
                    && icp->namespace == namespace) {
                    pushToStack (sdata, icp);
                    se->hasMatched = 1;
                    se->interleaveState[i] = 1;
                    return 1;
                }
                break;

            case SCHEMA_CTYPE_CHOICE:
                Tcl_Panic ("MIXED or CHOICE child of INTERLEAVE");

            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                pushToStack (sdata, icp);
                rc = matchElementStart (interp, sdata, name, namespace);
                if (rc == 1) {
                    se->hasMatched = 1;
                    se->interleaveState[i] = 1;
                    return 1;
                }
                popStack (sdata);
................................................................................
            if (se->pattern->namespace) {
                Tcl_AppendResult (interp, namespace, ":", NULL);
            }
            Tcl_AppendResult (interp, name, "\"", NULL);
            return TCL_ERROR;
        }
    } else {

        if (!pattern) {
            if (recover (interp, sdata, S("UNKNOWN_ROOT_ELEMENT"), 0, 0)) {
                sdata->skipDeep = 1;
                return TCL_OK;
            }
            SetResult ("Unknown element");
            return TCL_ERROR;
        }
        pushToStack (sdata, pattern);
        sdata->validationState = VALIDATION_STARTED;
        return TCL_OK;
    }

    /* The normal case: we're inside the tree */
    rc = matchElementStart (interp, sdata, (char *) namePtr, namespacePtr);
    while (rc == -1) {
        popStack (sdata);
................................................................................

                    case SCHEMA_CTYPE_NAME:
                    case SCHEMA_CTYPE_ANY:
                        continue;
                        
                    case SCHEMA_CTYPE_INTERLEAVE:
                    case SCHEMA_CTYPE_PATTERN:
                        pushToStack (sdata, ic);
                        if (checkElementEnd (interp, sdata)) {
                            mayMiss = 1;
                        }
                        popStack (sdata);
                        break;
                        
                    case SCHEMA_CTYPE_KEYSPACE_END:
................................................................................
                if (mayMiss) break;
                if (!recover (interp, sdata, S("MISSING_ONE_OF_CHOICE"), 0, 0)) {
                    return 0;
                }
                break;
                
            case SCHEMA_CTYPE_VIRTUAL:
                if (evalVirtual (interp, sdata, cp->content[ac])) break;
                else return 0;
                
            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                pushToStack (sdata, cp->content[ac]);
                rc = checkElementEnd (interp, sdata);
                popStack (sdata);
                if (rc) break;
                return 0;
                
            case SCHEMA_CTYPE_ANY:
                if (recover (interp, sdata, S("MISSING_ANY"), 0, 0)) {
................................................................................
    ) 
{
    Tcl_HashEntry *h, *h1;
    Tcl_HashSearch search, search1;
    int haveErrMsg = 0;
    SchemaDocKey *dk;


    if (sdata->unknownIDrefs) {
        haveErrMsg = 1;
        SetResult ("References to unknown IDs:");
        for (h = Tcl_FirstHashEntry (&sdata->ids, &search);
             h != NULL;
             h = Tcl_NextHashEntry (&search)) {
            if (Tcl_GetHashValue (h) == 0) {
................................................................................

                        case SCHEMA_CTYPE_NAME:
                        case SCHEMA_CTYPE_ANY:
                            break;

                        case SCHEMA_CTYPE_INTERLEAVE:
                        case SCHEMA_CTYPE_PATTERN:
                            pushToStack (sdata, ic);
                            if (matchText (interp, sdata, text)) {
                                updateStack (se, cp, ac);
                                return 1;
                            }
                            popStack (sdata);
                            break;

                        case SCHEMA_CTYPE_VIRTUAL:
                            if (!evalVirtual (interp, sdata, ic)) return 0;
                            break;
                            
                        case SCHEMA_CTYPE_CHOICE:
                            Tcl_Panic ("MIXED or CHOICE child of MIXED or CHOICE");

                        case SCHEMA_CTYPE_KEYSPACE_END:
                        case SCHEMA_CTYPE_KEYSPACE:
                            Tcl_Panic ("Keyspace constrain in MIXED or CHOICE");
................................................................................
                        SetResult ("Unexpected text content");
                        return 0;
                    }
                    break;

                case SCHEMA_CTYPE_INTERLEAVE:
                case SCHEMA_CTYPE_PATTERN:
                    pushToStack (sdata, candidate);
                    if (matchText (interp, sdata, text)) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
                    popStack (sdata);
                    if (mustMatch (cp->quants[ac], hm)) {
                        SetResult ("Unexpected text content");
                        return 0;
                    }
                    break;

                case SCHEMA_CTYPE_VIRTUAL:
                    if (!evalVirtual (interp, sdata, candidate)) return 0;
                    break;

                case SCHEMA_CTYPE_KEYSPACE:
                    if (!cp->content[ac]->keySpace->active) {
                        Tcl_InitHashTable (&cp->content[ac]->keySpace->ids,
                                           TCL_STRING_KEYS);
                        cp->content[ac]->keySpace->active = 1;
................................................................................

                case SCHEMA_CTYPE_NAME:
                case SCHEMA_CTYPE_ANY:
                    break;

                case SCHEMA_CTYPE_INTERLEAVE:
                case SCHEMA_CTYPE_PATTERN:
                    pushToStack (sdata, ic);
                    if (matchText (interp, sdata, text)) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
                    popStack (sdata);
                    break;

................................................................................
    sdata->cp = savedCP;
    sdata->contentSize = savedContenSize;
    if (sdata->cp && !sdata->isAttributeConstaint && cp->nc) {
        sdata->cp->flags |= CONSTRAINT_TEXT_CHILD;
    }
    return result;
}















































































static int












































































































































schemaInstanceInfoCmd (
    SchemaData *sdata,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
{
    int methodIndex;
    Tcl_HashEntry *h;
    Tcl_HashSearch search;
    SchemaCP *cp;
    SchemaValidationStack *se;
    Tcl_Obj *resultObj, *elmObj;




    
    static const char *schemaInstanceInfoMethods[] = {
        "validationstate", "vstate", "defelements", "stack", "toplevel",
        NULL

    };
    enum schemaInstanceInfoMethod {
        m_validationstate, m_vstate, m_defelements, m_stack, m_toplevel

    };

    static const char *schemaInstanceInfoStackMethods[] = {
        "top", "inside", NULL
    };
    enum schemaInstanceInfoStackMethod {
        m_top, m_inside
................................................................................
            break;
        default:
            SetResult ("Internal error: Invalid validation state");
            return TCL_ERROR;
        }
        break;
        
    case m_defelements:
        if (objc != 2) {
            Tcl_WrongNumArgs (interp, 1, objv, "defelements");
            return TCL_ERROR;
        }
        resultObj = Tcl_GetObjResult (interp);
        for (h = Tcl_FirstHashEntry (&sdata->element, &search);
             h != NULL;
             h = Tcl_NextHashEntry (&search)) {
            cp = (SchemaCP *) Tcl_GetHashValue (h);
            if (!cp) continue;
            if (cp->flags & FORWARD_PATTERN_DEF
                || cp->flags & PLACEHOLDER_PATTERN_DEF) continue;
            serializeElementName (elmObj, cp);
            if (Tcl_ListObjAppendElement (interp, resultObj, elmObj) != TCL_OK)
                return TCL_ERROR;
        }
        break;

    case m_stack:
        if (Tcl_GetIndexFromObj (interp, objv[2],
                                 schemaInstanceInfoStackMethods,
                                 "method", 0, &methodIndex)
            != TCL_OK) {
................................................................................
                Tcl_ResetResult (interp);
                return TCL_OK;
            }
            se = sdata->stack;
            while (se->pattern->type != SCHEMA_CTYPE_NAME) {
                se = se->down;
            }
            serializeElementName (elmObj, se->pattern);
            Tcl_SetObjResult (interp, elmObj);
            return TCL_OK;
            break;
        }
        
    case m_toplevel:
        if (objc != 2) {
            Tcl_WrongNumArgs (interp, 1, objv, "no argument expected");
................................................................................
            return TCL_ERROR;
        }
        if (!sdata->defineToplevel && sdata->currentEvals > 1) {
            SetBooleanResult (0);
        } else {
            SetBooleanResult (1);
        }

    }






    



















































































    return TCL_OK;
}

int
schemaInstanceCmd (
    ClientData clientData,
    Tcl_Interp *interp,
................................................................................
            if (forwardDef) {
                if (pattern->flags & FORWARD_PATTERN_DEF) {
                    sdata->forwardPatternDefs--;
                    pattern->flags &= ~FORWARD_PATTERN_DEF;
                }
                pattern->flags &= ~PLACEHOLDER_PATTERN_DEF;
            }


        } else {
            cleanupLastPattern (sdata, savedNumPatternList);
        }
        sdata->defineToplevel = savedDefineToplevel;
        sdata->currentNamespace = savedNamespacePtr;
        if (!savedDefineToplevel) {
            SETASI(savedsdata);
................................................................................
        } else {
            node = tcldom_getNodeFromObj (interp, objv[2]);
            if (!node) {
                SetResult ("The second argument must be either a "
                           "document or a element node");
                return TCL_ERROR;
            }
            sdata->validationState = VALIDATION_STARTED;
        }

        if (validateDOM (interp, sdata, node) == TCL_OK) {
            SetBooleanResult (1);
            if (objc == 4) {
                Tcl_SetVar (interp, Tcl_GetString (objv[3]), "", 0);
            }
        } else {
            if (objc == 4) {
................................................................................
    Tcl_Obj *const objv[]
    )
{
    SchemaData *sdata = GETASI;
    SchemaCP *pattern;
    SchemaQuant quant;
    char *ns = NULL;
    int n, m, hnew;
    Tcl_HashEntry *h;

    CHECK_SI
    CHECK_TOPLEVEL
    checkNrArgs (1,3,"?namespace? ?quant?");
    if (objc == 1) {
        quant = SCHEMA_CQUANT_ONE;
    } else if (objc == 2) {    
        quant = getQuant (interp, sdata, objv[1], &n, &m);
        if (quant == SCHEMA_CQUANT_ERROR) {
            h = Tcl_CreateHashEntry (&sdata->namespace,
                                     Tcl_GetString (objv[1]), &hnew);
            ns = Tcl_GetHashKey (&sdata->namespace, h);
            quant = SCHEMA_CQUANT_ONE;
        }
    } else {
        h = Tcl_CreateHashEntry (&sdata->namespace,
                                 Tcl_GetString (objv[1]), &hnew);
        ns = Tcl_GetHashKey (&sdata->namespace, h);
        quant = getQuant (interp, sdata, objv[2], &n, &m);
        if (quant == SCHEMA_CQUANT_ERROR) {
            return TCL_ERROR;
        }
    }
    pattern = initSchemaCP (SCHEMA_CTYPE_ANY, ns, NULL);
    REMEMBER_PATTERN (pattern)
................................................................................
        return TCL_ERROR;
    }

    if (sdata->cp->type != SCHEMA_CTYPE_NAME
        && sdata->cp->type != SCHEMA_CTYPE_PATTERN) {
        SetResult ("The \"tcl\" schema definition command is only "
                   "allowed in sequential context (defelement, "
                   "element or defpattern)");
        return TCL_ERROR;
    }

    pattern = initSchemaCP (SCHEMA_CTYPE_VIRTUAL, NULL, NULL);
    REMEMBER_PATTERN (pattern)
    /* We alloc for one arugment more: the always appended schema
     * cmd. */







|









|
>







 







<
<
<
<
<
<
<
<







 







>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|
>













>








>
>
>
>
>
>
>
>
>
>







 







>
>




|


|
>

>
>
>
>
>



>
>







 







>

<







 







|










|







 







|
<
<
<
<
<
|






|






>







 







|











|







 







>








|
<







 







|







 







|




|







 







>







 







|








|
<







 







|












|







 







|







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







|

<


<
>
>
>
>


|
<
>


|
>







 







|

|


|
<
<
<
<
<
<
<
<
<
<
<







 







|
|







 







>
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>







 







<

>







 







|
<









<
|
<



<
|
<







 







|







119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
...
257
258
259
260
261
262
263








264
265
266
267
268
269
270
...
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
...
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
...
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
....
1006
1007
1008
1009
1010
1011
1012
1013
1014

1015
1016
1017
1018
1019
1020
1021
....
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
....
1065
1066
1067
1068
1069
1070
1071
1072





1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
....
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
....
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367

1368
1369
1370
1371
1372
1373
1374
....
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
....
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
....
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
....
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983

1984
1985
1986
1987
1988
1989
1990
....
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
....
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
....
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987

2988
2989

2990
2991
2992
2993
2994
2995
2996

2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
....
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047











3048
3049
3050
3051
3052
3053
3054
....
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
....
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
....
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
....
3530
3531
3532
3533
3534
3535
3536

3537
3538
3539
3540
3541
3542
3543
3544
3545
....
3780
3781
3782
3783
3784
3785
3786
3787

3788
3789
3790
3791
3792
3793
3794
3795
3796

3797

3798
3799
3800

3801

3802
3803
3804
3805
3806
3807
3808
....
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
#if defined(DEBUG) || defined(DDEBUG)
static char *Schema_CP_Type2str[] = {
    "ANY",
    "NAME",
    "CHOICE",
    "INTERLEAVE",
    "PATTERN",
    "TEXT",
    "VIRTUAL",
    "KEYSPACE_START",
    "KEYSPACE_END"
};
static char *Schema_Quant_Type2str[] = {
    "ONE",
    "OPT",
    "REP",
    "PLUS",
    "NM",
    "ERROR"
};
#endif


#ifndef TCL_THREADS
  static SchemaData *activeSchemaData = 0;
# define GETASI activeSchemaData
................................................................................
        se->activeChild = ac + 1;                 \
        se->hasMatched = 0;                       \
    } else {                                      \
        se->activeChild = ac;                     \
        se->hasMatched = 1;                       \
    }









#define S(str)  str, sizeof (str) -1

static SchemaCP*
initSchemaCP (
    Schema_CP_Type type,
    void *namespace,
    char *name
................................................................................
                FREE (pattern->attrs[i]);
            }
            FREE (pattern->attrs);
        }
        freedomKeyConstraints (pattern->domKeys);
        break;
    }
    if (pattern->defScript) {
        Tcl_DecrRefCount (pattern->defScript);
    }
    FREE (pattern);
}

static SchemaData*
initSchemaData (
    Tcl_Obj *cmdNameObj)
{
................................................................................
    }
    if (savedCP) {
        sdata->cp = savedCP;
        sdata->contentSize = savedContenSize;
    }
}

static SchemaValidationStack * 
getStackElement (
    SchemaData *sdata,
    SchemaCP *pattern
    )
{
    SchemaValidationStack *stackElm;

    if (sdata->stackPool) {
        stackElm = sdata->stackPool;
        sdata->stackPool = stackElm->down;
    } else {
        stackElm = TMALLOC (SchemaValidationStack);
    }
    memset (stackElm, 0, sizeof (SchemaValidationStack));
    stackElm->pattern = pattern;
    return stackElm;
}

/* The ac argument is the currend looked at child of the stack top
 * (which is not pattern, in case the looked ad child of the stack top
 * is SCHEMA_CTYPE_CHOICE). */
static void
pushToStack (
    SchemaData *sdata,
    SchemaCP *pattern,
    int ac
    )
{
    SchemaValidationStack *stackElm, *se;

    DBG(fprintf(stderr, "push to Stack:\n");serializeCP(pattern));
    if (sdata->stackPool) {
        stackElm = sdata->stackPool;
        sdata->stackPool = stackElm->down;
    } else {
        stackElm = TMALLOC (SchemaValidationStack);
    }
    memset (stackElm, 0, sizeof (SchemaValidationStack));
    se = sdata->stack;
    if (se) se->activeChild = ac;
    stackElm->down = se;
    stackElm->pattern = pattern;
    if (pattern->type == SCHEMA_CTYPE_INTERLEAVE) {
        stackElm->interleaveState = MALLOC (sizeof (int) * pattern->nc);
        memset (stackElm->interleaveState, 0, sizeof (int) * pattern->nc);
    }
    sdata->stack = stackElm;
}

static void
repoolStackElement (
    SchemaData *sdata,
    SchemaValidationStack *se
    ) 
{
    se->down = sdata->stackPool;
    sdata->stackPool = se;
}

static void
popStack (
    SchemaData *sdata
    )
{
    SchemaValidationStack *se;
................................................................................
        if (!(sc->constraint) (interp, sc->constraintData, text)) {
            return 0;
        }
    }
    return 1;
}

/* The argument ac points to the child of the current top-most stack
 * element pattern which is to evaluate. */
static int
evalVirtual (
    Tcl_Interp *interp,
    SchemaData *sdata,
    int ac
    )
{
    SchemaCP *cp;
    int savedac, savedhm, rc;

    cp = sdata->stack->pattern->content[ac];
    savedac = sdata->stack->activeChild;
    savedhm = sdata->stack->hasMatched;
    sdata->stack->activeChild = ac;
    sdata->stack->hasMatched = 1;
    cp->content[cp->nc-1] = (SchemaCP *) sdata->self;
    rc = Tcl_EvalObjv (interp, cp->nc, (Tcl_Obj **) cp->content,
                       TCL_EVAL_GLOBAL);
    sdata->stack->activeChild = savedac;
    sdata->stack->hasMatched = savedhm;    
    if (rc != TCL_OK) {
        sdata->evalError = 1;
        return 0;
    }
    return 1;
}

................................................................................

            case SCHEMA_CTYPE_NAME:
                DBG(fprintf (stderr, "name: %s ns: %s candidate name: %s "
                             "candidate ns: %s\n", name, namespace,
                             candidate->name, candidate->namespace));
                if (candidate->name == name
                    && candidate->namespace == namespace) {
                    pushToStack (sdata, candidate, ac);
                    updateStack (se, cp, ac);

                    return 1;
                }
                break;

            case SCHEMA_CTYPE_CHOICE:
                for (i = 0; i < candidate->nc; i++) {
                    icp = candidate->content[i];
................................................................................
                        updateStack (se, cp, ac);
                        sdata->skipDeep = 1;
                        return 1;

                    case SCHEMA_CTYPE_NAME:
                        if (icp->name == name
                            && icp->namespace == namespace) {
                            pushToStack (sdata, icp, ac);
                            updateStack (se, cp, ac);
                            return 1;
                        }
                        break;

                    case SCHEMA_CTYPE_CHOICE:
                        Tcl_Panic ("MIXED or CHOICE child of MIXED or CHOICE");

                    case SCHEMA_CTYPE_INTERLEAVE:
                    case SCHEMA_CTYPE_PATTERN:
                        pushToStack (sdata, icp, ac);
                        rc = matchElementStart (interp, sdata, name, namespace);
                        if (rc == 1) {
                            updateStack (se, cp, ac);
                            return 1;
                        }
                        popStack (sdata);
                        if (rc == -1) mayskip = 1;
................................................................................
                    }
                    if (!mayskip && mayMiss (candidate->quants[i]))
                        mayskip = 1;
                }
                break;

            case SCHEMA_CTYPE_VIRTUAL:
                if (evalVirtual (interp, sdata, ac)) {





                    hm = 1;
                    break;
                }
                else return 0;

            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                pushToStack (sdata, candidate, ac);
                rc = matchElementStart (interp, sdata, name, namespace);
                if (rc == 1) {
                    updateStack (se, cp, ac);
                    return 1;
                }
                popStack (sdata);
                if (rc == -1) mayskip = 1;
                break;

            case SCHEMA_CTYPE_KEYSPACE_END:
                candidate->keySpace->active--;
                if (!candidate->keySpace->active) {
                    if (candidate->keySpace->unknownIDrefs) {
                        if (!recover (interp, sdata, S("UNKNOWN_KEYREF"),
................................................................................
                se->hasMatched = 1;
                se->interleaveState[i] = 1;
                return 1;

            case SCHEMA_CTYPE_NAME:
                if (icp->name == name
                    && icp->namespace == namespace) {
                    pushToStack (sdata, icp, 0);
                    se->hasMatched = 1;
                    se->interleaveState[i] = 1;
                    return 1;
                }
                break;

            case SCHEMA_CTYPE_CHOICE:
                Tcl_Panic ("MIXED or CHOICE child of INTERLEAVE");

            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                pushToStack (sdata, icp, 0);
                rc = matchElementStart (interp, sdata, name, namespace);
                if (rc == 1) {
                    se->hasMatched = 1;
                    se->interleaveState[i] = 1;
                    return 1;
                }
                popStack (sdata);
................................................................................
            if (se->pattern->namespace) {
                Tcl_AppendResult (interp, namespace, ":", NULL);
            }
            Tcl_AppendResult (interp, name, "\"", NULL);
            return TCL_ERROR;
        }
    } else {
        sdata->validationState = VALIDATION_STARTED;
        if (!pattern) {
            if (recover (interp, sdata, S("UNKNOWN_ROOT_ELEMENT"), 0, 0)) {
                sdata->skipDeep = 1;
                return TCL_OK;
            }
            SetResult ("Unknown element");
            return TCL_ERROR;
        }
        pushToStack (sdata, pattern, 0);

        return TCL_OK;
    }

    /* The normal case: we're inside the tree */
    rc = matchElementStart (interp, sdata, (char *) namePtr, namespacePtr);
    while (rc == -1) {
        popStack (sdata);
................................................................................

                    case SCHEMA_CTYPE_NAME:
                    case SCHEMA_CTYPE_ANY:
                        continue;
                        
                    case SCHEMA_CTYPE_INTERLEAVE:
                    case SCHEMA_CTYPE_PATTERN:
                        pushToStack (sdata, ic, ac);
                        if (checkElementEnd (interp, sdata)) {
                            mayMiss = 1;
                        }
                        popStack (sdata);
                        break;
                        
                    case SCHEMA_CTYPE_KEYSPACE_END:
................................................................................
                if (mayMiss) break;
                if (!recover (interp, sdata, S("MISSING_ONE_OF_CHOICE"), 0, 0)) {
                    return 0;
                }
                break;
                
            case SCHEMA_CTYPE_VIRTUAL:
                if (evalVirtual (interp, sdata, ac)) break;
                else return 0;
                
            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                pushToStack (sdata, cp->content[ac], ac);
                rc = checkElementEnd (interp, sdata);
                popStack (sdata);
                if (rc) break;
                return 0;
                
            case SCHEMA_CTYPE_ANY:
                if (recover (interp, sdata, S("MISSING_ANY"), 0, 0)) {
................................................................................
    ) 
{
    Tcl_HashEntry *h, *h1;
    Tcl_HashSearch search, search1;
    int haveErrMsg = 0;
    SchemaDocKey *dk;

    /* TODO: add recovering */
    if (sdata->unknownIDrefs) {
        haveErrMsg = 1;
        SetResult ("References to unknown IDs:");
        for (h = Tcl_FirstHashEntry (&sdata->ids, &search);
             h != NULL;
             h = Tcl_NextHashEntry (&search)) {
            if (Tcl_GetHashValue (h) == 0) {
................................................................................

                        case SCHEMA_CTYPE_NAME:
                        case SCHEMA_CTYPE_ANY:
                            break;

                        case SCHEMA_CTYPE_INTERLEAVE:
                        case SCHEMA_CTYPE_PATTERN:
                            pushToStack (sdata, ic, ac);
                            if (matchText (interp, sdata, text)) {
                                updateStack (se, cp, ac);
                                return 1;
                            }
                            popStack (sdata);
                            break;

                        case SCHEMA_CTYPE_VIRTUAL:
                            Tcl_Panic ("Virtual constrain in MIXED or CHOICE");

                            
                        case SCHEMA_CTYPE_CHOICE:
                            Tcl_Panic ("MIXED or CHOICE child of MIXED or CHOICE");

                        case SCHEMA_CTYPE_KEYSPACE_END:
                        case SCHEMA_CTYPE_KEYSPACE:
                            Tcl_Panic ("Keyspace constrain in MIXED or CHOICE");
................................................................................
                        SetResult ("Unexpected text content");
                        return 0;
                    }
                    break;

                case SCHEMA_CTYPE_INTERLEAVE:
                case SCHEMA_CTYPE_PATTERN:
                    pushToStack (sdata, candidate, ac);
                    if (matchText (interp, sdata, text)) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
                    popStack (sdata);
                    if (mustMatch (cp->quants[ac], hm)) {
                        SetResult ("Unexpected text content");
                        return 0;
                    }
                    break;

                case SCHEMA_CTYPE_VIRTUAL:
                    if (!evalVirtual (interp, sdata, ac)) return 0;
                    break;

                case SCHEMA_CTYPE_KEYSPACE:
                    if (!cp->content[ac]->keySpace->active) {
                        Tcl_InitHashTable (&cp->content[ac]->keySpace->ids,
                                           TCL_STRING_KEYS);
                        cp->content[ac]->keySpace->active = 1;
................................................................................

                case SCHEMA_CTYPE_NAME:
                case SCHEMA_CTYPE_ANY:
                    break;

                case SCHEMA_CTYPE_INTERLEAVE:
                case SCHEMA_CTYPE_PATTERN:
                    pushToStack (sdata, ic, 0);
                    if (matchText (interp, sdata, text)) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
                    popStack (sdata);
                    break;

................................................................................
    sdata->cp = savedCP;
    sdata->contentSize = savedContenSize;
    if (sdata->cp && !sdata->isAttributeConstaint && cp->nc) {
        sdata->cp->flags |= CONSTRAINT_TEXT_CHILD;
    }
    return result;
}

/* cp must be of type SCHEMA_CTYPE_NAME for useful results */
static Tcl_Obj*
serializeElementName (
    Tcl_Interp *interp,
    SchemaCP *cp
    )
{
    Tcl_Obj *rObj;

    rObj = Tcl_NewObj();
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewStringObj (cp->name, -1));
    if (cp->namespace) {
        Tcl_ListObjAppendElement (interp, rObj,
                                  Tcl_NewStringObj (cp->namespace, -1));
    }
    return rObj;
}

/* cp must be of type SCHEMA_CTYPE_ANY for useful results */
static Tcl_Obj*
serializeAnyCP (
    Tcl_Interp *interp,
    SchemaCP *cp
    )
{
    Tcl_Obj *rObj;

    rObj = Tcl_NewObj();
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewStringObj ("<any>", 5));
    if (cp->namespace) {
        Tcl_ListObjAppendElement (interp, rObj,
                                  Tcl_NewStringObj (cp->namespace, -1));
    } else {
        Tcl_ListObjAppendElement (interp, rObj, Tcl_NewObj());
    }
    return rObj;
}

/* The cp argument may be NULL. If it isn't NULL cp must be of type
 * SCHEMA_CTYPE_TEXT for useful results */
static Tcl_Obj*
serializeTextCP (
    Tcl_Interp *interp,
    SchemaCP *cp
    )
{
    Tcl_Obj *rObj;

    rObj = Tcl_NewObj();
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewStringObj ("#text", 5));
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewObj());
    return rObj;
}

static void
definedElements (
    SchemaData *sdata,
    Tcl_Interp *interp
    )
{
    Tcl_Obj *rObj, *elmObj;
    Tcl_HashEntry *h;
    Tcl_HashSearch search;
    SchemaCP *cp;
    
    rObj = Tcl_GetObjResult (interp);
    for (h = Tcl_FirstHashEntry (&sdata->element, &search);
         h != NULL;
         h = Tcl_NextHashEntry (&search)) {
        cp = (SchemaCP *) Tcl_GetHashValue (h);
        if (!cp) continue;
        if (cp->flags & FORWARD_PATTERN_DEF
            || cp->flags & PLACEHOLDER_PATTERN_DEF) continue;
        elmObj = serializeElementName (interp, cp);
        Tcl_ListObjAppendElement (interp, rObj, elmObj);
    }
}

static int
getNextExpected (
    SchemaData *sdata,
    SchemaValidationStack *se,
    Tcl_Interp *interp,
    Tcl_HashTable *seenCPs,
    Tcl_Obj *rObj
    )
{
    int ac, hm, i, hnew, mustMatch, mayskip, rc;
    SchemaCP *cp, *ic, *jc;
    SchemaValidationStack *se1;

    getContext (cp, ac, hm);
    if (hm && maxOne(cp->quants[ac])) ac++;
    switch (cp->type) {
    case SCHEMA_CTYPE_INTERLEAVE:
        ac = 0;
        mustMatch = 0;
        /* Fall through */
    case SCHEMA_CTYPE_NAME:
    case SCHEMA_CTYPE_PATTERN:
        while (ac < cp->nc) {
            if (se->interleaveState
                && se->interleaveState[ac]
                && maxOne (cp->quants[ac])) {
                ac++;
                continue;
            }
            ic = cp->content[ac];
            mayskip = 0;
            switch (ic->type) {
            case SCHEMA_CTYPE_NAME:
                Tcl_ListObjAppendElement (interp, rObj,
                                          serializeElementName (interp, ic));
                break;
            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                Tcl_CreateHashEntry (seenCPs, ic, &hnew);
                if (hnew) {
                    se1 = getStackElement (sdata, ic);
                    mayskip = getNextExpected (sdata, se1, interp, seenCPs,
                                               rObj);
                    repoolStackElement (sdata, se1);
                }
                break;

            case SCHEMA_CTYPE_ANY:
                Tcl_ListObjAppendElement (interp, rObj,
                                          serializeAnyCP (interp, ic));
                break;

            case SCHEMA_CTYPE_TEXT:
                Tcl_ListObjAppendElement (interp, rObj,
                                          serializeTextCP (interp, ic));
                if (ic->nc == 0 || checkText (interp, ic, "")) {
                    mayskip = 1;
                }
                break;
                
            case SCHEMA_CTYPE_CHOICE:
                if (ic->flags & MIXED_CONTENT) {
                    Tcl_ListObjAppendElement (interp, rObj,
                                              serializeTextCP (interp, NULL));
                }
                for (i = 0; i < ic->nc; i++) {
                    jc = ic->content[i];
                    switch (jc->type) {
                    case SCHEMA_CTYPE_NAME:
                        Tcl_ListObjAppendElement (
                            interp, rObj, serializeElementName (interp, jc)
                            );
                        break;
                    case SCHEMA_CTYPE_INTERLEAVE:
                    case SCHEMA_CTYPE_PATTERN:
                        Tcl_CreateHashEntry (seenCPs, jc, &hnew);
                        if (hnew) {
                            se1 = getStackElement (sdata, ic);
                            mayskip = getNextExpected (sdata, se1, interp,
                                                       seenCPs, rObj);
                            repoolStackElement (sdata, se1);
                        }
                        break;
                    case SCHEMA_CTYPE_ANY:
                        Tcl_ListObjAppendElement (
                            interp, rObj, serializeAnyCP (interp, jc)
                            );
                        break;
                    case SCHEMA_CTYPE_TEXT:
                        Tcl_ListObjAppendElement (
                            interp, rObj, serializeTextCP (interp, jc)
                            );
                        break;
                    case SCHEMA_CTYPE_CHOICE:
                        Tcl_Panic ("MIXED or CHOICE child of MIXED or CHOICE");

                    case SCHEMA_CTYPE_VIRTUAL:
                    case SCHEMA_CTYPE_KEYSPACE:
                    case SCHEMA_CTYPE_KEYSPACE_END:
                        break;
                    }
                }
                break;

            case SCHEMA_CTYPE_VIRTUAL:
            case SCHEMA_CTYPE_KEYSPACE:
            case SCHEMA_CTYPE_KEYSPACE_END:
                break;
            }
            if (cp->type == SCHEMA_CTYPE_INTERLEAVE && minOne(cp->quants[ac]))
                mustMatch = 1;
            else if (!mayskip && minOne (cp->quants[ac])) break;
            ac++;
        }
        break;
        
    case SCHEMA_CTYPE_ANY:
    case SCHEMA_CTYPE_CHOICE:
    case SCHEMA_CTYPE_TEXT:
    case SCHEMA_CTYPE_VIRTUAL:
    case SCHEMA_CTYPE_KEYSPACE:
    case SCHEMA_CTYPE_KEYSPACE_END:
        Tcl_Panic ("Invalid CTYPE onto the validation stack!");
    }
    if (cp->type == SCHEMA_CTYPE_NAME) {
        return 0;
    }
    if (cp->type == SCHEMA_CTYPE_INTERLEAVE && mustMatch) {
        return 0;
    }
    if (se->down) {
        hm = se->down->hasMatched;
        se->down->hasMatched = 1;
        rc = getNextExpected (sdata, se->down, interp, seenCPs, rObj);
        se->down->hasMatched = hm;
        return rc;
    }
    return 1;
}

static int
schemaInstanceInfoCmd (
    SchemaData *sdata,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
{
    int methodIndex, len, i, hnew;
    Tcl_HashEntry *h;

    SchemaCP *cp;
    SchemaValidationStack *se;

    void *ns;
    Tcl_Obj *rObj, *r2Obj, *thisObj;
    Tcl_HashTable localHash;
    Tcl_HashSearch search;
    
    static const char *schemaInstanceInfoMethods[] = {
        "validationstate", "vstate", "definedElements", "stack", "toplevel",

        "pastexpected", "nextexpected", "definition", NULL
    };
    enum schemaInstanceInfoMethod {
        m_validationstate, m_vstate, m_definedElements, m_stack, m_toplevel,
        m_pastexpected, m_nextexpected, m_definition
    };

    static const char *schemaInstanceInfoStackMethods[] = {
        "top", "inside", NULL
    };
    enum schemaInstanceInfoStackMethod {
        m_top, m_inside
................................................................................
            break;
        default:
            SetResult ("Internal error: Invalid validation state");
            return TCL_ERROR;
        }
        break;
        
    case m_definedElements:
        if (objc != 2) {
            Tcl_WrongNumArgs (interp, 1, objv, "definedElements");
            return TCL_ERROR;
        }
        definedElements (sdata, interp);











        break;

    case m_stack:
        if (Tcl_GetIndexFromObj (interp, objv[2],
                                 schemaInstanceInfoStackMethods,
                                 "method", 0, &methodIndex)
            != TCL_OK) {
................................................................................
                Tcl_ResetResult (interp);
                return TCL_OK;
            }
            se = sdata->stack;
            while (se->pattern->type != SCHEMA_CTYPE_NAME) {
                se = se->down;
            }
            rObj = serializeElementName (interp, se->pattern);
            Tcl_SetObjResult (interp, rObj);
            return TCL_OK;
            break;
        }
        
    case m_toplevel:
        if (objc != 2) {
            Tcl_WrongNumArgs (interp, 1, objv, "no argument expected");
................................................................................
            return TCL_ERROR;
        }
        if (!sdata->defineToplevel && sdata->currentEvals > 1) {
            SetBooleanResult (0);
        } else {
            SetBooleanResult (1);
        }
        return TCL_OK;

    case m_nextexpected:
        if (sdata->validationState == VALIDATION_ERROR) {
            SetResult ("Validation command in error state.");
            return TCL_ERROR;
        } else if (sdata->validationState == VALIDATION_FINISHED) {
            return TCL_OK;
        }
        if (!sdata->stack) {
            if (sdata->start) {
                Tcl_AppendElement (interp, sdata->start);
                if (sdata->startNamespace) {
                    Tcl_AppendElement (interp, sdata->startNamespace);
                }
            } else {
                definedElements (sdata, interp);
            }
        } else {
            rObj = Tcl_NewObj();
            Tcl_InitHashTable (&localHash, TCL_ONE_WORD_KEYS);
            getNextExpected (sdata, sdata->stack, interp, &localHash, rObj);
            Tcl_DeleteHashTable (&localHash);
            Tcl_ListObjLength (interp, rObj, &len);
            if (len <= 1) {
                Tcl_SetObjResult (interp, rObj);
            } else {
                Tcl_InitHashTable (&localHash, TCL_STRING_KEYS);
                for (i = 0; i < len; i++) {
                    Tcl_ListObjIndex (interp, rObj, i, &thisObj);
                    h = Tcl_CreateHashEntry (
                        &localHash, Tcl_GetString (thisObj), &hnew
                        );
                    if (hnew) {
                        Tcl_SetHashValue (h, thisObj);
                    }
                }
                r2Obj = Tcl_NewObj();
                len = 0;
                for (h = Tcl_FirstHashEntry (&localHash, &search);
                     h != NULL;
                     h = Tcl_NextHashEntry (&search)) {
                    Tcl_ListObjAppendElement (interp, r2Obj,
                                              Tcl_GetHashValue (h));
                    len++;
                }
                Tcl_DeleteHashTable (&localHash);
                Tcl_DecrRefCount (rObj);
                Tcl_SetObjResult (interp, r2Obj);
            }
        }
        return TCL_OK;
        
    case m_pastexpected:
        break;
        
    case m_definition:
        if (objc < 3 && objc > 4) {
            Tcl_WrongNumArgs (interp, 1, objv, "name ?namespace?");
            return TCL_ERROR;
        }
        h = Tcl_FindHashEntry (&sdata->element, Tcl_GetString (objv[2]));
        if (!h) {
            SetResult ("Unknown element");
            return TCL_ERROR;
        }
        cp = Tcl_GetHashValue (h);
        ns = NULL;
        if (objc == 4) {
            ns = getNamespacePtr (sdata, Tcl_GetString (objv[3]));
        }
        while (cp && cp->namespace != ns) {
            cp = cp->next;
        }
        if (!cp) {
            SetResult ("Unknown element");
            return TCL_ERROR;
        }
        if (cp->flags & LOCAL_DEFINED_ELEMENT) {
            Tcl_AppendElement (interp, "element");
        } else {
            Tcl_AppendElement (interp, "defelement");
        }
        Tcl_AppendElement (interp, cp->name);
        if (cp->namespace) {
            Tcl_AppendElement (interp, cp->namespace);
        }
        if (cp->defScript) {
            Tcl_AppendElement (interp, Tcl_GetString (cp->defScript));
        }
        break;
    }
    return TCL_OK;
}

int
schemaInstanceCmd (
    ClientData clientData,
    Tcl_Interp *interp,
................................................................................
            if (forwardDef) {
                if (pattern->flags & FORWARD_PATTERN_DEF) {
                    sdata->forwardPatternDefs--;
                    pattern->flags &= ~FORWARD_PATTERN_DEF;
                }
                pattern->flags &= ~PLACEHOLDER_PATTERN_DEF;
            }
            pattern->defScript = objv[patternIndex];
            Tcl_IncrRefCount (pattern->defScript);
        } else {
            cleanupLastPattern (sdata, savedNumPatternList);
        }
        sdata->defineToplevel = savedDefineToplevel;
        sdata->currentNamespace = savedNamespacePtr;
        if (!savedDefineToplevel) {
            SETASI(savedsdata);
................................................................................
        } else {
            node = tcldom_getNodeFromObj (interp, objv[2]);
            if (!node) {
                SetResult ("The second argument must be either a "
                           "document or a element node");
                return TCL_ERROR;
            }

        }
        sdata->validationState = VALIDATION_STARTED;
        if (validateDOM (interp, sdata, node) == TCL_OK) {
            SetBooleanResult (1);
            if (objc == 4) {
                Tcl_SetVar (interp, Tcl_GetString (objv[3]), "", 0);
            }
        } else {
            if (objc == 4) {
................................................................................
    Tcl_Obj *const objv[]
    )
{
    SchemaData *sdata = GETASI;
    SchemaCP *pattern;
    SchemaQuant quant;
    char *ns = NULL;
    int n, m;


    CHECK_SI
    CHECK_TOPLEVEL
    checkNrArgs (1,3,"?namespace? ?quant?");
    if (objc == 1) {
        quant = SCHEMA_CQUANT_ONE;
    } else if (objc == 2) {    
        quant = getQuant (interp, sdata, objv[1], &n, &m);
        if (quant == SCHEMA_CQUANT_ERROR) {

            ns = getNamespacePtr (sdata, Tcl_GetString (objv[1]));

            quant = SCHEMA_CQUANT_ONE;
        }
    } else {

        ns = getNamespacePtr (sdata, Tcl_GetString (objv[1]));

        quant = getQuant (interp, sdata, objv[2], &n, &m);
        if (quant == SCHEMA_CQUANT_ERROR) {
            return TCL_ERROR;
        }
    }
    pattern = initSchemaCP (SCHEMA_CTYPE_ANY, ns, NULL);
    REMEMBER_PATTERN (pattern)
................................................................................
        return TCL_ERROR;
    }

    if (sdata->cp->type != SCHEMA_CTYPE_NAME
        && sdata->cp->type != SCHEMA_CTYPE_PATTERN) {
        SetResult ("The \"tcl\" schema definition command is only "
                   "allowed in sequential context (defelement, "
                   "element, group or defpattern)");
        return TCL_ERROR;
    }

    pattern = initSchemaCP (SCHEMA_CTYPE_VIRTUAL, NULL, NULL);
    REMEMBER_PATTERN (pattern)
    /* We alloc for one arugment more: the always appended schema
     * cmd. */

Changes to generic/schema.h.

104
105
106
107
108
109
110

111
112
113
114
115
116
117
    SchemaQuant      *quants;
    unsigned int      nc;
    SchemaAttr      **attrs;
    unsigned int      numAttr;
    unsigned int      numReqAttr;
    domKeyConstraint *domKeys;
    SchemaKeySpace   *keySpace;

} SchemaCP;

typedef struct SchemaValidationStack
{
    SchemaCP *pattern;
    struct SchemaValidationStack *next;
    struct SchemaValidationStack *down;







>







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    SchemaQuant      *quants;
    unsigned int      nc;
    SchemaAttr      **attrs;
    unsigned int      numAttr;
    unsigned int      numReqAttr;
    domKeyConstraint *domKeys;
    SchemaKeySpace   *keySpace;
    Tcl_Obj          *defScript;
} SchemaCP;

typedef struct SchemaValidationStack
{
    SchemaCP *pattern;
    struct SchemaValidationStack *next;
    struct SchemaValidationStack *down;

Changes to tests/schema.test.

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
....
2128
2129
2130
2131
2132
2133
2134




























2135
2136
2137
2138
2139
2140
2141
....
4690
4691
4692
4693
4694
4695
4696




























4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
....
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
....
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744




































































































































































































































4745
4746
4747
4748
4749
4750
4751
#    schema-15.*: Constraint cmd tcl
#    schema-16.*: interleave
#    schema-17.*: info
#    schema-18.*: reportcmd
#    schema-19.*: keyspace
#    schema-20.*: domunique
#
# Copyright (c) 2018 Rolf Ade.

source [file join [file dir [info script]] loadtdom.tcl]

if {[dom featureinfo schema]} {

test schema-1.1 {create} {
    tdom::schema create grammar
................................................................................
        {<doc><e4></doc>}
    } {
        lappend result [grammar validate $xml]
    }
    grammar delete
    set result
} {0 1 0 1 1 1 0}





























test schema-10.1 {any} {
    tdom::schema create s
    s define {
        defelement doc {
            element a
            any
................................................................................
        {<doc><items><b/><b/><c/></items><items><a/><c/></items><items><b/><a/><c/></items></doc>}
    } {
        lappend result [s validate $xml errMsg]
    }
    s delete
    set result
} {1 1 0}





























test schema-17.1 {info} {
    tdom::schema s
    s define {
        foreach e {a b1 b11 b2 c} {
            defelement $e {}
        }
    }
    set result [lsort [s info defelements]]
    s delete
    set result
} {a b1 b11 b2 c}

test schema-17.2 {info} {
    tdom::schema s
    s define {
................................................................................
            element b2
        }
        defelement a {
            element a1
            element a2
        }
    }
    set result [lsort [s info defelements]]
    s delete
    set result
} {a b}

test schema-17.3 {info} {
    tdom::schema s
    s define {
................................................................................
            element b2
        }
        defelement a {
            element a1
            element a2
        }
    }
    set result [lsort [s info defelements]]
    s delete
    set result
} {a b}





































































































































































































































proc schema-18 {args} {
    lappend ::result {*}$args
}
test schema-18.1 {reportcmd} {
    tdom::schema s
    s define {
        defelement doc {







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








|







 







|







 







|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
....
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
....
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
....
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
....
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
#    schema-15.*: Constraint cmd tcl
#    schema-16.*: interleave
#    schema-17.*: info
#    schema-18.*: reportcmd
#    schema-19.*: keyspace
#    schema-20.*: domunique
#
# Copyright (c) 2018-2019 Rolf Ade.

source [file join [file dir [info script]] loadtdom.tcl]

if {[dom featureinfo schema]} {

test schema-1.1 {create} {
    tdom::schema create grammar
................................................................................
        {<doc><e4></doc>}
    } {
        lappend result [grammar validate $xml]
    }
    grammar delete
    set result
} {0 1 0 1 1 1 0}

test schema-9.9 {choice with optional cp belong the choices} {
    tdom::schema create grammar
    grammar define {
        defelement doc {
            element e1
            choice {
                element e2
                element e3 ?
            }
            element e4 ?
        }
    }
    set result [list]
    foreach xml {
        <doc/>
        {<doc><e1/></doc>}
        {<doc><e1/><e2/></doc>}
        {<doc><e1/><e3/></doc>}
        {<doc><e1/><e4/></doc>}
        {<doc><e1/><e2/><e4/></doc>}
        {<doc><e1/><e3/><e4/></doc>}
    } {
        lappend result [grammar validate $xml]
    }
    grammar delete
    set result
} {0 1 1 1 1 1 1}

test schema-10.1 {any} {
    tdom::schema create s
    s define {
        defelement doc {
            element a
            any
................................................................................
        {<doc><items><b/><b/><c/></items><items><a/><c/></items><items><b/><a/><c/></items></doc>}
    } {
        lappend result [s validate $xml errMsg]
    }
    s delete
    set result
} {1 1 0}

test schema-16.17 {interleave with all content cp optional} {
    tdom::schema s
    s defelement doc {
        interleave {
            element a ?
            element b ?
            element c ?
        }
        element d
    }
    set result [list]
    foreach xml {
        <doc/>
        <doc><a/><d/></doc>
        <doc><b/><d/></doc>
        <doc><c/><d/></doc>
        <doc><a/><c/><d/></doc>
        <doc><b/><c/><d/></doc>
        <doc><c/><a/><d/></doc>
        <doc><c/><a/><b/><d/></doc>
        <doc><c/><a/><b/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 1 1 1 1 1 1 1 0}

test schema-17.1 {info} {
    tdom::schema s
    s define {
        foreach e {a b1 b11 b2 c} {
            defelement $e {}
        }
    }
    set result [lsort [s info definedElements]]
    s delete
    set result
} {a b1 b11 b2 c}

test schema-17.2 {info} {
    tdom::schema s
    s define {
................................................................................
            element b2
        }
        defelement a {
            element a1
            element a2
        }
    }
    set result [lsort [s info definedElements]]
    s delete
    set result
} {a b}

test schema-17.3 {info} {
    tdom::schema s
    s define {
................................................................................
            element b2
        }
        defelement a {
            element a1
            element a2
        }
    }
    set result [lsort [s info definedElements]]
    s delete
    set result
} {a b}

test schema-17.4 {info} {
    tdom::schema s
    s define {
        defelement b {
            element b1 1 text
            element a
            element b2
        }
        defelement a {
            element a1
            element a2
        }
    }
    set result [s info definition b]
    s delete
    set result
} {defelement b {
            element b1 1 text
            element a
            element b2
        }}

test schema-17.5 {info nextexpected} {
    tdom::schema s
    s define {
        defelement doc {
            choice ? {
                element a
                element c
                element b
            }
            element toplevel ?
            element musthave
            element aftermust
        }
    }
    s event start doc
    set result [s info nextexpected]
    s delete
    lsort $result
} {a b c musthave toplevel}

test schema-17.6 {info nextexpected} {
    tdom::schema s
    s prefixns {foo http://foo.bar}
    s define {
        defelement doc {
            choice ? {
                element a
                element c
                element b
            }
            element toplevel ?
            namespace foo {
                element musthave
            }
            element aftermust
        }
    }
    s event start doc
    set result [s info nextexpected]
    s delete
    lsort $result
} {a b c {musthave http://foo.bar} toplevel}

test schema-17.7 {info nextexpected} {
    tdom::schema s
    s prefixns {foo http://foo.bar}
    s define {
        defelement doc {
            mixed {
                element a
                element c
                element b
            }
            element toplevel ?
            namespace foo {
                element musthave
            }
            element aftermust
        }
    }
    s event start doc
    set result [s info nextexpected]
    s delete
    lsort $result
} {a b c {musthave http://foo.bar} toplevel {{#text} {}}}

test schema-17.8 {info nextexpected} {
    tdom::schema s
    s defelement doc {
        choice ? {
            element a
            element c
            element b
        }
        element toplevel ?
        element musthave
        element aftermust
    }
    set result [s info nextexpected]
    s define {
        foreach elm {a b c} {
            defelement $elm {}
        }
    }
    lappend result {*}[lsort [s info nextexpected]]
    s event start doc
    lappend result {*}[lsort [s info nextexpected]]
    s event start c
    s event end
    lappend result {*}[lsort [s info nextexpected]]
    s delete
    set result
} {doc a b c doc a b c musthave toplevel musthave toplevel}

proc schema-17.9 {scmd} {
    global result
    lappend result {*}[lsort [$scmd info nextexpected]]
}
                  
test schema-17.9 {info nextexpected from scripted constrain} {
    tdom::schema s
    s define {
        defpattern some {
            element a ?
            group ? {
                element b ?
                tcl schema-17.9
            }
            element c
        }
        defelement doc {
            ref some ?
            element may ?
            element must
        }
    }
    set result ""
    lappend [s validate {<doc><must/></doc>}]
    s delete
    set result
} {c may must}

test schema-17.10 {info nextexpected interleave} {
    set defs {
        {
            interleave {
                element a ?
                element b
                element c ?
            }
            element d
        }
        {
            interleave {
                element a ?
                element b ?
                element c ?
            }
            element d
        }
        {
            interleave ? {
                element a ?
                element b
                element c ?
            }
            element d
        }
    }
    set result [list]
    foreach def $defs {
        tdom::schema s
        s defelement doc $def
        s event start doc
        lappend result {*}[lsort [s info nextexpected]]
        s delete
    }
    set result
} {a b c a b c d a b c d}

test schema-17.11 {info nextexpected interleave} {
    set defs {
        {
            group + {
                element c ?
                element a ?
                element b ?
            }
            element d
        }
    }
    set result [list]
    foreach def $defs {
        tdom::schema s
        s defelement doc $def
        s event start doc
        s event start b
        s event end
        set result [lsort [s info nextexpected]]
        s delete
    }
    set result
} {a b c d}

test schema-17.12 {info nextexpected interleave} {
    tdom::schema s
    s define {
        prefixns {ns1 http://foo.bar}
        defelement doc {
            element a
            any
            any ns1 ?
            element b ?
        }
    }
    s event start doc
    s event start a
    s event end
    set result [lsort [s info nextexpected]]
    s event start something
    s event end
    lappend result {*}[lsort [s info nextexpected]]
    s delete
    set result
} {{<any> {}} {<any> http://foo.bar} b}

proc schema-18 {args} {
    lappend ::result {*}$args
}
test schema-18.1 {reportcmd} {
    tdom::schema s
    s define {
        defelement doc {