2 * valid.c : part of the code use to do the DTD handling and the validity
5 * See Copyright for the status of this software.
19 #include <libxml/xmlmemory.h>
20 #include <libxml/hash.h>
21 #include <libxml/uri.h>
22 #include <libxml/valid.h>
23 #include <libxml/parser.h>
24 #include <libxml/parserInternals.h>
25 #include <libxml/xmlerror.h>
26 #include <libxml/list.h>
27 #include <libxml/globals.h>
29 static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
31 /* #define DEBUG_VALID_ALGO */
32 /* #define DEBUG_REGEXP_ALGO */
35 xmlGenericError(xmlGenericErrorContext, \
36 "Unimplemented block at %s:%d\n", \
39 #ifdef LIBXML_VALID_ENABLED
41 xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
42 const xmlChar *value);
44 /************************************************************************
46 * Error handling routines *
48 ************************************************************************/
52 * @ctxt: an XML validation parser context
53 * @extra: extra informations
55 * Handle an out of memory error
58 xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
60 xmlGenericErrorFunc channel = NULL;
61 xmlParserCtxtPtr pctxt = NULL;
65 channel = ctxt->error;
66 data = ctxt->userData;
67 /* Use the special values to detect if it is part of a parsing
69 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
70 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
71 long delta = (char *) ctxt - (char *) ctxt->userData;
72 if ((delta > 0) && (delta < 250))
73 pctxt = ctxt->userData;
77 __xmlRaiseError(NULL, channel, data,
78 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
79 XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
80 "Memory allocation failed : %s\n", extra);
82 __xmlRaiseError(NULL, channel, data,
83 pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
84 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
85 "Memory allocation failed\n");
90 * @ctxt: an XML validation parser context
91 * @error: the error number
92 * @extra: extra informations
94 * Handle a validation error
96 static void LIBXML_ATTR_FORMAT(3,0)
97 xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
98 const char *msg, const char *extra)
100 xmlGenericErrorFunc channel = NULL;
101 xmlParserCtxtPtr pctxt = NULL;
105 channel = ctxt->error;
106 data = ctxt->userData;
107 /* Use the special values to detect if it is part of a parsing
109 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
110 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
111 long delta = (char *) ctxt - (char *) ctxt->userData;
112 if ((delta > 0) && (delta < 250))
113 pctxt = ctxt->userData;
117 __xmlRaiseError(NULL, channel, data,
118 pctxt, NULL, XML_FROM_VALID, error,
119 XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
122 __xmlRaiseError(NULL, channel, data,
123 pctxt, NULL, XML_FROM_VALID, error,
124 XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
128 #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
131 * @ctxt: an XML validation parser context
132 * @node: the node raising the error
133 * @error: the error number
134 * @str1: extra informations
135 * @str2: extra informations
136 * @str3: extra informations
138 * Handle a validation error, provide contextual informations
140 static void LIBXML_ATTR_FORMAT(4,0)
141 xmlErrValidNode(xmlValidCtxtPtr ctxt,
142 xmlNodePtr node, xmlParserErrors error,
143 const char *msg, const xmlChar * str1,
144 const xmlChar * str2, const xmlChar * str3)
146 xmlStructuredErrorFunc schannel = NULL;
147 xmlGenericErrorFunc channel = NULL;
148 xmlParserCtxtPtr pctxt = NULL;
152 channel = ctxt->error;
153 data = ctxt->userData;
154 /* Use the special values to detect if it is part of a parsing
156 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
157 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
158 long delta = (char *) ctxt - (char *) ctxt->userData;
159 if ((delta > 0) && (delta < 250))
160 pctxt = ctxt->userData;
163 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
164 XML_ERR_ERROR, NULL, 0,
167 (const char *) str3, 0, 0, msg, str1, str2, str3);
169 #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
171 #ifdef LIBXML_VALID_ENABLED
174 * @ctxt: an XML validation parser context
175 * @node: the node raising the error
176 * @error: the error number
177 * @str1: extra informations
178 * @int2: extra informations
179 * @str3: extra informations
181 * Handle a validation error, provide contextual informations
183 static void LIBXML_ATTR_FORMAT(4,0)
184 xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
185 xmlNodePtr node, xmlParserErrors error,
186 const char *msg, const xmlChar * str1,
187 int int2, const xmlChar * str3)
189 xmlStructuredErrorFunc schannel = NULL;
190 xmlGenericErrorFunc channel = NULL;
191 xmlParserCtxtPtr pctxt = NULL;
195 channel = ctxt->error;
196 data = ctxt->userData;
197 /* Use the special values to detect if it is part of a parsing
199 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
200 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
201 long delta = (char *) ctxt - (char *) ctxt->userData;
202 if ((delta > 0) && (delta < 250))
203 pctxt = ctxt->userData;
206 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
207 XML_ERR_ERROR, NULL, 0,
210 NULL, int2, 0, msg, str1, int2, str3);
214 * xmlErrValidWarning:
215 * @ctxt: an XML validation parser context
216 * @node: the node raising the error
217 * @error: the error number
218 * @str1: extra information
219 * @str2: extra information
220 * @str3: extra information
222 * Handle a validation error, provide contextual information
224 static void LIBXML_ATTR_FORMAT(4,0)
225 xmlErrValidWarning(xmlValidCtxtPtr ctxt,
226 xmlNodePtr node, xmlParserErrors error,
227 const char *msg, const xmlChar * str1,
228 const xmlChar * str2, const xmlChar * str3)
230 xmlStructuredErrorFunc schannel = NULL;
231 xmlGenericErrorFunc channel = NULL;
232 xmlParserCtxtPtr pctxt = NULL;
236 channel = ctxt->warning;
237 data = ctxt->userData;
238 /* Use the special values to detect if it is part of a parsing
240 if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
241 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
242 long delta = (char *) ctxt - (char *) ctxt->userData;
243 if ((delta > 0) && (delta < 250))
244 pctxt = ctxt->userData;
247 __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
248 XML_ERR_WARNING, NULL, 0,
251 (const char *) str3, 0, 0, msg, str1, str2, str3);
256 #ifdef LIBXML_REGEXP_ENABLED
258 * If regexp are enabled we can do continuous validation without the
259 * need of a tree to validate the content model. this is done in each
261 * Each xmlValidState represent the validation state associated to the
262 * set of nodes currently open from the document root to the current element.
266 typedef struct _xmlValidState {
267 xmlElementPtr elemDecl; /* pointer to the content model */
268 xmlNodePtr node; /* pointer to the current node */
269 xmlRegExecCtxtPtr exec; /* regexp runtime */
274 vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
275 if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
276 ctxt->vstateMax = 10;
277 ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
278 sizeof(ctxt->vstateTab[0]));
279 if (ctxt->vstateTab == NULL) {
280 xmlVErrMemory(ctxt, "malloc failed");
285 if (ctxt->vstateNr >= ctxt->vstateMax) {
288 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
289 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
291 xmlVErrMemory(ctxt, "realloc failed");
294 ctxt->vstateMax *= 2;
295 ctxt->vstateTab = tmp;
297 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
298 ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
299 ctxt->vstateTab[ctxt->vstateNr].node = node;
300 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
301 if (elemDecl->contModel == NULL)
302 xmlValidBuildContentModel(ctxt, elemDecl);
303 if (elemDecl->contModel != NULL) {
304 ctxt->vstateTab[ctxt->vstateNr].exec =
305 xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
307 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
308 xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
309 XML_ERR_INTERNAL_ERROR,
310 "Failed to build content model regexp for %s\n",
311 node->name, NULL, NULL);
314 return(ctxt->vstateNr++);
318 vstateVPop(xmlValidCtxtPtr ctxt) {
319 xmlElementPtr elemDecl;
321 if (ctxt->vstateNr < 1) return(-1);
323 elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
324 ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
325 ctxt->vstateTab[ctxt->vstateNr].node = NULL;
326 if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
327 xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
329 ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
330 if (ctxt->vstateNr >= 1)
331 ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
334 return(ctxt->vstateNr);
337 #else /* not LIBXML_REGEXP_ENABLED */
339 * If regexp are not enabled, it uses a home made algorithm less
340 * complex and easier to
341 * debug/maintain than a generic NFA -> DFA state based algo. The
342 * only restriction is on the deepness of the tree limited by the
343 * size of the occurs bitfield
345 * this is the content of a saved state for rollbacks
348 #define ROLLBACK_OR 0
349 #define ROLLBACK_PARENT 1
351 typedef struct _xmlValidState {
352 xmlElementContentPtr cont; /* pointer to the content model subtree */
353 xmlNodePtr node; /* pointer to the current node in the list */
354 long occurs;/* bitfield for multiple occurrences */
355 unsigned char depth; /* current depth in the overall tree */
356 unsigned char state; /* ROLLBACK_XXX */
359 #define MAX_RECURSE 25000
360 #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
361 #define CONT ctxt->vstate->cont
362 #define NODE ctxt->vstate->node
363 #define DEPTH ctxt->vstate->depth
364 #define OCCURS ctxt->vstate->occurs
365 #define STATE ctxt->vstate->state
367 #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
368 #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
370 #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
371 #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
374 vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
375 xmlNodePtr node, unsigned char depth, long occurs,
376 unsigned char state) {
377 int i = ctxt->vstateNr - 1;
379 if (ctxt->vstateNr > MAX_RECURSE) {
382 if (ctxt->vstateTab == NULL) {
384 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
385 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
386 if (ctxt->vstateTab == NULL) {
387 xmlVErrMemory(ctxt, "malloc failed");
391 if (ctxt->vstateNr >= ctxt->vstateMax) {
394 tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
395 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
397 xmlVErrMemory(ctxt, "malloc failed");
400 ctxt->vstateMax *= 2;
401 ctxt->vstateTab = tmp;
402 ctxt->vstate = &ctxt->vstateTab[0];
405 * Don't push on the stack a state already here
407 if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
408 (ctxt->vstateTab[i].node == node) &&
409 (ctxt->vstateTab[i].depth == depth) &&
410 (ctxt->vstateTab[i].occurs == occurs) &&
411 (ctxt->vstateTab[i].state == state))
412 return(ctxt->vstateNr);
413 ctxt->vstateTab[ctxt->vstateNr].cont = cont;
414 ctxt->vstateTab[ctxt->vstateNr].node = node;
415 ctxt->vstateTab[ctxt->vstateNr].depth = depth;
416 ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
417 ctxt->vstateTab[ctxt->vstateNr].state = state;
418 return(ctxt->vstateNr++);
422 vstateVPop(xmlValidCtxtPtr ctxt) {
423 if (ctxt->vstateNr <= 1) return(-1);
425 ctxt->vstate = &ctxt->vstateTab[0];
426 ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
427 ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
428 ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
429 ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
430 ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
431 return(ctxt->vstateNr);
434 #endif /* LIBXML_REGEXP_ENABLED */
437 nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
439 if (ctxt->nodeMax <= 0) {
442 (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
443 sizeof(ctxt->nodeTab[0]));
444 if (ctxt->nodeTab == NULL) {
445 xmlVErrMemory(ctxt, "malloc failed");
450 if (ctxt->nodeNr >= ctxt->nodeMax) {
452 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
453 ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
455 xmlVErrMemory(ctxt, "realloc failed");
461 ctxt->nodeTab[ctxt->nodeNr] = value;
463 return (ctxt->nodeNr++);
466 nodeVPop(xmlValidCtxtPtr ctxt)
470 if (ctxt->nodeNr <= 0)
473 if (ctxt->nodeNr > 0)
474 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
477 ret = ctxt->nodeTab[ctxt->nodeNr];
478 ctxt->nodeTab[ctxt->nodeNr] = NULL;
482 #ifdef DEBUG_VALID_ALGO
484 xmlValidPrintNode(xmlNodePtr cur) {
486 xmlGenericError(xmlGenericErrorContext, "null");
490 case XML_ELEMENT_NODE:
491 xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
494 xmlGenericError(xmlGenericErrorContext, "text ");
496 case XML_CDATA_SECTION_NODE:
497 xmlGenericError(xmlGenericErrorContext, "cdata ");
499 case XML_ENTITY_REF_NODE:
500 xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
503 xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
505 case XML_COMMENT_NODE:
506 xmlGenericError(xmlGenericErrorContext, "comment ");
508 case XML_ATTRIBUTE_NODE:
509 xmlGenericError(xmlGenericErrorContext, "?attr? ");
511 case XML_ENTITY_NODE:
512 xmlGenericError(xmlGenericErrorContext, "?ent? ");
514 case XML_DOCUMENT_NODE:
515 xmlGenericError(xmlGenericErrorContext, "?doc? ");
517 case XML_DOCUMENT_TYPE_NODE:
518 xmlGenericError(xmlGenericErrorContext, "?doctype? ");
520 case XML_DOCUMENT_FRAG_NODE:
521 xmlGenericError(xmlGenericErrorContext, "?frag? ");
523 case XML_NOTATION_NODE:
524 xmlGenericError(xmlGenericErrorContext, "?nota? ");
526 case XML_HTML_DOCUMENT_NODE:
527 xmlGenericError(xmlGenericErrorContext, "?html? ");
529 #ifdef LIBXML_DOCB_ENABLED
530 case XML_DOCB_DOCUMENT_NODE:
531 xmlGenericError(xmlGenericErrorContext, "?docb? ");
535 xmlGenericError(xmlGenericErrorContext, "?dtd? ");
537 case XML_ELEMENT_DECL:
538 xmlGenericError(xmlGenericErrorContext, "?edecl? ");
540 case XML_ATTRIBUTE_DECL:
541 xmlGenericError(xmlGenericErrorContext, "?adecl? ");
543 case XML_ENTITY_DECL:
544 xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
546 case XML_NAMESPACE_DECL:
547 xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
549 case XML_XINCLUDE_START:
550 xmlGenericError(xmlGenericErrorContext, "incstart ");
552 case XML_XINCLUDE_END:
553 xmlGenericError(xmlGenericErrorContext, "incend ");
559 xmlValidPrintNodeList(xmlNodePtr cur) {
561 xmlGenericError(xmlGenericErrorContext, "null ");
562 while (cur != NULL) {
563 xmlValidPrintNode(cur);
569 xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
573 xmlGenericError(xmlGenericErrorContext, "valid: ");
574 xmlValidPrintNodeList(cur);
575 xmlGenericError(xmlGenericErrorContext, "against ");
576 xmlSnprintfElementContent(expr, 5000, cont, 1);
577 xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
581 xmlValidDebugState(xmlValidStatePtr state) {
582 xmlGenericError(xmlGenericErrorContext, "(");
583 if (state->cont == NULL)
584 xmlGenericError(xmlGenericErrorContext, "null,");
586 switch (state->cont->type) {
587 case XML_ELEMENT_CONTENT_PCDATA:
588 xmlGenericError(xmlGenericErrorContext, "pcdata,");
590 case XML_ELEMENT_CONTENT_ELEMENT:
591 xmlGenericError(xmlGenericErrorContext, "%s,",
594 case XML_ELEMENT_CONTENT_SEQ:
595 xmlGenericError(xmlGenericErrorContext, "seq,");
597 case XML_ELEMENT_CONTENT_OR:
598 xmlGenericError(xmlGenericErrorContext, "or,");
601 xmlValidPrintNode(state->node);
602 xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
603 state->depth, state->occurs, state->state);
607 xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
610 xmlGenericError(xmlGenericErrorContext, "state: ");
611 xmlValidDebugState(ctxt->vstate);
612 xmlGenericError(xmlGenericErrorContext, " stack: %d ",
614 for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
615 xmlValidDebugState(&ctxt->vstateTab[j]);
616 xmlGenericError(xmlGenericErrorContext, "\n");
620 #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
623 #define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
624 #define DEBUG_VALID_MSG(m) \
625 xmlGenericError(xmlGenericErrorContext, "%s\n", m);
628 #define DEBUG_VALID_STATE(n,c)
629 #define DEBUG_VALID_MSG(m)
632 /* TODO: use hash table for accesses to elem and attribute definitions */
636 if (doc == NULL) return(0); \
637 else if ((doc->intSubset == NULL) && \
638 (doc->extSubset == NULL)) return(0)
640 #ifdef LIBXML_REGEXP_ENABLED
642 /************************************************************************
644 * Content model validation based on the regexps *
646 ************************************************************************/
649 * xmlValidBuildAContentModel:
650 * @content: the content model
651 * @ctxt: the schema parser context
652 * @name: the element name whose content is being built
654 * Generate the automata sequence needed for that type
656 * Returns 1 if successful or 0 in case of error.
659 xmlValidBuildAContentModel(xmlElementContentPtr content,
660 xmlValidCtxtPtr ctxt,
661 const xmlChar *name) {
662 if (content == NULL) {
663 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
664 "Found NULL content in content model of %s\n",
668 switch (content->type) {
669 case XML_ELEMENT_CONTENT_PCDATA:
670 xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
671 "Found PCDATA in content model of %s\n",
675 case XML_ELEMENT_CONTENT_ELEMENT: {
676 xmlAutomataStatePtr oldstate = ctxt->state;
680 fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
681 if (fullname == NULL) {
682 xmlVErrMemory(ctxt, "Building content model");
686 switch (content->ocur) {
687 case XML_ELEMENT_CONTENT_ONCE:
688 ctxt->state = xmlAutomataNewTransition(ctxt->am,
689 ctxt->state, NULL, fullname, NULL);
691 case XML_ELEMENT_CONTENT_OPT:
692 ctxt->state = xmlAutomataNewTransition(ctxt->am,
693 ctxt->state, NULL, fullname, NULL);
694 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
696 case XML_ELEMENT_CONTENT_PLUS:
697 ctxt->state = xmlAutomataNewTransition(ctxt->am,
698 ctxt->state, NULL, fullname, NULL);
699 xmlAutomataNewTransition(ctxt->am, ctxt->state,
700 ctxt->state, fullname, NULL);
702 case XML_ELEMENT_CONTENT_MULT:
703 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
705 xmlAutomataNewTransition(ctxt->am,
706 ctxt->state, ctxt->state, fullname, NULL);
709 if ((fullname != fn) && (fullname != content->name))
713 case XML_ELEMENT_CONTENT_SEQ: {
714 xmlAutomataStatePtr oldstate, oldend;
715 xmlElementContentOccur ocur;
718 * Simply iterate over the content
720 oldstate = ctxt->state;
721 ocur = content->ocur;
722 if (ocur != XML_ELEMENT_CONTENT_ONCE) {
723 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
724 oldstate = ctxt->state;
727 xmlValidBuildAContentModel(content->c1, ctxt, name);
728 content = content->c2;
729 } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
730 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
731 xmlValidBuildAContentModel(content, ctxt, name);
732 oldend = ctxt->state;
733 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
735 case XML_ELEMENT_CONTENT_ONCE:
737 case XML_ELEMENT_CONTENT_OPT:
738 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
740 case XML_ELEMENT_CONTENT_MULT:
741 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
742 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
744 case XML_ELEMENT_CONTENT_PLUS:
745 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
750 case XML_ELEMENT_CONTENT_OR: {
751 xmlAutomataStatePtr oldstate, oldend;
752 xmlElementContentOccur ocur;
754 ocur = content->ocur;
755 if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
756 (ocur == XML_ELEMENT_CONTENT_MULT)) {
757 ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
760 oldstate = ctxt->state;
761 oldend = xmlAutomataNewState(ctxt->am);
764 * iterate over the subtypes and remerge the end with an
768 ctxt->state = oldstate;
769 xmlValidBuildAContentModel(content->c1, ctxt, name);
770 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
771 content = content->c2;
772 } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
773 (content->ocur == XML_ELEMENT_CONTENT_ONCE));
774 ctxt->state = oldstate;
775 xmlValidBuildAContentModel(content, ctxt, name);
776 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
777 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
779 case XML_ELEMENT_CONTENT_ONCE:
781 case XML_ELEMENT_CONTENT_OPT:
782 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
784 case XML_ELEMENT_CONTENT_MULT:
785 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
786 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
788 case XML_ELEMENT_CONTENT_PLUS:
789 xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
795 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
796 "ContentModel broken for element %s\n",
797 (const char *) name);
803 * xmlValidBuildContentModel:
804 * @ctxt: a validation context
805 * @elem: an element declaration node
807 * (Re)Build the automata associated to the content model of this
810 * Returns 1 in case of success, 0 in case of error
813 xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
815 if ((ctxt == NULL) || (elem == NULL))
817 if (elem->type != XML_ELEMENT_DECL)
819 if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
821 /* TODO: should we rebuild in this case ? */
822 if (elem->contModel != NULL) {
823 if (!xmlRegexpIsDeterminist(elem->contModel)) {
830 ctxt->am = xmlNewAutomata();
831 if (ctxt->am == NULL) {
832 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
833 XML_ERR_INTERNAL_ERROR,
834 "Cannot create automata for element %s\n",
835 elem->name, NULL, NULL);
838 ctxt->state = xmlAutomataGetInitState(ctxt->am);
839 xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
840 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
841 elem->contModel = xmlAutomataCompile(ctxt->am);
842 if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
845 xmlSnprintfElementContent(expr, 5000, elem->content, 1);
846 xmlErrValidNode(ctxt, (xmlNodePtr) elem,
847 XML_DTD_CONTENT_NOT_DETERMINIST,
848 "Content model of %s is not determinist: %s\n",
849 elem->name, BAD_CAST expr, NULL);
850 #ifdef DEBUG_REGEXP_ALGO
851 xmlRegexpPrint(stderr, elem->contModel);
855 xmlFreeAutomata(ctxt->am);
860 xmlFreeAutomata(ctxt->am);
865 #endif /* LIBXML_REGEXP_ENABLED */
867 /****************************************************************
869 * Util functions for data allocation/deallocation *
871 ****************************************************************/
876 * Allocate a validation context structure.
878 * Returns NULL if not, otherwise the new validation context structure
880 xmlValidCtxtPtr xmlNewValidCtxt(void) {
883 if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
884 xmlVErrMemory(NULL, "malloc failed");
888 (void) memset(ret, 0, sizeof (xmlValidCtxt));
895 * @cur: the validation context to free
897 * Free a validation context structure.
900 xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
901 if (cur->vstateTab != NULL)
902 xmlFree(cur->vstateTab);
903 if (cur->nodeTab != NULL)
904 xmlFree(cur->nodeTab);
908 #endif /* LIBXML_VALID_ENABLED */
911 * xmlNewDocElementContent:
913 * @name: the subelement name or NULL
914 * @type: the type of element content decl
916 * Allocate an element content structure for the document.
918 * Returns NULL if not, otherwise the new element content structure
921 xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
922 xmlElementContentType type) {
923 xmlElementContentPtr ret;
924 xmlDictPtr dict = NULL;
930 case XML_ELEMENT_CONTENT_ELEMENT:
932 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
933 "xmlNewElementContent : name == NULL !\n",
937 case XML_ELEMENT_CONTENT_PCDATA:
938 case XML_ELEMENT_CONTENT_SEQ:
939 case XML_ELEMENT_CONTENT_OR:
941 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
942 "xmlNewElementContent : name != NULL !\n",
947 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
948 "Internal: ELEMENT content corrupted invalid type\n",
952 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
954 xmlVErrMemory(NULL, "malloc failed");
957 memset(ret, 0, sizeof(xmlElementContent));
959 ret->ocur = XML_ELEMENT_CONTENT_ONCE;
964 tmp = xmlSplitQName3(name, &l);
967 ret->name = xmlStrdup(name);
969 ret->name = xmlDictLookup(dict, name, -1);
972 ret->prefix = xmlStrndup(name, l);
973 ret->name = xmlStrdup(tmp);
975 ret->prefix = xmlDictLookup(dict, name, l);
976 ret->name = xmlDictLookup(dict, tmp, -1);
984 * xmlNewElementContent:
985 * @name: the subelement name or NULL
986 * @type: the type of element content decl
988 * Allocate an element content structure.
989 * Deprecated in favor of xmlNewDocElementContent
991 * Returns NULL if not, otherwise the new element content structure
994 xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
995 return(xmlNewDocElementContent(NULL, name, type));
999 * xmlCopyDocElementContent:
1000 * @doc: the document owning the element declaration
1001 * @cur: An element content pointer.
1003 * Build a copy of an element content description.
1005 * Returns the new xmlElementContentPtr or NULL in case of error.
1007 xmlElementContentPtr
1008 xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
1009 xmlElementContentPtr ret = NULL, prev = NULL, tmp;
1010 xmlDictPtr dict = NULL;
1012 if (cur == NULL) return(NULL);
1017 ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1019 xmlVErrMemory(NULL, "malloc failed");
1022 memset(ret, 0, sizeof(xmlElementContent));
1023 ret->type = cur->type;
1024 ret->ocur = cur->ocur;
1025 if (cur->name != NULL) {
1027 ret->name = xmlDictLookup(dict, cur->name, -1);
1029 ret->name = xmlStrdup(cur->name);
1032 if (cur->prefix != NULL) {
1034 ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
1036 ret->prefix = xmlStrdup(cur->prefix);
1038 if (cur->c1 != NULL)
1039 ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
1040 if (ret->c1 != NULL)
1041 ret->c1->parent = ret;
1042 if (cur->c2 != NULL) {
1045 while (cur != NULL) {
1046 tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1048 xmlVErrMemory(NULL, "malloc failed");
1051 memset(tmp, 0, sizeof(xmlElementContent));
1052 tmp->type = cur->type;
1053 tmp->ocur = cur->ocur;
1055 if (cur->name != NULL) {
1057 tmp->name = xmlDictLookup(dict, cur->name, -1);
1059 tmp->name = xmlStrdup(cur->name);
1062 if (cur->prefix != NULL) {
1064 tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
1066 tmp->prefix = xmlStrdup(cur->prefix);
1068 if (cur->c1 != NULL)
1069 tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
1070 if (tmp->c1 != NULL)
1071 tmp->c1->parent = ret;
1080 * xmlCopyElementContent:
1081 * @cur: An element content pointer.
1083 * Build a copy of an element content description.
1084 * Deprecated, use xmlCopyDocElementContent instead
1086 * Returns the new xmlElementContentPtr or NULL in case of error.
1088 xmlElementContentPtr
1089 xmlCopyElementContent(xmlElementContentPtr cur) {
1090 return(xmlCopyDocElementContent(NULL, cur));
1094 * xmlFreeDocElementContent:
1095 * @doc: the document owning the element declaration
1096 * @cur: the element content tree to free
1098 * Free an element content structure. The whole subtree is removed.
1101 xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
1102 xmlElementContentPtr next;
1103 xmlDictPtr dict = NULL;
1108 while (cur != NULL) {
1110 switch (cur->type) {
1111 case XML_ELEMENT_CONTENT_PCDATA:
1112 case XML_ELEMENT_CONTENT_ELEMENT:
1113 case XML_ELEMENT_CONTENT_SEQ:
1114 case XML_ELEMENT_CONTENT_OR:
1117 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1118 "Internal: ELEMENT content corrupted invalid type\n",
1122 if (cur->c1 != NULL) xmlFreeDocElementContent(doc, cur->c1);
1124 if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
1125 xmlFree((xmlChar *) cur->name);
1126 if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
1127 xmlFree((xmlChar *) cur->prefix);
1129 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
1130 if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
1138 * xmlFreeElementContent:
1139 * @cur: the element content tree to free
1141 * Free an element content structure. The whole subtree is removed.
1142 * Deprecated, use xmlFreeDocElementContent instead
1145 xmlFreeElementContent(xmlElementContentPtr cur) {
1146 xmlFreeDocElementContent(NULL, cur);
1149 #ifdef LIBXML_OUTPUT_ENABLED
1151 * xmlDumpElementContent:
1152 * @buf: An XML buffer
1153 * @content: An element table
1154 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
1156 * This will dump the content of the element table as an XML DTD definition
1159 xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
1160 if (content == NULL) return;
1162 if (glob) xmlBufferWriteChar(buf, "(");
1163 switch (content->type) {
1164 case XML_ELEMENT_CONTENT_PCDATA:
1165 xmlBufferWriteChar(buf, "#PCDATA");
1167 case XML_ELEMENT_CONTENT_ELEMENT:
1168 if (content->prefix != NULL) {
1169 xmlBufferWriteCHAR(buf, content->prefix);
1170 xmlBufferWriteChar(buf, ":");
1172 xmlBufferWriteCHAR(buf, content->name);
1174 case XML_ELEMENT_CONTENT_SEQ:
1175 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1176 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1177 xmlDumpElementContent(buf, content->c1, 1);
1179 xmlDumpElementContent(buf, content->c1, 0);
1180 xmlBufferWriteChar(buf, " , ");
1181 if ((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1182 ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) &&
1183 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
1184 xmlDumpElementContent(buf, content->c2, 1);
1186 xmlDumpElementContent(buf, content->c2, 0);
1188 case XML_ELEMENT_CONTENT_OR:
1189 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1190 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1191 xmlDumpElementContent(buf, content->c1, 1);
1193 xmlDumpElementContent(buf, content->c1, 0);
1194 xmlBufferWriteChar(buf, " | ");
1195 if ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1196 ((content->c2->type == XML_ELEMENT_CONTENT_OR) &&
1197 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
1198 xmlDumpElementContent(buf, content->c2, 1);
1200 xmlDumpElementContent(buf, content->c2, 0);
1203 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1204 "Internal: ELEMENT content corrupted invalid type\n",
1208 xmlBufferWriteChar(buf, ")");
1209 switch (content->ocur) {
1210 case XML_ELEMENT_CONTENT_ONCE:
1212 case XML_ELEMENT_CONTENT_OPT:
1213 xmlBufferWriteChar(buf, "?");
1215 case XML_ELEMENT_CONTENT_MULT:
1216 xmlBufferWriteChar(buf, "*");
1218 case XML_ELEMENT_CONTENT_PLUS:
1219 xmlBufferWriteChar(buf, "+");
1225 * xmlSprintfElementContent:
1226 * @buf: an output buffer
1227 * @content: An element table
1228 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
1230 * Deprecated, unsafe, use xmlSnprintfElementContent
1233 xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1234 xmlElementContentPtr content ATTRIBUTE_UNUSED,
1235 int englob ATTRIBUTE_UNUSED) {
1237 #endif /* LIBXML_OUTPUT_ENABLED */
1240 * xmlSnprintfElementContent:
1241 * @buf: an output buffer
1242 * @size: the buffer size
1243 * @content: An element table
1244 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
1246 * This will dump the content of the element content definition
1247 * Intended just for the debug routine
1250 xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) {
1253 if (content == NULL) return;
1255 if (size - len < 50) {
1256 if ((size - len > 4) && (buf[len - 1] != '.'))
1257 strcat(buf, " ...");
1260 if (englob) strcat(buf, "(");
1261 switch (content->type) {
1262 case XML_ELEMENT_CONTENT_PCDATA:
1263 strcat(buf, "#PCDATA");
1265 case XML_ELEMENT_CONTENT_ELEMENT:
1266 if (content->prefix != NULL) {
1267 if (size - len < xmlStrlen(content->prefix) + 10) {
1268 strcat(buf, " ...");
1271 strcat(buf, (char *) content->prefix);
1274 if (size - len < xmlStrlen(content->name) + 10) {
1275 strcat(buf, " ...");
1278 if (content->name != NULL)
1279 strcat(buf, (char *) content->name);
1281 case XML_ELEMENT_CONTENT_SEQ:
1282 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1283 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1284 xmlSnprintfElementContent(buf, size, content->c1, 1);
1286 xmlSnprintfElementContent(buf, size, content->c1, 0);
1288 if (size - len < 50) {
1289 if ((size - len > 4) && (buf[len - 1] != '.'))
1290 strcat(buf, " ...");
1294 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1295 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1296 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1297 xmlSnprintfElementContent(buf, size, content->c2, 1);
1299 xmlSnprintfElementContent(buf, size, content->c2, 0);
1301 case XML_ELEMENT_CONTENT_OR:
1302 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1303 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1304 xmlSnprintfElementContent(buf, size, content->c1, 1);
1306 xmlSnprintfElementContent(buf, size, content->c1, 0);
1308 if (size - len < 50) {
1309 if ((size - len > 4) && (buf[len - 1] != '.'))
1310 strcat(buf, " ...");
1314 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1315 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1316 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1317 xmlSnprintfElementContent(buf, size, content->c2, 1);
1319 xmlSnprintfElementContent(buf, size, content->c2, 0);
1324 switch (content->ocur) {
1325 case XML_ELEMENT_CONTENT_ONCE:
1327 case XML_ELEMENT_CONTENT_OPT:
1330 case XML_ELEMENT_CONTENT_MULT:
1333 case XML_ELEMENT_CONTENT_PLUS:
1339 /****************************************************************
1341 * Registration of DTD declarations *
1343 ****************************************************************/
1349 * Deallocate the memory used by an element definition
1352 xmlFreeElement(xmlElementPtr elem) {
1353 if (elem == NULL) return;
1354 xmlUnlinkNode((xmlNodePtr) elem);
1355 xmlFreeDocElementContent(elem->doc, elem->content);
1356 if (elem->name != NULL)
1357 xmlFree((xmlChar *) elem->name);
1358 if (elem->prefix != NULL)
1359 xmlFree((xmlChar *) elem->prefix);
1360 #ifdef LIBXML_REGEXP_ENABLED
1361 if (elem->contModel != NULL)
1362 xmlRegFreeRegexp(elem->contModel);
1369 * xmlAddElementDecl:
1370 * @ctxt: the validation context
1371 * @dtd: pointer to the DTD
1372 * @name: the entity name
1373 * @type: the element type
1374 * @content: the element content tree or NULL
1376 * Register a new element declaration
1378 * Returns NULL if not, otherwise the entity
1381 xmlAddElementDecl(xmlValidCtxtPtr ctxt,
1382 xmlDtdPtr dtd, const xmlChar *name,
1383 xmlElementTypeVal type,
1384 xmlElementContentPtr content) {
1386 xmlElementTablePtr table;
1387 xmlAttributePtr oldAttributes = NULL;
1388 xmlChar *ns, *uqname;
1398 case XML_ELEMENT_TYPE_EMPTY:
1399 if (content != NULL) {
1400 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1401 "xmlAddElementDecl: content != NULL for EMPTY\n",
1406 case XML_ELEMENT_TYPE_ANY:
1407 if (content != NULL) {
1408 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1409 "xmlAddElementDecl: content != NULL for ANY\n",
1414 case XML_ELEMENT_TYPE_MIXED:
1415 if (content == NULL) {
1416 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1417 "xmlAddElementDecl: content == NULL for MIXED\n",
1422 case XML_ELEMENT_TYPE_ELEMENT:
1423 if (content == NULL) {
1424 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1425 "xmlAddElementDecl: content == NULL for ELEMENT\n",
1431 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1432 "Internal: ELEMENT decl corrupted invalid type\n",
1438 * check if name is a QName
1440 uqname = xmlSplitQName2(name, &ns);
1445 * Create the Element table if needed.
1447 table = (xmlElementTablePtr) dtd->elements;
1448 if (table == NULL) {
1449 xmlDictPtr dict = NULL;
1451 if (dtd->doc != NULL)
1452 dict = dtd->doc->dict;
1453 table = xmlHashCreateDict(0, dict);
1454 dtd->elements = (void *) table;
1456 if (table == NULL) {
1458 "xmlAddElementDecl: Table creation failed!\n");
1467 * lookup old attributes inserted on an undefined element in the
1470 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1471 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1472 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1473 oldAttributes = ret->attributes;
1474 ret->attributes = NULL;
1475 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1476 xmlFreeElement(ret);
1481 * The element may already be present if one of its attribute
1482 * was registered first
1484 ret = xmlHashLookup2(table, name, ns);
1486 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1487 #ifdef LIBXML_VALID_ENABLED
1489 * The element is already defined in this DTD.
1491 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1492 "Redefinition of element %s\n",
1494 #endif /* LIBXML_VALID_ENABLED */
1506 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1508 xmlVErrMemory(ctxt, "malloc failed");
1515 memset(ret, 0, sizeof(xmlElement));
1516 ret->type = XML_ELEMENT_DECL;
1519 * fill the structure.
1521 ret->name = xmlStrdup(name);
1522 if (ret->name == NULL) {
1523 xmlVErrMemory(ctxt, "malloc failed");
1535 * Insertion must not fail
1537 if (xmlHashAddEntry2(table, name, ns, ret)) {
1538 #ifdef LIBXML_VALID_ENABLED
1540 * The element is already defined in this DTD.
1542 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1543 "Redefinition of element %s\n",
1545 #endif /* LIBXML_VALID_ENABLED */
1546 xmlFreeElement(ret);
1552 * For new element, may have attributes from earlier
1553 * definition in internal subset
1555 ret->attributes = oldAttributes;
1559 * Finish to fill the structure.
1563 * Avoid a stupid copy when called by the parser
1564 * and flag it by setting a special parent value
1565 * so the parser doesn't unallocate it.
1567 if ((ctxt != NULL) &&
1568 ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
1569 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1))) {
1570 ret->content = content;
1571 if (content != NULL)
1572 content->parent = (xmlElementContentPtr) 1;
1574 ret->content = xmlCopyDocElementContent(dtd->doc, content);
1578 * Link it to the DTD
1581 ret->doc = dtd->doc;
1582 if (dtd->last == NULL) {
1583 dtd->children = dtd->last = (xmlNodePtr) ret;
1585 dtd->last->next = (xmlNodePtr) ret;
1586 ret->prev = dtd->last;
1587 dtd->last = (xmlNodePtr) ret;
1595 * xmlFreeElementTable:
1596 * @table: An element table
1598 * Deallocate the memory used by an element hash table.
1601 xmlFreeElementTable(xmlElementTablePtr table) {
1602 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1605 #ifdef LIBXML_TREE_ENABLED
1610 * Build a copy of an element.
1612 * Returns the new xmlElementPtr or NULL in case of error.
1614 static xmlElementPtr
1615 xmlCopyElement(xmlElementPtr elem) {
1618 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1620 xmlVErrMemory(NULL, "malloc failed");
1623 memset(cur, 0, sizeof(xmlElement));
1624 cur->type = XML_ELEMENT_DECL;
1625 cur->etype = elem->etype;
1626 if (elem->name != NULL)
1627 cur->name = xmlStrdup(elem->name);
1630 if (elem->prefix != NULL)
1631 cur->prefix = xmlStrdup(elem->prefix);
1634 cur->content = xmlCopyElementContent(elem->content);
1635 /* TODO : rebuild the attribute list on the copy */
1636 cur->attributes = NULL;
1641 * xmlCopyElementTable:
1642 * @table: An element table
1644 * Build a copy of an element table.
1646 * Returns the new xmlElementTablePtr or NULL in case of error.
1649 xmlCopyElementTable(xmlElementTablePtr table) {
1650 return((xmlElementTablePtr) xmlHashCopy(table,
1651 (xmlHashCopier) xmlCopyElement));
1653 #endif /* LIBXML_TREE_ENABLED */
1655 #ifdef LIBXML_OUTPUT_ENABLED
1657 * xmlDumpElementDecl:
1658 * @buf: the XML buffer output
1659 * @elem: An element table
1661 * This will dump the content of the element declaration as an XML
1665 xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1666 if ((buf == NULL) || (elem == NULL))
1668 switch (elem->etype) {
1669 case XML_ELEMENT_TYPE_EMPTY:
1670 xmlBufferWriteChar(buf, "<!ELEMENT ");
1671 if (elem->prefix != NULL) {
1672 xmlBufferWriteCHAR(buf, elem->prefix);
1673 xmlBufferWriteChar(buf, ":");
1675 xmlBufferWriteCHAR(buf, elem->name);
1676 xmlBufferWriteChar(buf, " EMPTY>\n");
1678 case XML_ELEMENT_TYPE_ANY:
1679 xmlBufferWriteChar(buf, "<!ELEMENT ");
1680 if (elem->prefix != NULL) {
1681 xmlBufferWriteCHAR(buf, elem->prefix);
1682 xmlBufferWriteChar(buf, ":");
1684 xmlBufferWriteCHAR(buf, elem->name);
1685 xmlBufferWriteChar(buf, " ANY>\n");
1687 case XML_ELEMENT_TYPE_MIXED:
1688 xmlBufferWriteChar(buf, "<!ELEMENT ");
1689 if (elem->prefix != NULL) {
1690 xmlBufferWriteCHAR(buf, elem->prefix);
1691 xmlBufferWriteChar(buf, ":");
1693 xmlBufferWriteCHAR(buf, elem->name);
1694 xmlBufferWriteChar(buf, " ");
1695 xmlDumpElementContent(buf, elem->content, 1);
1696 xmlBufferWriteChar(buf, ">\n");
1698 case XML_ELEMENT_TYPE_ELEMENT:
1699 xmlBufferWriteChar(buf, "<!ELEMENT ");
1700 if (elem->prefix != NULL) {
1701 xmlBufferWriteCHAR(buf, elem->prefix);
1702 xmlBufferWriteChar(buf, ":");
1704 xmlBufferWriteCHAR(buf, elem->name);
1705 xmlBufferWriteChar(buf, " ");
1706 xmlDumpElementContent(buf, elem->content, 1);
1707 xmlBufferWriteChar(buf, ">\n");
1710 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1711 "Internal: ELEMENT struct corrupted invalid type\n",
1717 * xmlDumpElementDeclScan:
1718 * @elem: An element table
1719 * @buf: the XML buffer output
1721 * This routine is used by the hash scan function. It just reverses
1725 xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) {
1726 xmlDumpElementDecl(buf, elem);
1730 * xmlDumpElementTable:
1731 * @buf: the XML buffer output
1732 * @table: An element table
1734 * This will dump the content of the element table as an XML DTD definition
1737 xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1738 if ((buf == NULL) || (table == NULL))
1740 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf);
1742 #endif /* LIBXML_OUTPUT_ENABLED */
1745 * xmlCreateEnumeration:
1746 * @name: the enumeration name or NULL
1748 * create and initialize an enumeration attribute node.
1750 * Returns the xmlEnumerationPtr just created or NULL in case
1754 xmlCreateEnumeration(const xmlChar *name) {
1755 xmlEnumerationPtr ret;
1757 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1759 xmlVErrMemory(NULL, "malloc failed");
1762 memset(ret, 0, sizeof(xmlEnumeration));
1765 ret->name = xmlStrdup(name);
1770 * xmlFreeEnumeration:
1771 * @cur: the tree to free.
1773 * free an enumeration attribute node (recursive).
1776 xmlFreeEnumeration(xmlEnumerationPtr cur) {
1777 if (cur == NULL) return;
1779 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1781 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
1785 #ifdef LIBXML_TREE_ENABLED
1787 * xmlCopyEnumeration:
1788 * @cur: the tree to copy.
1790 * Copy an enumeration attribute node (recursive).
1792 * Returns the xmlEnumerationPtr just created or NULL in case
1796 xmlCopyEnumeration(xmlEnumerationPtr cur) {
1797 xmlEnumerationPtr ret;
1799 if (cur == NULL) return(NULL);
1800 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1801 if (ret == NULL) return(NULL);
1803 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1804 else ret->next = NULL;
1808 #endif /* LIBXML_TREE_ENABLED */
1810 #ifdef LIBXML_OUTPUT_ENABLED
1812 * xmlDumpEnumeration:
1813 * @buf: the XML buffer output
1814 * @enum: An enumeration
1816 * This will dump the content of the enumeration
1819 xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1820 if ((buf == NULL) || (cur == NULL))
1823 xmlBufferWriteCHAR(buf, cur->name);
1824 if (cur->next == NULL)
1825 xmlBufferWriteChar(buf, ")");
1827 xmlBufferWriteChar(buf, " | ");
1828 xmlDumpEnumeration(buf, cur->next);
1831 #endif /* LIBXML_OUTPUT_ENABLED */
1833 #ifdef LIBXML_VALID_ENABLED
1835 * xmlScanIDAttributeDecl:
1836 * @ctxt: the validation context
1837 * @elem: the element name
1838 * @err: whether to raise errors here
1840 * Verify that the element don't have too many ID attributes
1843 * Returns the number of ID attributes found.
1846 xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
1847 xmlAttributePtr cur;
1850 if (elem == NULL) return(0);
1851 cur = elem->attributes;
1852 while (cur != NULL) {
1853 if (cur->atype == XML_ATTRIBUTE_ID) {
1855 if ((ret > 1) && (err))
1856 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
1857 "Element %s has too many ID attributes defined : %s\n",
1858 elem->name, cur->name, NULL);
1864 #endif /* LIBXML_VALID_ENABLED */
1868 * @elem: An attribute
1870 * Deallocate the memory used by an attribute definition
1873 xmlFreeAttribute(xmlAttributePtr attr) {
1876 if (attr == NULL) return;
1877 if (attr->doc != NULL)
1878 dict = attr->doc->dict;
1881 xmlUnlinkNode((xmlNodePtr) attr);
1882 if (attr->tree != NULL)
1883 xmlFreeEnumeration(attr->tree);
1885 if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
1886 xmlFree((xmlChar *) attr->elem);
1887 if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
1888 xmlFree((xmlChar *) attr->name);
1889 if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
1890 xmlFree((xmlChar *) attr->prefix);
1891 if ((attr->defaultValue != NULL) &&
1892 (!xmlDictOwns(dict, attr->defaultValue)))
1893 xmlFree((xmlChar *) attr->defaultValue);
1895 if (attr->elem != NULL)
1896 xmlFree((xmlChar *) attr->elem);
1897 if (attr->name != NULL)
1898 xmlFree((xmlChar *) attr->name);
1899 if (attr->defaultValue != NULL)
1900 xmlFree((xmlChar *) attr->defaultValue);
1901 if (attr->prefix != NULL)
1902 xmlFree((xmlChar *) attr->prefix);
1909 * xmlAddAttributeDecl:
1910 * @ctxt: the validation context
1911 * @dtd: pointer to the DTD
1912 * @elem: the element name
1913 * @name: the attribute name
1914 * @ns: the attribute namespace prefix
1915 * @type: the attribute type
1916 * @def: the attribute default type
1917 * @defaultValue: the attribute default value
1918 * @tree: if it's an enumeration, the associated list
1920 * Register a new attribute declaration
1921 * Note that @tree becomes the ownership of the DTD
1923 * Returns NULL if not new, otherwise the attribute decl
1926 xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
1927 xmlDtdPtr dtd, const xmlChar *elem,
1928 const xmlChar *name, const xmlChar *ns,
1929 xmlAttributeType type, xmlAttributeDefault def,
1930 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1931 xmlAttributePtr ret;
1932 xmlAttributeTablePtr table;
1933 xmlElementPtr elemDef;
1934 xmlDictPtr dict = NULL;
1937 xmlFreeEnumeration(tree);
1941 xmlFreeEnumeration(tree);
1945 xmlFreeEnumeration(tree);
1948 if (dtd->doc != NULL)
1949 dict = dtd->doc->dict;
1951 #ifdef LIBXML_VALID_ENABLED
1953 * Check the type and possibly the default value.
1956 case XML_ATTRIBUTE_CDATA:
1958 case XML_ATTRIBUTE_ID:
1960 case XML_ATTRIBUTE_IDREF:
1962 case XML_ATTRIBUTE_IDREFS:
1964 case XML_ATTRIBUTE_ENTITY:
1966 case XML_ATTRIBUTE_ENTITIES:
1968 case XML_ATTRIBUTE_NMTOKEN:
1970 case XML_ATTRIBUTE_NMTOKENS:
1972 case XML_ATTRIBUTE_ENUMERATION:
1974 case XML_ATTRIBUTE_NOTATION:
1977 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1978 "Internal: ATTRIBUTE struct corrupted invalid type\n",
1980 xmlFreeEnumeration(tree);
1983 if ((defaultValue != NULL) &&
1984 (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) {
1985 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1986 "Attribute %s of %s: invalid default value\n",
1987 elem, name, defaultValue);
1988 defaultValue = NULL;
1992 #endif /* LIBXML_VALID_ENABLED */
1995 * Check first that an attribute defined in the external subset wasn't
1996 * already defined in the internal subset
1998 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1999 (dtd->doc->intSubset != NULL) &&
2000 (dtd->doc->intSubset->attributes != NULL)) {
2001 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
2003 xmlFreeEnumeration(tree);
2009 * Create the Attribute table if needed.
2011 table = (xmlAttributeTablePtr) dtd->attributes;
2012 if (table == NULL) {
2013 table = xmlHashCreateDict(0, dict);
2014 dtd->attributes = (void *) table;
2016 if (table == NULL) {
2018 "xmlAddAttributeDecl: Table creation failed!\n");
2019 xmlFreeEnumeration(tree);
2024 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2026 xmlVErrMemory(ctxt, "malloc failed");
2027 xmlFreeEnumeration(tree);
2030 memset(ret, 0, sizeof(xmlAttribute));
2031 ret->type = XML_ATTRIBUTE_DECL;
2034 * fill the structure.
2038 * doc must be set before possible error causes call
2039 * to xmlFreeAttribute (because it's used to check on
2042 ret->doc = dtd->doc;
2044 ret->name = xmlDictLookup(dict, name, -1);
2045 ret->prefix = xmlDictLookup(dict, ns, -1);
2046 ret->elem = xmlDictLookup(dict, elem, -1);
2048 ret->name = xmlStrdup(name);
2049 ret->prefix = xmlStrdup(ns);
2050 ret->elem = xmlStrdup(elem);
2054 if (defaultValue != NULL) {
2056 ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
2058 ret->defaultValue = xmlStrdup(defaultValue);
2063 * Search the DTD for previous declarations of the ATTLIST
2065 if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
2066 #ifdef LIBXML_VALID_ENABLED
2068 * The attribute is already defined in this DTD.
2070 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
2071 "Attribute %s of element %s: already defined\n",
2073 #endif /* LIBXML_VALID_ENABLED */
2074 xmlFreeAttribute(ret);
2080 * Multiple ID per element
2082 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
2083 if (elemDef != NULL) {
2085 #ifdef LIBXML_VALID_ENABLED
2086 if ((type == XML_ATTRIBUTE_ID) &&
2087 (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) {
2088 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
2089 "Element %s has too may ID attributes defined : %s\n",
2094 #endif /* LIBXML_VALID_ENABLED */
2097 * Insert namespace default def first they need to be
2100 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
2101 ((ret->prefix != NULL &&
2102 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
2103 ret->nexth = elemDef->attributes;
2104 elemDef->attributes = ret;
2106 xmlAttributePtr tmp = elemDef->attributes;
2108 while ((tmp != NULL) &&
2109 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
2110 ((ret->prefix != NULL &&
2111 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
2112 if (tmp->nexth == NULL)
2117 ret->nexth = tmp->nexth;
2120 ret->nexth = elemDef->attributes;
2121 elemDef->attributes = ret;
2127 * Link it to the DTD
2130 if (dtd->last == NULL) {
2131 dtd->children = dtd->last = (xmlNodePtr) ret;
2133 dtd->last->next = (xmlNodePtr) ret;
2134 ret->prev = dtd->last;
2135 dtd->last = (xmlNodePtr) ret;
2141 * xmlFreeAttributeTable:
2142 * @table: An attribute table
2144 * Deallocate the memory used by an entities hash table.
2147 xmlFreeAttributeTable(xmlAttributeTablePtr table) {
2148 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
2151 #ifdef LIBXML_TREE_ENABLED
2154 * @attr: An attribute
2156 * Build a copy of an attribute.
2158 * Returns the new xmlAttributePtr or NULL in case of error.
2160 static xmlAttributePtr
2161 xmlCopyAttribute(xmlAttributePtr attr) {
2162 xmlAttributePtr cur;
2164 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2166 xmlVErrMemory(NULL, "malloc failed");
2169 memset(cur, 0, sizeof(xmlAttribute));
2170 cur->type = XML_ATTRIBUTE_DECL;
2171 cur->atype = attr->atype;
2172 cur->def = attr->def;
2173 cur->tree = xmlCopyEnumeration(attr->tree);
2174 if (attr->elem != NULL)
2175 cur->elem = xmlStrdup(attr->elem);
2176 if (attr->name != NULL)
2177 cur->name = xmlStrdup(attr->name);
2178 if (attr->prefix != NULL)
2179 cur->prefix = xmlStrdup(attr->prefix);
2180 if (attr->defaultValue != NULL)
2181 cur->defaultValue = xmlStrdup(attr->defaultValue);
2186 * xmlCopyAttributeTable:
2187 * @table: An attribute table
2189 * Build a copy of an attribute table.
2191 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2193 xmlAttributeTablePtr
2194 xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2195 return((xmlAttributeTablePtr) xmlHashCopy(table,
2196 (xmlHashCopier) xmlCopyAttribute));
2198 #endif /* LIBXML_TREE_ENABLED */
2200 #ifdef LIBXML_OUTPUT_ENABLED
2202 * xmlDumpAttributeDecl:
2203 * @buf: the XML buffer output
2204 * @attr: An attribute declaration
2206 * This will dump the content of the attribute declaration as an XML
2210 xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
2211 if ((buf == NULL) || (attr == NULL))
2213 xmlBufferWriteChar(buf, "<!ATTLIST ");
2214 xmlBufferWriteCHAR(buf, attr->elem);
2215 xmlBufferWriteChar(buf, " ");
2216 if (attr->prefix != NULL) {
2217 xmlBufferWriteCHAR(buf, attr->prefix);
2218 xmlBufferWriteChar(buf, ":");
2220 xmlBufferWriteCHAR(buf, attr->name);
2221 switch (attr->atype) {
2222 case XML_ATTRIBUTE_CDATA:
2223 xmlBufferWriteChar(buf, " CDATA");
2225 case XML_ATTRIBUTE_ID:
2226 xmlBufferWriteChar(buf, " ID");
2228 case XML_ATTRIBUTE_IDREF:
2229 xmlBufferWriteChar(buf, " IDREF");
2231 case XML_ATTRIBUTE_IDREFS:
2232 xmlBufferWriteChar(buf, " IDREFS");
2234 case XML_ATTRIBUTE_ENTITY:
2235 xmlBufferWriteChar(buf, " ENTITY");
2237 case XML_ATTRIBUTE_ENTITIES:
2238 xmlBufferWriteChar(buf, " ENTITIES");
2240 case XML_ATTRIBUTE_NMTOKEN:
2241 xmlBufferWriteChar(buf, " NMTOKEN");
2243 case XML_ATTRIBUTE_NMTOKENS:
2244 xmlBufferWriteChar(buf, " NMTOKENS");
2246 case XML_ATTRIBUTE_ENUMERATION:
2247 xmlBufferWriteChar(buf, " (");
2248 xmlDumpEnumeration(buf, attr->tree);
2250 case XML_ATTRIBUTE_NOTATION:
2251 xmlBufferWriteChar(buf, " NOTATION (");
2252 xmlDumpEnumeration(buf, attr->tree);
2255 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2256 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2259 switch (attr->def) {
2260 case XML_ATTRIBUTE_NONE:
2262 case XML_ATTRIBUTE_REQUIRED:
2263 xmlBufferWriteChar(buf, " #REQUIRED");
2265 case XML_ATTRIBUTE_IMPLIED:
2266 xmlBufferWriteChar(buf, " #IMPLIED");
2268 case XML_ATTRIBUTE_FIXED:
2269 xmlBufferWriteChar(buf, " #FIXED");
2272 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2273 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2276 if (attr->defaultValue != NULL) {
2277 xmlBufferWriteChar(buf, " ");
2278 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2280 xmlBufferWriteChar(buf, ">\n");
2284 * xmlDumpAttributeDeclScan:
2285 * @attr: An attribute declaration
2286 * @buf: the XML buffer output
2288 * This is used with the hash scan function - just reverses arguments
2291 xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) {
2292 xmlDumpAttributeDecl(buf, attr);
2296 * xmlDumpAttributeTable:
2297 * @buf: the XML buffer output
2298 * @table: An attribute table
2300 * This will dump the content of the attribute table as an XML DTD definition
2303 xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
2304 if ((buf == NULL) || (table == NULL))
2306 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf);
2308 #endif /* LIBXML_OUTPUT_ENABLED */
2310 /************************************************************************
2314 ************************************************************************/
2319 * Deallocate the memory used by an notation definition
2322 xmlFreeNotation(xmlNotationPtr nota) {
2323 if (nota == NULL) return;
2324 if (nota->name != NULL)
2325 xmlFree((xmlChar *) nota->name);
2326 if (nota->PublicID != NULL)
2327 xmlFree((xmlChar *) nota->PublicID);
2328 if (nota->SystemID != NULL)
2329 xmlFree((xmlChar *) nota->SystemID);
2335 * xmlAddNotationDecl:
2336 * @dtd: pointer to the DTD
2337 * @ctxt: the validation context
2338 * @name: the entity name
2339 * @PublicID: the public identifier or NULL
2340 * @SystemID: the system identifier or NULL
2342 * Register a new notation declaration
2344 * Returns NULL if not, otherwise the entity
2347 xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
2348 const xmlChar *name,
2349 const xmlChar *PublicID, const xmlChar *SystemID) {
2351 xmlNotationTablePtr table;
2359 if ((PublicID == NULL) && (SystemID == NULL)) {
2364 * Create the Notation table if needed.
2366 table = (xmlNotationTablePtr) dtd->notations;
2367 if (table == NULL) {
2368 xmlDictPtr dict = NULL;
2369 if (dtd->doc != NULL)
2370 dict = dtd->doc->dict;
2372 dtd->notations = table = xmlHashCreateDict(0, dict);
2374 if (table == NULL) {
2376 "xmlAddNotationDecl: Table creation failed!\n");
2380 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2382 xmlVErrMemory(ctxt, "malloc failed");
2385 memset(ret, 0, sizeof(xmlNotation));
2388 * fill the structure.
2390 ret->name = xmlStrdup(name);
2391 if (SystemID != NULL)
2392 ret->SystemID = xmlStrdup(SystemID);
2393 if (PublicID != NULL)
2394 ret->PublicID = xmlStrdup(PublicID);
2398 * Check the DTD for previous declarations of the ATTLIST
2400 if (xmlHashAddEntry(table, name, ret)) {
2401 #ifdef LIBXML_VALID_ENABLED
2402 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2403 "xmlAddNotationDecl: %s already defined\n",
2404 (const char *) name);
2405 #endif /* LIBXML_VALID_ENABLED */
2406 xmlFreeNotation(ret);
2413 * xmlFreeNotationTable:
2414 * @table: An notation table
2416 * Deallocate the memory used by an entities hash table.
2419 xmlFreeNotationTable(xmlNotationTablePtr table) {
2420 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2423 #ifdef LIBXML_TREE_ENABLED
2428 * Build a copy of a notation.
2430 * Returns the new xmlNotationPtr or NULL in case of error.
2432 static xmlNotationPtr
2433 xmlCopyNotation(xmlNotationPtr nota) {
2436 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2438 xmlVErrMemory(NULL, "malloc failed");
2441 if (nota->name != NULL)
2442 cur->name = xmlStrdup(nota->name);
2445 if (nota->PublicID != NULL)
2446 cur->PublicID = xmlStrdup(nota->PublicID);
2448 cur->PublicID = NULL;
2449 if (nota->SystemID != NULL)
2450 cur->SystemID = xmlStrdup(nota->SystemID);
2452 cur->SystemID = NULL;
2457 * xmlCopyNotationTable:
2458 * @table: A notation table
2460 * Build a copy of a notation table.
2462 * Returns the new xmlNotationTablePtr or NULL in case of error.
2465 xmlCopyNotationTable(xmlNotationTablePtr table) {
2466 return((xmlNotationTablePtr) xmlHashCopy(table,
2467 (xmlHashCopier) xmlCopyNotation));
2469 #endif /* LIBXML_TREE_ENABLED */
2471 #ifdef LIBXML_OUTPUT_ENABLED
2473 * xmlDumpNotationDecl:
2474 * @buf: the XML buffer output
2475 * @nota: A notation declaration
2477 * This will dump the content the notation declaration as an XML DTD definition
2480 xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2481 if ((buf == NULL) || (nota == NULL))
2483 xmlBufferWriteChar(buf, "<!NOTATION ");
2484 xmlBufferWriteCHAR(buf, nota->name);
2485 if (nota->PublicID != NULL) {
2486 xmlBufferWriteChar(buf, " PUBLIC ");
2487 xmlBufferWriteQuotedString(buf, nota->PublicID);
2488 if (nota->SystemID != NULL) {
2489 xmlBufferWriteChar(buf, " ");
2490 xmlBufferWriteQuotedString(buf, nota->SystemID);
2493 xmlBufferWriteChar(buf, " SYSTEM ");
2494 xmlBufferWriteQuotedString(buf, nota->SystemID);
2496 xmlBufferWriteChar(buf, " >\n");
2500 * xmlDumpNotationDeclScan:
2501 * @nota: A notation declaration
2502 * @buf: the XML buffer output
2504 * This is called with the hash scan function, and just reverses args
2507 xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) {
2508 xmlDumpNotationDecl(buf, nota);
2512 * xmlDumpNotationTable:
2513 * @buf: the XML buffer output
2514 * @table: A notation table
2516 * This will dump the content of the notation table as an XML DTD definition
2519 xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2520 if ((buf == NULL) || (table == NULL))
2522 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf);
2524 #endif /* LIBXML_OUTPUT_ENABLED */
2526 /************************************************************************
2530 ************************************************************************/
2535 * Free a string if it is not owned by the "dict" dictionary in the
2538 #define DICT_FREE(str) \
2539 if ((str) && ((!dict) || \
2540 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
2541 xmlFree((char *)(str));
2547 * Deallocate the memory used by an id definition
2550 xmlFreeID(xmlIDPtr id) {
2551 xmlDictPtr dict = NULL;
2553 if (id == NULL) return;
2555 if (id->doc != NULL)
2556 dict = id->doc->dict;
2558 if (id->value != NULL)
2559 DICT_FREE(id->value)
2560 if (id->name != NULL)
2568 * @ctxt: the validation context
2569 * @doc: pointer to the document
2570 * @value: the value name
2571 * @attr: the attribute holding the ID
2573 * Register a new id declaration
2575 * Returns NULL if not, otherwise the new xmlIDPtr
2578 xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2581 xmlIDTablePtr table;
2586 if (value == NULL) {
2594 * Create the ID table if needed.
2596 table = (xmlIDTablePtr) doc->ids;
2597 if (table == NULL) {
2598 doc->ids = table = xmlHashCreateDict(0, doc->dict);
2600 if (table == NULL) {
2602 "xmlAddID: Table creation failed!\n");
2606 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2608 xmlVErrMemory(ctxt, "malloc failed");
2613 * fill the structure.
2615 ret->value = xmlStrdup(value);
2617 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2619 * Operating in streaming mode, attr is gonna disapear
2621 if (doc->dict != NULL)
2622 ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2624 ret->name = xmlStrdup(attr->name);
2630 ret->lineno = xmlGetLineNo(attr->parent);
2632 if (xmlHashAddEntry(table, value, ret) < 0) {
2633 #ifdef LIBXML_VALID_ENABLED
2635 * The id is already defined in this DTD.
2638 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2639 "ID %s already defined\n", value, NULL, NULL);
2641 #endif /* LIBXML_VALID_ENABLED */
2646 attr->atype = XML_ATTRIBUTE_ID;
2652 * @table: An id table
2654 * Deallocate the memory used by an ID hash table.
2657 xmlFreeIDTable(xmlIDTablePtr table) {
2658 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2663 * @doc: the document
2664 * @elem: the element carrying the attribute
2665 * @attr: the attribute
2667 * Determine whether an attribute is of type ID. In case we have DTD(s)
2668 * then this is done if DTD loading has been requested. In the case
2669 * of HTML documents parsed with the HTML parser, then ID detection is
2670 * done systematically.
2672 * Returns 0 or 1 depending on the lookup result
2675 xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2676 if ((attr == NULL) || (attr->name == NULL)) return(0);
2677 if ((attr->ns != NULL) && (attr->ns->prefix != NULL) &&
2678 (!strcmp((char *) attr->name, "id")) &&
2679 (!strcmp((char *) attr->ns->prefix, "xml")))
2681 if (doc == NULL) return(0);
2682 if ((doc->intSubset == NULL) && (doc->extSubset == NULL) &&
2683 (doc->type != XML_HTML_DOCUMENT_NODE)) {
2685 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2686 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2687 ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
2688 ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a")))))
2691 } else if (elem == NULL) {
2694 xmlAttributePtr attrDecl = NULL;
2696 xmlChar felem[50], fattr[50];
2697 xmlChar *fullelemname, *fullattrname;
2699 fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
2700 xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
2701 (xmlChar *)elem->name;
2703 fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ?
2704 xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) :
2705 (xmlChar *)attr->name;
2707 if (fullelemname != NULL && fullattrname != NULL) {
2708 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname,
2710 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2711 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname,
2715 if ((fullattrname != fattr) && (fullattrname != attr->name))
2716 xmlFree(fullattrname);
2717 if ((fullelemname != felem) && (fullelemname != elem->name))
2718 xmlFree(fullelemname);
2720 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2728 * @doc: the document
2729 * @attr: the attribute
2731 * Remove the given attribute from the ID table maintained internally.
2733 * Returns -1 if the lookup failed and 0 otherwise
2736 xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2737 xmlIDTablePtr table;
2741 if (doc == NULL) return(-1);
2742 if (attr == NULL) return(-1);
2744 table = (xmlIDTablePtr) doc->ids;
2748 ID = xmlNodeListGetString(doc, attr->children, 1);
2752 id = xmlHashLookup(table, ID);
2753 if (id == NULL || id->attr != attr) {
2758 xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID);
2766 * @doc: pointer to the document
2769 * Search the attribute declaring the given ID
2771 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2774 xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2775 xmlIDTablePtr table;
2786 table = (xmlIDTablePtr) doc->ids;
2790 id = xmlHashLookup(table, ID);
2793 if (id->attr == NULL) {
2795 * We are operating on a stream, return a well known reference
2796 * since the attribute node doesn't exist anymore
2798 return((xmlAttrPtr) doc);
2803 /************************************************************************
2807 ************************************************************************/
2808 typedef struct xmlRemoveMemo_t
2814 typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2816 typedef struct xmlValidateMemo_t
2818 xmlValidCtxtPtr ctxt;
2819 const xmlChar *name;
2822 typedef xmlValidateMemo *xmlValidateMemoPtr;
2828 * Deallocate the memory used by a ref definition
2831 xmlFreeRef(xmlLinkPtr lk) {
2832 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2833 if (ref == NULL) return;
2834 if (ref->value != NULL)
2835 xmlFree((xmlChar *)ref->value);
2836 if (ref->name != NULL)
2837 xmlFree((xmlChar *)ref->name);
2843 * @list_ref: A list of references.
2845 * Deallocate the memory used by a list of references
2848 xmlFreeRefList(xmlListPtr list_ref) {
2849 if (list_ref == NULL) return;
2850 xmlListDelete(list_ref);
2855 * @data: Contents of current link
2856 * @user: Value supplied by the user
2858 * Returns 0 to abort the walk or 1 to continue
2861 xmlWalkRemoveRef(const void *data, const void *user)
2863 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2864 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2865 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
2867 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2868 xmlListRemoveFirst(ref_list, (void *)data);
2876 * @data0: Value supplied by the user
2877 * @data1: Value supplied by the user
2879 * Do nothing, return 0. Used to create unordered lists.
2882 xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2883 const void *data1 ATTRIBUTE_UNUSED)
2890 * @ctxt: the validation context
2891 * @doc: pointer to the document
2892 * @value: the value name
2893 * @attr: the attribute holding the Ref
2895 * Register a new ref declaration
2897 * Returns NULL if not, otherwise the new xmlRefPtr
2900 xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2903 xmlRefTablePtr table;
2904 xmlListPtr ref_list;
2909 if (value == NULL) {
2917 * Create the Ref table if needed.
2919 table = (xmlRefTablePtr) doc->refs;
2920 if (table == NULL) {
2921 doc->refs = table = xmlHashCreateDict(0, doc->dict);
2923 if (table == NULL) {
2925 "xmlAddRef: Table creation failed!\n");
2929 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2931 xmlVErrMemory(ctxt, "malloc failed");
2936 * fill the structure.
2938 ret->value = xmlStrdup(value);
2939 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2941 * Operating in streaming mode, attr is gonna disapear
2943 ret->name = xmlStrdup(attr->name);
2949 ret->lineno = xmlGetLineNo(attr->parent);
2951 /* To add a reference :-
2952 * References are maintained as a list of references,
2953 * Lookup the entry, if no entry create new nodelist
2954 * Add the owning node to the NodeList
2958 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2959 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
2960 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2961 "xmlAddRef: Reference list creation failed!\n",
2965 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2966 xmlListDelete(ref_list);
2967 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2968 "xmlAddRef: Reference list insertion failed!\n",
2973 if (xmlListAppend(ref_list, ret) != 0) {
2974 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2975 "xmlAddRef: Reference list insertion failed!\n",
2982 if (ret->value != NULL)
2983 xmlFree((char *)ret->value);
2984 if (ret->name != NULL)
2985 xmlFree((char *)ret->name);
2993 * @table: An ref table
2995 * Deallocate the memory used by an Ref hash table.
2998 xmlFreeRefTable(xmlRefTablePtr table) {
2999 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
3004 * @doc: the document
3005 * @elem: the element carrying the attribute
3006 * @attr: the attribute
3008 * Determine whether an attribute is of type Ref. In case we have DTD(s)
3009 * then this is simple, otherwise we use an heuristic: name Ref (upper
3012 * Returns 0 or 1 depending on the lookup result
3015 xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
3020 if (doc == NULL) return(0);
3023 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
3025 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
3029 xmlAttributePtr attrDecl;
3031 if (elem == NULL) return(0);
3032 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
3033 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3034 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3035 elem->name, attr->name);
3037 if ((attrDecl != NULL) &&
3038 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
3039 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
3047 * @doc: the document
3048 * @attr: the attribute
3050 * Remove the given attribute from the Ref table maintained internally.
3052 * Returns -1 if the lookup failed and 0 otherwise
3055 xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
3056 xmlListPtr ref_list;
3057 xmlRefTablePtr table;
3059 xmlRemoveMemo target;
3061 if (doc == NULL) return(-1);
3062 if (attr == NULL) return(-1);
3064 table = (xmlRefTablePtr) doc->refs;
3068 ID = xmlNodeListGetString(doc, attr->children, 1);
3072 ref_list = xmlHashLookup(table, ID);
3073 if(ref_list == NULL) {
3078 /* At this point, ref_list refers to a list of references which
3079 * have the same key as the supplied attr. Our list of references
3080 * is ordered by reference address and we don't have that information
3081 * here to use when removing. We'll have to walk the list and
3082 * check for a matching attribute, when we find one stop the walk
3083 * and remove the entry.
3084 * The list is ordered by reference, so that means we don't have the
3085 * key. Passing the list and the reference to the walker means we
3086 * will have enough data to be able to remove the entry.
3088 target.l = ref_list;
3091 /* Remove the supplied attr from our list */
3092 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
3094 /*If the list is empty then remove the list entry in the hash */
3095 if (xmlListEmpty(ref_list))
3096 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
3104 * @doc: pointer to the document
3107 * Find the set of references for the supplied ID.
3109 * Returns NULL if not found, otherwise node set for the ID.
3112 xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
3113 xmlRefTablePtr table;
3123 table = (xmlRefTablePtr) doc->refs;
3127 return (xmlHashLookup(table, ID));
3130 /************************************************************************
3132 * Routines for validity checking *
3134 ************************************************************************/
3137 * xmlGetDtdElementDesc:
3138 * @dtd: a pointer to the DtD to search
3139 * @name: the element name
3141 * Search the DTD for the description of this element
3143 * returns the xmlElementPtr if found or NULL
3147 xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
3148 xmlElementTablePtr table;
3150 xmlChar *uqname = NULL, *prefix = NULL;
3152 if ((dtd == NULL) || (name == NULL)) return(NULL);
3153 if (dtd->elements == NULL)
3155 table = (xmlElementTablePtr) dtd->elements;
3157 uqname = xmlSplitQName2(name, &prefix);
3160 cur = xmlHashLookup2(table, name, prefix);
3161 if (prefix != NULL) xmlFree(prefix);
3162 if (uqname != NULL) xmlFree(uqname);
3166 * xmlGetDtdElementDesc2:
3167 * @dtd: a pointer to the DtD to search
3168 * @name: the element name
3169 * @create: create an empty description if not found
3171 * Search the DTD for the description of this element
3173 * returns the xmlElementPtr if found or NULL
3176 static xmlElementPtr
3177 xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
3178 xmlElementTablePtr table;
3180 xmlChar *uqname = NULL, *prefix = NULL;
3182 if (dtd == NULL) return(NULL);
3183 if (dtd->elements == NULL) {
3184 xmlDictPtr dict = NULL;
3186 if (dtd->doc != NULL)
3187 dict = dtd->doc->dict;
3192 * Create the Element table if needed.
3194 table = (xmlElementTablePtr) dtd->elements;
3195 if (table == NULL) {
3196 table = xmlHashCreateDict(0, dict);
3197 dtd->elements = (void *) table;
3199 if (table == NULL) {
3200 xmlVErrMemory(NULL, "element table allocation failed");
3204 table = (xmlElementTablePtr) dtd->elements;
3206 uqname = xmlSplitQName2(name, &prefix);
3209 cur = xmlHashLookup2(table, name, prefix);
3210 if ((cur == NULL) && (create)) {
3211 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3213 xmlVErrMemory(NULL, "malloc failed");
3216 memset(cur, 0, sizeof(xmlElement));
3217 cur->type = XML_ELEMENT_DECL;
3220 * fill the structure.
3222 cur->name = xmlStrdup(name);
3223 cur->prefix = xmlStrdup(prefix);
3224 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3226 xmlHashAddEntry2(table, name, prefix, cur);
3228 if (prefix != NULL) xmlFree(prefix);
3229 if (uqname != NULL) xmlFree(uqname);
3234 * xmlGetDtdQElementDesc:
3235 * @dtd: a pointer to the DtD to search
3236 * @name: the element name
3237 * @prefix: the element namespace prefix
3239 * Search the DTD for the description of this element
3241 * returns the xmlElementPtr if found or NULL
3245 xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3246 const xmlChar *prefix) {
3247 xmlElementTablePtr table;
3249 if (dtd == NULL) return(NULL);
3250 if (dtd->elements == NULL) return(NULL);
3251 table = (xmlElementTablePtr) dtd->elements;
3253 return(xmlHashLookup2(table, name, prefix));
3257 * xmlGetDtdAttrDesc:
3258 * @dtd: a pointer to the DtD to search
3259 * @elem: the element name
3260 * @name: the attribute name
3262 * Search the DTD for the description of this attribute on
3265 * returns the xmlAttributePtr if found or NULL
3269 xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3270 xmlAttributeTablePtr table;
3271 xmlAttributePtr cur;
3272 xmlChar *uqname = NULL, *prefix = NULL;
3274 if (dtd == NULL) return(NULL);
3275 if (dtd->attributes == NULL) return(NULL);
3277 table = (xmlAttributeTablePtr) dtd->attributes;
3281 uqname = xmlSplitQName2(name, &prefix);
3283 if (uqname != NULL) {
3284 cur = xmlHashLookup3(table, uqname, prefix, elem);
3285 if (prefix != NULL) xmlFree(prefix);
3286 if (uqname != NULL) xmlFree(uqname);
3288 cur = xmlHashLookup3(table, name, NULL, elem);
3293 * xmlGetDtdQAttrDesc:
3294 * @dtd: a pointer to the DtD to search
3295 * @elem: the element name
3296 * @name: the attribute name
3297 * @prefix: the attribute namespace prefix
3299 * Search the DTD for the description of this qualified attribute on
3302 * returns the xmlAttributePtr if found or NULL
3306 xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3307 const xmlChar *prefix) {
3308 xmlAttributeTablePtr table;
3310 if (dtd == NULL) return(NULL);
3311 if (dtd->attributes == NULL) return(NULL);
3312 table = (xmlAttributeTablePtr) dtd->attributes;
3314 return(xmlHashLookup3(table, name, prefix, elem));
3318 * xmlGetDtdNotationDesc:
3319 * @dtd: a pointer to the DtD to search
3320 * @name: the notation name
3322 * Search the DTD for the description of this notation
3324 * returns the xmlNotationPtr if found or NULL
3328 xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3329 xmlNotationTablePtr table;
3331 if (dtd == NULL) return(NULL);
3332 if (dtd->notations == NULL) return(NULL);
3333 table = (xmlNotationTablePtr) dtd->notations;
3335 return(xmlHashLookup(table, name));
3338 #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
3340 * xmlValidateNotationUse:
3341 * @ctxt: the validation context
3342 * @doc: the document
3343 * @notationName: the notation name to check
3345 * Validate that the given name match a notation declaration.
3346 * - [ VC: Notation Declared ]
3348 * returns 1 if valid or 0 otherwise
3352 xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3353 const xmlChar *notationName) {
3354 xmlNotationPtr notaDecl;
3355 if ((doc == NULL) || (doc->intSubset == NULL) ||
3356 (notationName == NULL)) return(-1);
3358 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3359 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3360 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3362 if ((notaDecl == NULL) && (ctxt != NULL)) {
3363 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3364 "NOTATION %s is not declared\n",
3365 notationName, NULL, NULL);
3370 #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
3373 * xmlIsMixedElement:
3374 * @doc: the document
3375 * @name: the element name
3377 * Search in the DtDs whether an element accept Mixed content (or ANY)
3378 * basically if it is supposed to accept text childs
3380 * returns 0 if no, 1 if yes, and -1 if no element description is available
3384 xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3385 xmlElementPtr elemDecl;
3387 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3389 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3390 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3391 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3392 if (elemDecl == NULL) return(-1);
3393 switch (elemDecl->etype) {
3394 case XML_ELEMENT_TYPE_UNDEFINED:
3396 case XML_ELEMENT_TYPE_ELEMENT:
3398 case XML_ELEMENT_TYPE_EMPTY:
3400 * return 1 for EMPTY since we want VC error to pop up
3401 * on <empty> </empty> for example
3403 case XML_ELEMENT_TYPE_ANY:
3404 case XML_ELEMENT_TYPE_MIXED:
3410 #ifdef LIBXML_VALID_ENABLED
3413 xmlIsDocNameStartChar(xmlDocPtr doc, int c) {
3414 if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3416 * Use the new checks of production [4] [4a] amd [5] of the
3417 * Update 5 of XML-1.0
3419 if (((c >= 'a') && (c <= 'z')) ||
3420 ((c >= 'A') && (c <= 'Z')) ||
3421 (c == '_') || (c == ':') ||
3422 ((c >= 0xC0) && (c <= 0xD6)) ||
3423 ((c >= 0xD8) && (c <= 0xF6)) ||
3424 ((c >= 0xF8) && (c <= 0x2FF)) ||
3425 ((c >= 0x370) && (c <= 0x37D)) ||
3426 ((c >= 0x37F) && (c <= 0x1FFF)) ||
3427 ((c >= 0x200C) && (c <= 0x200D)) ||
3428 ((c >= 0x2070) && (c <= 0x218F)) ||
3429 ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3430 ((c >= 0x3001) && (c <= 0xD7FF)) ||
3431 ((c >= 0xF900) && (c <= 0xFDCF)) ||
3432 ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3433 ((c >= 0x10000) && (c <= 0xEFFFF)))
3436 if (IS_LETTER(c) || (c == '_') || (c == ':'))
3443 xmlIsDocNameChar(xmlDocPtr doc, int c) {
3444 if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3446 * Use the new checks of production [4] [4a] amd [5] of the
3447 * Update 5 of XML-1.0
3449 if (((c >= 'a') && (c <= 'z')) ||
3450 ((c >= 'A') && (c <= 'Z')) ||
3451 ((c >= '0') && (c <= '9')) || /* !start */
3452 (c == '_') || (c == ':') ||
3453 (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
3454 ((c >= 0xC0) && (c <= 0xD6)) ||
3455 ((c >= 0xD8) && (c <= 0xF6)) ||
3456 ((c >= 0xF8) && (c <= 0x2FF)) ||
3457 ((c >= 0x300) && (c <= 0x36F)) || /* !start */
3458 ((c >= 0x370) && (c <= 0x37D)) ||
3459 ((c >= 0x37F) && (c <= 0x1FFF)) ||
3460 ((c >= 0x200C) && (c <= 0x200D)) ||
3461 ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
3462 ((c >= 0x2070) && (c <= 0x218F)) ||
3463 ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3464 ((c >= 0x3001) && (c <= 0xD7FF)) ||
3465 ((c >= 0xF900) && (c <= 0xFDCF)) ||
3466 ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3467 ((c >= 0x10000) && (c <= 0xEFFFF)))
3470 if ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
3471 (c == '.') || (c == '-') ||
3472 (c == '_') || (c == ':') ||
3473 (IS_COMBINING(c)) ||
3481 * xmlValidateNameValue:
3482 * @doc: pointer to the document or NULL
3483 * @value: an Name value
3485 * Validate that the given value match Name production
3487 * returns 1 if valid or 0 otherwise
3491 xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) {
3495 if (value == NULL) return(0);
3497 val = xmlStringCurrentChar(NULL, cur, &len);
3499 if (!xmlIsDocNameStartChar(doc, val))
3502 val = xmlStringCurrentChar(NULL, cur, &len);
3504 while (xmlIsDocNameChar(doc, val)) {
3505 val = xmlStringCurrentChar(NULL, cur, &len);
3509 if (val != 0) return(0);
3515 * xmlValidateNameValue:
3516 * @value: an Name value
3518 * Validate that the given value match Name production
3520 * returns 1 if valid or 0 otherwise
3524 xmlValidateNameValue(const xmlChar *value) {
3525 return(xmlValidateNameValueInternal(NULL, value));
3529 * xmlValidateNamesValueInternal:
3530 * @doc: pointer to the document or NULL
3531 * @value: an Names value
3533 * Validate that the given value match Names production
3535 * returns 1 if valid or 0 otherwise
3539 xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) {
3543 if (value == NULL) return(0);
3545 val = xmlStringCurrentChar(NULL, cur, &len);
3548 if (!xmlIsDocNameStartChar(doc, val))
3551 val = xmlStringCurrentChar(NULL, cur, &len);
3553 while (xmlIsDocNameChar(doc, val)) {
3554 val = xmlStringCurrentChar(NULL, cur, &len);
3558 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3559 while (val == 0x20) {
3560 while (val == 0x20) {
3561 val = xmlStringCurrentChar(NULL, cur, &len);
3565 if (!xmlIsDocNameStartChar(doc, val))
3568 val = xmlStringCurrentChar(NULL, cur, &len);
3571 while (xmlIsDocNameChar(doc, val)) {
3572 val = xmlStringCurrentChar(NULL, cur, &len);
3577 if (val != 0) return(0);
3583 * xmlValidateNamesValue:
3584 * @value: an Names value
3586 * Validate that the given value match Names production
3588 * returns 1 if valid or 0 otherwise
3592 xmlValidateNamesValue(const xmlChar *value) {
3593 return(xmlValidateNamesValueInternal(NULL, value));
3597 * xmlValidateNmtokenValueInternal:
3598 * @doc: pointer to the document or NULL
3599 * @value: an Nmtoken value
3601 * Validate that the given value match Nmtoken production
3603 * [ VC: Name Token ]
3605 * returns 1 if valid or 0 otherwise
3609 xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) {
3613 if (value == NULL) return(0);
3615 val = xmlStringCurrentChar(NULL, cur, &len);
3618 if (!xmlIsDocNameChar(doc, val))
3621 val = xmlStringCurrentChar(NULL, cur, &len);
3623 while (xmlIsDocNameChar(doc, val)) {
3624 val = xmlStringCurrentChar(NULL, cur, &len);
3628 if (val != 0) return(0);
3634 * xmlValidateNmtokenValue:
3635 * @value: an Nmtoken value
3637 * Validate that the given value match Nmtoken production
3639 * [ VC: Name Token ]
3641 * returns 1 if valid or 0 otherwise
3645 xmlValidateNmtokenValue(const xmlChar *value) {
3646 return(xmlValidateNmtokenValueInternal(NULL, value));
3650 * xmlValidateNmtokensValueInternal:
3651 * @doc: pointer to the document or NULL
3652 * @value: an Nmtokens value
3654 * Validate that the given value match Nmtokens production
3656 * [ VC: Name Token ]
3658 * returns 1 if valid or 0 otherwise
3662 xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) {
3666 if (value == NULL) return(0);
3668 val = xmlStringCurrentChar(NULL, cur, &len);
3671 while (IS_BLANK(val)) {
3672 val = xmlStringCurrentChar(NULL, cur, &len);
3676 if (!xmlIsDocNameChar(doc, val))
3679 while (xmlIsDocNameChar(doc, val)) {
3680 val = xmlStringCurrentChar(NULL, cur, &len);
3684 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3685 while (val == 0x20) {
3686 while (val == 0x20) {
3687 val = xmlStringCurrentChar(NULL, cur, &len);
3690 if (val == 0) return(1);
3692 if (!xmlIsDocNameChar(doc, val))
3695 val = xmlStringCurrentChar(NULL, cur, &len);
3698 while (xmlIsDocNameChar(doc, val)) {
3699 val = xmlStringCurrentChar(NULL, cur, &len);
3704 if (val != 0) return(0);
3710 * xmlValidateNmtokensValue:
3711 * @value: an Nmtokens value
3713 * Validate that the given value match Nmtokens production
3715 * [ VC: Name Token ]
3717 * returns 1 if valid or 0 otherwise
3721 xmlValidateNmtokensValue(const xmlChar *value) {
3722 return(xmlValidateNmtokensValueInternal(NULL, value));
3726 * xmlValidateNotationDecl:
3727 * @ctxt: the validation context
3728 * @doc: a document instance
3729 * @nota: a notation definition
3731 * Try to validate a single notation definition
3732 * basically it does the following checks as described by the
3733 * XML-1.0 recommendation:
3734 * - it seems that no validity constraint exists on notation declarations
3735 * But this function get called anyway ...
3737 * returns 1 if valid or 0 otherwise
3741 xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3742 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
3749 * xmlValidateAttributeValueInternal:
3750 * @doc: the document
3751 * @type: an attribute type
3752 * @value: an attribute value
3754 * Validate that the given attribute value match the proper production
3756 * returns 1 if valid or 0 otherwise
3760 xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
3761 const xmlChar *value) {
3763 case XML_ATTRIBUTE_ENTITIES:
3764 case XML_ATTRIBUTE_IDREFS:
3765 return(xmlValidateNamesValueInternal(doc, value));
3766 case XML_ATTRIBUTE_ENTITY:
3767 case XML_ATTRIBUTE_IDREF:
3768 case XML_ATTRIBUTE_ID:
3769 case XML_ATTRIBUTE_NOTATION:
3770 return(xmlValidateNameValueInternal(doc, value));
3771 case XML_ATTRIBUTE_NMTOKENS:
3772 case XML_ATTRIBUTE_ENUMERATION:
3773 return(xmlValidateNmtokensValueInternal(doc, value));
3774 case XML_ATTRIBUTE_NMTOKEN:
3775 return(xmlValidateNmtokenValueInternal(doc, value));
3776 case XML_ATTRIBUTE_CDATA:
3783 * xmlValidateAttributeValue:
3784 * @type: an attribute type
3785 * @value: an attribute value
3787 * Validate that the given attribute value match the proper production
3790 * Values of type ID must match the Name production....
3793 * Values of type IDREF must match the Name production, and values
3794 * of type IDREFS must match Names ...
3796 * [ VC: Entity Name ]
3797 * Values of type ENTITY must match the Name production, values
3798 * of type ENTITIES must match Names ...
3800 * [ VC: Name Token ]
3801 * Values of type NMTOKEN must match the Nmtoken production; values
3802 * of type NMTOKENS must match Nmtokens.
3804 * returns 1 if valid or 0 otherwise
3807 xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3808 return(xmlValidateAttributeValueInternal(NULL, type, value));
3812 * xmlValidateAttributeValue2:
3813 * @ctxt: the validation context
3814 * @doc: the document
3815 * @name: the attribute name (used for error reporting only)
3816 * @type: the attribute type
3817 * @value: the attribute value
3819 * Validate that the given attribute value match a given type.
3820 * This typically cannot be done before having finished parsing
3824 * Values of type IDREF must match one of the declared IDs
3825 * Values of type IDREFS must match a sequence of the declared IDs
3826 * each Name must match the value of an ID attribute on some element
3827 * in the XML document; i.e. IDREF values must match the value of
3830 * [ VC: Entity Name ]
3831 * Values of type ENTITY must match one declared entity
3832 * Values of type ENTITIES must match a sequence of declared entities
3834 * [ VC: Notation Attributes ]
3835 * all notation names in the declaration must be declared.
3837 * returns 1 if valid or 0 otherwise
3841 xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3842 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3845 case XML_ATTRIBUTE_IDREFS:
3846 case XML_ATTRIBUTE_IDREF:
3847 case XML_ATTRIBUTE_ID:
3848 case XML_ATTRIBUTE_NMTOKENS:
3849 case XML_ATTRIBUTE_ENUMERATION:
3850 case XML_ATTRIBUTE_NMTOKEN:
3851 case XML_ATTRIBUTE_CDATA:
3853 case XML_ATTRIBUTE_ENTITY: {
3856 ent = xmlGetDocEntity(doc, value);
3857 /* yeah it's a bit messy... */
3858 if ((ent == NULL) && (doc->standalone == 1)) {
3859 doc->standalone = 0;
3860 ent = xmlGetDocEntity(doc, value);
3863 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3864 XML_DTD_UNKNOWN_ENTITY,
3865 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3868 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3869 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3870 XML_DTD_ENTITY_TYPE,
3871 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3877 case XML_ATTRIBUTE_ENTITIES: {
3878 xmlChar *dup, *nam = NULL, *cur, save;
3881 dup = xmlStrdup(value);
3887 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
3890 ent = xmlGetDocEntity(doc, nam);
3892 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3893 XML_DTD_UNKNOWN_ENTITY,
3894 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3897 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3898 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3899 XML_DTD_ENTITY_TYPE,
3900 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3907 while (IS_BLANK_CH(*cur)) cur++;
3912 case XML_ATTRIBUTE_NOTATION: {
3913 xmlNotationPtr nota;
3915 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3916 if ((nota == NULL) && (doc->extSubset != NULL))
3917 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3920 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3921 XML_DTD_UNKNOWN_NOTATION,
3922 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3933 * xmlValidCtxtNormalizeAttributeValue:
3934 * @ctxt: the validation context
3935 * @doc: the document
3937 * @name: the attribute name
3938 * @value: the attribute value
3939 * @ctxt: the validation context or NULL
3941 * Does the validation related extra step of the normalization of attribute
3944 * If the declared value is not CDATA, then the XML processor must further
3945 * process the normalized attribute value by discarding any leading and
3946 * trailing space (#x20) characters, and by replacing sequences of space
3947 * (#x20) characters by single space (#x20) character.
3949 * Also check VC: Standalone Document Declaration in P32, and update
3950 * ctxt->valid accordingly
3952 * returns a new normalized string if normalization is needed, NULL otherwise
3953 * the caller must free the returned value.
3957 xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3958 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3961 xmlAttributePtr attrDecl = NULL;
3964 if (doc == NULL) return(NULL);
3965 if (elem == NULL) return(NULL);
3966 if (name == NULL) return(NULL);
3967 if (value == NULL) return(NULL);
3969 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3973 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3974 if (fullname == NULL)
3976 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
3977 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3978 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3979 if (attrDecl != NULL)
3982 if ((fullname != fn) && (fullname != elem->name))
3985 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3986 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3987 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3988 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3989 if (attrDecl != NULL)
3993 if (attrDecl == NULL)
3995 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3998 ret = xmlStrdup(value);
4003 while (*src == 0x20) src++;
4006 while (*src == 0x20) src++;
4014 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
4015 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
4016 "standalone: %s on %s value had to be normalized based on external subset declaration\n",
4017 name, elem->name, NULL);
4024 * xmlValidNormalizeAttributeValue:
4025 * @doc: the document
4027 * @name: the attribute name
4028 * @value: the attribute value
4030 * Does the validation related extra step of the normalization of attribute
4033 * If the declared value is not CDATA, then the XML processor must further
4034 * process the normalized attribute value by discarding any leading and
4035 * trailing space (#x20) characters, and by replacing sequences of space
4036 * (#x20) characters by single space (#x20) character.
4038 * Returns a new normalized string if normalization is needed, NULL otherwise
4039 * the caller must free the returned value.
4043 xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
4044 const xmlChar *name, const xmlChar *value) {
4047 xmlAttributePtr attrDecl = NULL;
4049 if (doc == NULL) return(NULL);
4050 if (elem == NULL) return(NULL);
4051 if (name == NULL) return(NULL);
4052 if (value == NULL) return(NULL);
4054 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4058 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4059 if (fullname == NULL)
4061 if ((fullname != fn) && (fullname != elem->name))
4064 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
4065 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4066 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
4068 if (attrDecl == NULL)
4070 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
4073 ret = xmlStrdup(value);
4078 while (*src == 0x20) src++;
4081 while (*src == 0x20) src++;
4093 xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
4094 const xmlChar* name ATTRIBUTE_UNUSED) {
4095 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
4099 * xmlValidateAttributeDecl:
4100 * @ctxt: the validation context
4101 * @doc: a document instance
4102 * @attr: an attribute definition
4104 * Try to validate a single attribute definition
4105 * basically it does the following checks as described by the
4106 * XML-1.0 recommendation:
4107 * - [ VC: Attribute Default Legal ]
4108 * - [ VC: Enumeration ]
4109 * - [ VC: ID Attribute Default ]
4111 * The ID/IDREF uniqueness and matching are done separately
4113 * returns 1 if valid or 0 otherwise
4117 xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4118 xmlAttributePtr attr) {
4122 if(attr == NULL) return(1);
4124 /* Attribute Default Legal */
4126 if (attr->defaultValue != NULL) {
4127 val = xmlValidateAttributeValueInternal(doc, attr->atype,
4128 attr->defaultValue);
4130 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
4131 "Syntax of default value for attribute %s of %s is not valid\n",
4132 attr->name, attr->elem, NULL);
4137 /* ID Attribute Default */
4138 if ((attr->atype == XML_ATTRIBUTE_ID)&&
4139 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
4140 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
4141 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
4142 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
4143 attr->name, attr->elem, NULL);
4147 /* One ID per Element Type */
4148 if (attr->atype == XML_ATTRIBUTE_ID) {
4151 /* the trick is that we parse DtD as their own internal subset */
4152 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
4155 nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
4157 xmlAttributeTablePtr table;
4160 * The attribute may be declared in the internal subset and the
4161 * element in the external subset.
4164 if (doc->intSubset != NULL) {
4165 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4166 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
4167 xmlValidateAttributeIdCallback, &nbId);
4172 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4173 "Element %s has %d ID attribute defined in the internal subset : %s\n",
4174 attr->elem, nbId, attr->name);
4175 } else if (doc->extSubset != NULL) {
4177 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
4179 extId = xmlScanIDAttributeDecl(NULL, elem, 0);
4182 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4183 "Element %s has %d ID attribute defined in the external subset : %s\n",
4184 attr->elem, extId, attr->name);
4185 } else if (extId + nbId > 1) {
4186 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4187 "Element %s has ID attributes defined in the internal and external subset : %s\n",
4188 attr->elem, attr->name, NULL);
4193 /* Validity Constraint: Enumeration */
4194 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4195 xmlEnumerationPtr tree = attr->tree;
4196 while (tree != NULL) {
4197 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4201 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
4202 "Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4203 attr->defaultValue, attr->name, attr->elem);
4212 * xmlValidateElementDecl:
4213 * @ctxt: the validation context
4214 * @doc: a document instance
4215 * @elem: an element definition
4217 * Try to validate a single element definition
4218 * basically it does the following checks as described by the
4219 * XML-1.0 recommendation:
4220 * - [ VC: One ID per Element Type ]
4221 * - [ VC: No Duplicate Types ]
4222 * - [ VC: Unique Element Type Declaration ]
4224 * returns 1 if valid or 0 otherwise
4228 xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4229 xmlElementPtr elem) {
4235 if (elem == NULL) return(1);
4238 #ifdef LIBXML_REGEXP_ENABLED
4239 /* Build the regexp associated to the content model */
4240 ret = xmlValidBuildContentModel(ctxt, elem);
4244 /* No Duplicate Types */
4245 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4246 xmlElementContentPtr cur, next;
4247 const xmlChar *name;
4249 cur = elem->content;
4250 while (cur != NULL) {
4251 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4252 if (cur->c1 == NULL) break;
4253 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4254 name = cur->c1->name;
4256 while (next != NULL) {
4257 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
4258 if ((xmlStrEqual(next->name, name)) &&
4259 (xmlStrEqual(next->prefix, cur->c1->prefix))) {
4260 if (cur->c1->prefix == NULL) {
4261 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4262 "Definition of %s has duplicate references of %s\n",
4263 elem->name, name, NULL);
4265 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4266 "Definition of %s has duplicate references of %s:%s\n",
4267 elem->name, cur->c1->prefix, name);
4273 if (next->c1 == NULL) break;
4274 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
4275 if ((xmlStrEqual(next->c1->name, name)) &&
4276 (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) {
4277 if (cur->c1->prefix == NULL) {
4278 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4279 "Definition of %s has duplicate references to %s\n",
4280 elem->name, name, NULL);
4282 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4283 "Definition of %s has duplicate references to %s:%s\n",
4284 elem->name, cur->c1->prefix, name);
4295 /* VC: Unique Element Type Declaration */
4296 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4297 if ((tst != NULL ) && (tst != elem) &&
4298 ((tst->prefix == elem->prefix) ||
4299 (xmlStrEqual(tst->prefix, elem->prefix))) &&
4300 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
4301 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4302 "Redefinition of element %s\n",
4303 elem->name, NULL, NULL);
4306 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4307 if ((tst != NULL ) && (tst != elem) &&
4308 ((tst->prefix == elem->prefix) ||
4309 (xmlStrEqual(tst->prefix, elem->prefix))) &&
4310 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
4311 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4312 "Redefinition of element %s\n",
4313 elem->name, NULL, NULL);
4316 /* One ID per Element Type
4317 * already done when registering the attribute
4318 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4325 * xmlValidateOneAttribute:
4326 * @ctxt: the validation context
4327 * @doc: a document instance
4328 * @elem: an element instance
4329 * @attr: an attribute instance
4330 * @value: the attribute value (without entities processing)
4332 * Try to validate a single attribute for an element
4333 * basically it does the following checks as described by the
4334 * XML-1.0 recommendation:
4335 * - [ VC: Attribute Value Type ]
4336 * - [ VC: Fixed Attribute Default ]
4337 * - [ VC: Entity Name ]
4338 * - [ VC: Name Token ]
4341 * - [ VC: Entity Name ]
4342 * - [ VC: Notation Attributes ]
4344 * The ID/IDREF uniqueness and matching are done separately
4346 * returns 1 if valid or 0 otherwise
4350 xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4351 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4353 xmlAttributePtr attrDecl = NULL;
4358 if ((elem == NULL) || (elem->name == NULL)) return(0);
4359 if ((attr == NULL) || (attr->name == NULL)) return(0);
4361 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4365 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4366 if (fullname == NULL)
4368 if (attr->ns != NULL) {
4369 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4370 attr->name, attr->ns->prefix);
4371 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4372 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4373 attr->name, attr->ns->prefix);
4375 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
4376 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4377 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4378 fullname, attr->name);
4380 if ((fullname != fn) && (fullname != elem->name))
4383 if (attrDecl == NULL) {
4384 if (attr->ns != NULL) {
4385 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4386 attr->name, attr->ns->prefix);
4387 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4388 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4389 attr->name, attr->ns->prefix);
4391 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4392 elem->name, attr->name);
4393 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4394 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4395 elem->name, attr->name);
4400 /* Validity Constraint: Attribute Value Type */
4401 if (attrDecl == NULL) {
4402 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4403 "No declaration for attribute %s of element %s\n",
4404 attr->name, elem->name, NULL);
4407 attr->atype = attrDecl->atype;
4409 val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4411 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4412 "Syntax of value for attribute %s of %s is not valid\n",
4413 attr->name, elem->name, NULL);
4417 /* Validity constraint: Fixed Attribute Default */
4418 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4419 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4420 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4421 "Value for attribute %s of %s is different from default \"%s\"\n",
4422 attr->name, elem->name, attrDecl->defaultValue);
4427 /* Validity Constraint: ID uniqueness */
4428 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4429 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4433 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4434 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4435 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4439 /* Validity Constraint: Notation Attributes */
4440 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4441 xmlEnumerationPtr tree = attrDecl->tree;
4442 xmlNotationPtr nota;
4444 /* First check that the given NOTATION was declared */
4445 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4447 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4450 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4451 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
4452 value, attr->name, elem->name);
4456 /* Second, verify that it's among the list */
4457 while (tree != NULL) {
4458 if (xmlStrEqual(tree->name, value)) break;
4462 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4463 "Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
4464 value, attr->name, elem->name);
4469 /* Validity Constraint: Enumeration */
4470 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4471 xmlEnumerationPtr tree = attrDecl->tree;
4472 while (tree != NULL) {
4473 if (xmlStrEqual(tree->name, value)) break;
4477 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4478 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4479 value, attr->name, elem->name);
4484 /* Fixed Attribute Default */
4485 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4486 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4487 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4488 "Value for attribute %s of %s must be \"%s\"\n",
4489 attr->name, elem->name, attrDecl->defaultValue);
4493 /* Extra check for the attribute value */
4494 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4495 attrDecl->atype, value);
4501 * xmlValidateOneNamespace:
4502 * @ctxt: the validation context
4503 * @doc: a document instance
4504 * @elem: an element instance
4505 * @prefix: the namespace prefix
4506 * @ns: an namespace declaration instance
4507 * @value: the attribute value (without entities processing)
4509 * Try to validate a single namespace declaration for an element
4510 * basically it does the following checks as described by the
4511 * XML-1.0 recommendation:
4512 * - [ VC: Attribute Value Type ]
4513 * - [ VC: Fixed Attribute Default ]
4514 * - [ VC: Entity Name ]
4515 * - [ VC: Name Token ]
4518 * - [ VC: Entity Name ]
4519 * - [ VC: Notation Attributes ]
4521 * The ID/IDREF uniqueness and matching are done separately
4523 * returns 1 if valid or 0 otherwise
4527 xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4528 xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4529 /* xmlElementPtr elemDecl; */
4530 xmlAttributePtr attrDecl = NULL;
4535 if ((elem == NULL) || (elem->name == NULL)) return(0);
4536 if ((ns == NULL) || (ns->href == NULL)) return(0);
4538 if (prefix != NULL) {
4542 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4543 if (fullname == NULL) {
4544 xmlVErrMemory(ctxt, "Validating namespace");
4547 if (ns->prefix != NULL) {
4548 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4549 ns->prefix, BAD_CAST "xmlns");
4550 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4551 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4552 ns->prefix, BAD_CAST "xmlns");
4554 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
4556 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4557 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
4560 if ((fullname != fn) && (fullname != elem->name))
4563 if (attrDecl == NULL) {
4564 if (ns->prefix != NULL) {
4565 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4566 ns->prefix, BAD_CAST "xmlns");
4567 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4568 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4569 ns->prefix, BAD_CAST "xmlns");
4571 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4572 elem->name, BAD_CAST "xmlns");
4573 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4574 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4575 elem->name, BAD_CAST "xmlns");
4580 /* Validity Constraint: Attribute Value Type */
4581 if (attrDecl == NULL) {
4582 if (ns->prefix != NULL) {
4583 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4584 "No declaration for attribute xmlns:%s of element %s\n",
4585 ns->prefix, elem->name, NULL);
4587 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4588 "No declaration for attribute xmlns of element %s\n",
4589 elem->name, NULL, NULL);
4594 val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4596 if (ns->prefix != NULL) {
4597 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4598 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4599 ns->prefix, elem->name, NULL);
4601 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4602 "Syntax of value for attribute xmlns of %s is not valid\n",
4603 elem->name, NULL, NULL);
4608 /* Validity constraint: Fixed Attribute Default */
4609 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4610 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4611 if (ns->prefix != NULL) {
4612 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4613 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4614 ns->prefix, elem->name, attrDecl->defaultValue);
4616 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4617 "Value for attribute xmlns of %s is different from default \"%s\"\n",
4618 elem->name, attrDecl->defaultValue, NULL);
4624 /* Validity Constraint: ID uniqueness */
4625 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4626 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4630 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4631 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4632 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4636 /* Validity Constraint: Notation Attributes */
4637 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4638 xmlEnumerationPtr tree = attrDecl->tree;
4639 xmlNotationPtr nota;
4641 /* First check that the given NOTATION was declared */
4642 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4644 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4647 if (ns->prefix != NULL) {
4648 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4649 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4650 value, ns->prefix, elem->name);
4652 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4653 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4654 value, elem->name, NULL);
4659 /* Second, verify that it's among the list */
4660 while (tree != NULL) {
4661 if (xmlStrEqual(tree->name, value)) break;
4665 if (ns->prefix != NULL) {
4666 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4667 "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4668 value, ns->prefix, elem->name);
4670 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4671 "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4672 value, elem->name, NULL);
4678 /* Validity Constraint: Enumeration */
4679 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4680 xmlEnumerationPtr tree = attrDecl->tree;
4681 while (tree != NULL) {
4682 if (xmlStrEqual(tree->name, value)) break;
4686 if (ns->prefix != NULL) {
4687 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4688 "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4689 value, ns->prefix, elem->name);
4691 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4692 "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4693 value, elem->name, NULL);
4699 /* Fixed Attribute Default */
4700 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4701 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4702 if (ns->prefix != NULL) {
4703 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4704 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4705 ns->prefix, elem->name, attrDecl->defaultValue);
4707 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4708 "Value for attribute xmlns of %s must be \"%s\"\n",
4709 elem->name, attrDecl->defaultValue, NULL);
4714 /* Extra check for the attribute value */
4715 if (ns->prefix != NULL) {
4716 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4717 attrDecl->atype, value);
4719 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4720 attrDecl->atype, value);
4726 #ifndef LIBXML_REGEXP_ENABLED
4728 * xmlValidateSkipIgnorable:
4729 * @ctxt: the validation context
4730 * @child: the child list
4732 * Skip ignorable elements w.r.t. the validation process
4734 * returns the first element to consider for validation of the content model
4738 xmlValidateSkipIgnorable(xmlNodePtr child) {
4739 while (child != NULL) {
4740 switch (child->type) {
4741 /* These things are ignored (skipped) during validation. */
4743 case XML_COMMENT_NODE:
4744 case XML_XINCLUDE_START:
4745 case XML_XINCLUDE_END:
4746 child = child->next;
4749 if (xmlIsBlankNode(child))
4750 child = child->next;
4754 /* keep current node */
4763 * xmlValidateElementType:
4764 * @ctxt: the validation context
4766 * Try to validate the content model of an element internal function
4768 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4769 * reference is found and -3 if the validation succeeded but
4770 * the content model is not determinist.
4774 xmlValidateElementType(xmlValidCtxtPtr ctxt) {
4776 int determinist = 1;
4778 NODE = xmlValidateSkipIgnorable(NODE);
4779 if ((NODE == NULL) && (CONT == NULL))
4781 if ((NODE == NULL) &&
4782 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4783 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4786 if (CONT == NULL) return(-1);
4787 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
4791 * We arrive here when more states need to be examined
4796 * We just recovered from a rollback generated by a possible
4797 * epsilon transition, go directly to the analysis phase
4799 if (STATE == ROLLBACK_PARENT) {
4800 DEBUG_VALID_MSG("restored parent branch");
4801 DEBUG_VALID_STATE(NODE, CONT)
4806 DEBUG_VALID_STATE(NODE, CONT)
4808 * we may have to save a backup state here. This is the equivalent
4809 * of handling epsilon transition in NFAs.
4811 if ((CONT != NULL) &&
4812 ((CONT->parent == NULL) ||
4813 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
4814 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4815 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
4816 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
4817 DEBUG_VALID_MSG("saving parent branch");
4818 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4824 * Check first if the content matches
4826 switch (CONT->type) {
4827 case XML_ELEMENT_CONTENT_PCDATA:
4829 DEBUG_VALID_MSG("pcdata failed no node");
4833 if (NODE->type == XML_TEXT_NODE) {
4834 DEBUG_VALID_MSG("pcdata found, skip to next");
4836 * go to next element in the content model
4837 * skipping ignorable elems
4841 NODE = xmlValidateSkipIgnorable(NODE);
4842 if ((NODE != NULL) &&
4843 (NODE->type == XML_ENTITY_REF_NODE))
4845 } while ((NODE != NULL) &&
4846 ((NODE->type != XML_ELEMENT_NODE) &&
4847 (NODE->type != XML_TEXT_NODE) &&
4848 (NODE->type != XML_CDATA_SECTION_NODE)));
4852 DEBUG_VALID_MSG("pcdata failed");
4857 case XML_ELEMENT_CONTENT_ELEMENT:
4859 DEBUG_VALID_MSG("element failed no node");
4863 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4864 (xmlStrEqual(NODE->name, CONT->name)));
4866 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4867 ret = (CONT->prefix == NULL);
4868 } else if (CONT->prefix == NULL) {
4871 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4875 DEBUG_VALID_MSG("element found, skip to next");
4877 * go to next element in the content model
4878 * skipping ignorable elems
4882 NODE = xmlValidateSkipIgnorable(NODE);
4883 if ((NODE != NULL) &&
4884 (NODE->type == XML_ENTITY_REF_NODE))
4886 } while ((NODE != NULL) &&
4887 ((NODE->type != XML_ELEMENT_NODE) &&
4888 (NODE->type != XML_TEXT_NODE) &&
4889 (NODE->type != XML_CDATA_SECTION_NODE)));
4891 DEBUG_VALID_MSG("element failed");
4896 case XML_ELEMENT_CONTENT_OR:
4898 * Small optimization.
4900 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4901 if ((NODE == NULL) ||
4902 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4907 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4908 ret = (CONT->c1->prefix == NULL);
4909 } else if (CONT->c1->prefix == NULL) {
4912 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4922 * save the second branch 'or' branch
4924 DEBUG_VALID_MSG("saving 'or' branch");
4925 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4926 OCCURS, ROLLBACK_OR) < 0)
4931 case XML_ELEMENT_CONTENT_SEQ:
4933 * Small optimization.
4935 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4936 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4937 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4938 if ((NODE == NULL) ||
4939 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4944 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4945 ret = (CONT->c1->prefix == NULL);
4946 } else if (CONT->c1->prefix == NULL) {
4949 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4963 * At this point handle going up in the tree
4966 DEBUG_VALID_MSG("error found returning");
4970 while (CONT != NULL) {
4972 * First do the analysis depending on the occurrence model at
4976 switch (CONT->ocur) {
4979 case XML_ELEMENT_CONTENT_ONCE:
4980 cur = ctxt->vstate->node;
4981 DEBUG_VALID_MSG("Once branch failed, rollback");
4982 if (vstateVPop(ctxt) < 0 ) {
4983 DEBUG_VALID_MSG("exhaustion, failed");
4986 if (cur != ctxt->vstate->node)
4989 case XML_ELEMENT_CONTENT_PLUS:
4990 if (OCCURRENCE == 0) {
4991 cur = ctxt->vstate->node;
4992 DEBUG_VALID_MSG("Plus branch failed, rollback");
4993 if (vstateVPop(ctxt) < 0 ) {
4994 DEBUG_VALID_MSG("exhaustion, failed");
4997 if (cur != ctxt->vstate->node)
5001 DEBUG_VALID_MSG("Plus branch found");
5004 case XML_ELEMENT_CONTENT_MULT:
5005 #ifdef DEBUG_VALID_ALGO
5006 if (OCCURRENCE == 0) {
5007 DEBUG_VALID_MSG("Mult branch failed");
5009 DEBUG_VALID_MSG("Mult branch found");
5014 case XML_ELEMENT_CONTENT_OPT:
5015 DEBUG_VALID_MSG("Option branch failed");
5020 switch (CONT->ocur) {
5021 case XML_ELEMENT_CONTENT_OPT:
5022 DEBUG_VALID_MSG("Option branch succeeded");
5025 case XML_ELEMENT_CONTENT_ONCE:
5026 DEBUG_VALID_MSG("Once branch succeeded");
5029 case XML_ELEMENT_CONTENT_PLUS:
5030 if (STATE == ROLLBACK_PARENT) {
5031 DEBUG_VALID_MSG("Plus branch rollback");
5036 DEBUG_VALID_MSG("Plus branch exhausted");
5040 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
5043 case XML_ELEMENT_CONTENT_MULT:
5044 if (STATE == ROLLBACK_PARENT) {
5045 DEBUG_VALID_MSG("Mult branch rollback");
5050 DEBUG_VALID_MSG("Mult branch exhausted");
5054 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
5055 /* SET_OCCURRENCE; */
5062 * Then act accordingly at the parent level
5065 if (CONT->parent == NULL)
5068 switch (CONT->parent->type) {
5069 case XML_ELEMENT_CONTENT_PCDATA:
5070 DEBUG_VALID_MSG("Error: parent pcdata");
5072 case XML_ELEMENT_CONTENT_ELEMENT:
5073 DEBUG_VALID_MSG("Error: parent element");
5075 case XML_ELEMENT_CONTENT_OR:
5077 DEBUG_VALID_MSG("Or succeeded");
5078 CONT = CONT->parent;
5081 DEBUG_VALID_MSG("Or failed");
5082 CONT = CONT->parent;
5086 case XML_ELEMENT_CONTENT_SEQ:
5088 DEBUG_VALID_MSG("Sequence failed");
5089 CONT = CONT->parent;
5091 } else if (CONT == CONT->parent->c1) {
5092 DEBUG_VALID_MSG("Sequence testing 2nd branch");
5093 CONT = CONT->parent->c2;
5096 DEBUG_VALID_MSG("Sequence succeeded");
5097 CONT = CONT->parent;
5105 cur = ctxt->vstate->node;
5106 DEBUG_VALID_MSG("Failed, remaining input, rollback");
5107 if (vstateVPop(ctxt) < 0 ) {
5108 DEBUG_VALID_MSG("exhaustion, failed");
5111 if (cur != ctxt->vstate->node)
5118 cur = ctxt->vstate->node;
5119 DEBUG_VALID_MSG("Failure, rollback");
5120 if (vstateVPop(ctxt) < 0 ) {
5121 DEBUG_VALID_MSG("exhaustion, failed");
5124 if (cur != ctxt->vstate->node)
5128 return(determinist);
5133 * xmlSnprintfElements:
5134 * @buf: an output buffer
5135 * @size: the size of the buffer
5136 * @content: An element
5137 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
5139 * This will dump the list of elements to the buffer
5140 * Intended just for the debug routine
5143 xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
5147 if (node == NULL) return;
5148 if (glob) strcat(buf, "(");
5150 while (cur != NULL) {
5152 if (size - len < 50) {
5153 if ((size - len > 4) && (buf[len - 1] != '.'))
5154 strcat(buf, " ...");
5157 switch (cur->type) {
5158 case XML_ELEMENT_NODE:
5159 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5160 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
5161 if ((size - len > 4) && (buf[len - 1] != '.'))
5162 strcat(buf, " ...");
5165 strcat(buf, (char *) cur->ns->prefix);
5168 if (size - len < xmlStrlen(cur->name) + 10) {
5169 if ((size - len > 4) && (buf[len - 1] != '.'))
5170 strcat(buf, " ...");
5173 strcat(buf, (char *) cur->name);
5174 if (cur->next != NULL)
5178 if (xmlIsBlankNode(cur))
5180 case XML_CDATA_SECTION_NODE:
5181 case XML_ENTITY_REF_NODE:
5182 strcat(buf, "CDATA");
5183 if (cur->next != NULL)
5186 case XML_ATTRIBUTE_NODE:
5187 case XML_DOCUMENT_NODE:
5188 #ifdef LIBXML_DOCB_ENABLED
5189 case XML_DOCB_DOCUMENT_NODE:
5191 case XML_HTML_DOCUMENT_NODE:
5192 case XML_DOCUMENT_TYPE_NODE:
5193 case XML_DOCUMENT_FRAG_NODE:
5194 case XML_NOTATION_NODE:
5195 case XML_NAMESPACE_DECL:
5197 if (cur->next != NULL)
5200 case XML_ENTITY_NODE:
5203 case XML_COMMENT_NODE:
5204 case XML_ELEMENT_DECL:
5205 case XML_ATTRIBUTE_DECL:
5206 case XML_ENTITY_DECL:
5207 case XML_XINCLUDE_START:
5208 case XML_XINCLUDE_END:
5213 if (glob) strcat(buf, ")");
5217 * xmlValidateElementContent:
5218 * @ctxt: the validation context
5219 * @child: the child list
5220 * @elemDecl: pointer to the element declaration
5221 * @warn: emit the error message
5222 * @parent: the parent element (for error reporting)
5224 * Try to validate the content model of an element
5226 * returns 1 if valid or 0 if not and -1 in case of error
5230 xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
5231 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
5233 #ifndef LIBXML_REGEXP_ENABLED
5234 xmlNodePtr repl = NULL, last = NULL, tmp;
5237 xmlElementContentPtr cont;
5238 const xmlChar *name;
5240 if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL))
5242 cont = elemDecl->content;
5243 name = elemDecl->name;
5245 #ifdef LIBXML_REGEXP_ENABLED
5246 /* Build the regexp associated to the content model */
5247 if (elemDecl->contModel == NULL)
5248 ret = xmlValidBuildContentModel(ctxt, elemDecl);
5249 if (elemDecl->contModel == NULL) {
5252 xmlRegExecCtxtPtr exec;
5254 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5259 ctxt->nodeTab = NULL;
5260 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5263 while (cur != NULL) {
5264 switch (cur->type) {
5265 case XML_ENTITY_REF_NODE:
5267 * Push the current node to be able to roll back
5268 * and process within the entity
5270 if ((cur->children != NULL) &&
5271 (cur->children->children != NULL)) {
5272 nodeVPush(ctxt, cur);
5273 cur = cur->children->children;
5278 if (xmlIsBlankNode(cur))
5282 case XML_CDATA_SECTION_NODE:
5286 case XML_ELEMENT_NODE:
5287 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5291 fullname = xmlBuildQName(cur->name,
5292 cur->ns->prefix, fn, 50);
5293 if (fullname == NULL) {
5297 ret = xmlRegExecPushString(exec, fullname, NULL);
5298 if ((fullname != fn) && (fullname != cur->name))
5301 ret = xmlRegExecPushString(exec, cur->name, NULL);
5308 * Switch to next element
5311 while (cur == NULL) {
5312 cur = nodeVPop(ctxt);
5318 ret = xmlRegExecPushString(exec, NULL, NULL);
5320 xmlRegFreeExecCtxt(exec);
5323 #else /* LIBXML_REGEXP_ENABLED */
5325 * Allocate the stack
5327 ctxt->vstateMax = 8;
5328 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5329 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5330 if (ctxt->vstateTab == NULL) {
5331 xmlVErrMemory(ctxt, "malloc failed");
5335 * The first entry in the stack is reserved to the current state
5339 ctxt->nodeTab = NULL;
5340 ctxt->vstate = &ctxt->vstateTab[0];
5347 ret = xmlValidateElementType(ctxt);
5348 if ((ret == -3) && (warn)) {
5349 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5350 "Content model for Element %s is ambiguous\n",
5352 } else if (ret == -2) {
5354 * An entities reference appeared at this level.
5355 * Buid a minimal representation of this node content
5356 * sufficient to run the validation process on it
5358 DEBUG_VALID_MSG("Found an entity reference, linearizing");
5360 while (cur != NULL) {
5361 switch (cur->type) {
5362 case XML_ENTITY_REF_NODE:
5364 * Push the current node to be able to roll back
5365 * and process within the entity
5367 if ((cur->children != NULL) &&
5368 (cur->children->children != NULL)) {
5369 nodeVPush(ctxt, cur);
5370 cur = cur->children->children;
5375 if (xmlIsBlankNode(cur))
5377 /* no break on purpose */
5378 case XML_CDATA_SECTION_NODE:
5379 /* no break on purpose */
5380 case XML_ELEMENT_NODE:
5382 * Allocate a new node and minimally fills in
5385 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5387 xmlVErrMemory(ctxt, "malloc failed");
5388 xmlFreeNodeList(repl);
5392 tmp->type = cur->type;
5393 tmp->name = cur->name;
5396 tmp->content = NULL;
5403 if (cur->type == XML_CDATA_SECTION_NODE) {
5405 * E59 spaces in CDATA does not match the
5408 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5415 * Switch to next element
5418 while (cur == NULL) {
5419 cur = nodeVPop(ctxt);
5427 * Relaunch the validation
5429 ctxt->vstate = &ctxt->vstateTab[0];
5436 ret = xmlValidateElementType(ctxt);
5438 #endif /* LIBXML_REGEXP_ENABLED */
5439 if ((warn) && ((ret != 1) && (ret != -3))) {
5445 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
5447 #ifndef LIBXML_REGEXP_ENABLED
5449 xmlSnprintfElements(&list[0], 5000, repl, 1);
5451 #endif /* LIBXML_REGEXP_ENABLED */
5452 xmlSnprintfElements(&list[0], 5000, child, 1);
5455 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5456 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5457 name, BAD_CAST expr, BAD_CAST list);
5459 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5460 "Element content does not follow the DTD, expecting %s, got %s\n",
5461 BAD_CAST expr, BAD_CAST list, NULL);
5465 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5466 "Element %s content does not follow the DTD\n",
5469 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5470 "Element content does not follow the DTD\n",
5479 #ifndef LIBXML_REGEXP_ENABLED
5482 * Deallocate the copy if done, and free up the validation stack
5484 while (repl != NULL) {
5489 ctxt->vstateMax = 0;
5490 if (ctxt->vstateTab != NULL) {
5491 xmlFree(ctxt->vstateTab);
5492 ctxt->vstateTab = NULL;
5497 if (ctxt->nodeTab != NULL) {
5498 xmlFree(ctxt->nodeTab);
5499 ctxt->nodeTab = NULL;
5506 * xmlValidateCdataElement:
5507 * @ctxt: the validation context
5508 * @doc: a document instance
5509 * @elem: an element instance
5511 * Check that an element follows #CDATA
5513 * returns 1 if valid or 0 otherwise
5516 xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5519 xmlNodePtr cur, child;
5521 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) ||
5522 (elem->type != XML_ELEMENT_NODE))
5525 child = elem->children;
5528 while (cur != NULL) {
5529 switch (cur->type) {
5530 case XML_ENTITY_REF_NODE:
5532 * Push the current node to be able to roll back
5533 * and process within the entity
5535 if ((cur->children != NULL) &&
5536 (cur->children->children != NULL)) {
5537 nodeVPush(ctxt, cur);
5538 cur = cur->children->children;
5542 case XML_COMMENT_NODE:
5545 case XML_CDATA_SECTION_NODE:
5552 * Switch to next element
5555 while (cur == NULL) {
5556 cur = nodeVPop(ctxt);
5565 if (ctxt->nodeTab != NULL) {
5566 xmlFree(ctxt->nodeTab);
5567 ctxt->nodeTab = NULL;
5573 * xmlValidateCheckMixed:
5574 * @ctxt: the validation context
5575 * @cont: the mixed content model
5576 * @qname: the qualified name as appearing in the serialization
5578 * Check if the given node is part of the content model.
5580 * Returns 1 if yes, 0 if no, -1 in case of error
5583 xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
5584 xmlElementContentPtr cont, const xmlChar *qname) {
5585 const xmlChar *name;
5587 name = xmlSplitQName3(qname, &plen);
5590 while (cont != NULL) {
5591 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5592 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5594 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5595 (cont->c1 != NULL) &&
5596 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5597 if ((cont->c1->prefix == NULL) &&
5598 (xmlStrEqual(cont->c1->name, qname)))
5600 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5601 (cont->c1 == NULL) ||
5602 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5603 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5604 "Internal: MIXED struct corrupted\n",
5611 while (cont != NULL) {
5612 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5613 if ((cont->prefix != NULL) &&
5614 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5615 (xmlStrEqual(cont->name, name)))
5617 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5618 (cont->c1 != NULL) &&
5619 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5620 if ((cont->c1->prefix != NULL) &&
5621 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5622 (xmlStrEqual(cont->c1->name, name)))
5624 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5625 (cont->c1 == NULL) ||
5626 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5627 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5628 "Internal: MIXED struct corrupted\n",
5639 * xmlValidGetElemDecl:
5640 * @ctxt: the validation context
5641 * @doc: a document instance
5642 * @elem: an element instance
5643 * @extsubset: pointer, (out) indicate if the declaration was found
5644 * in the external subset.
5646 * Finds a declaration associated to an element in the document.
5648 * returns the pointer to the declaration or NULL if not found.
5650 static xmlElementPtr
5651 xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5652 xmlNodePtr elem, int *extsubset) {
5653 xmlElementPtr elemDecl = NULL;
5654 const xmlChar *prefix = NULL;
5656 if ((ctxt == NULL) || (doc == NULL) ||
5657 (elem == NULL) || (elem->name == NULL))
5659 if (extsubset != NULL)
5663 * Fetch the declaration for the qualified name
5665 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5666 prefix = elem->ns->prefix;
5668 if (prefix != NULL) {
5669 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5670 elem->name, prefix);
5671 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5672 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5673 elem->name, prefix);
5674 if ((elemDecl != NULL) && (extsubset != NULL))
5680 * Fetch the declaration for the non qualified name
5681 * This is "non-strict" validation should be done on the
5682 * full QName but in that case being flexible makes sense.
5684 if (elemDecl == NULL) {
5685 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5686 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5687 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5688 if ((elemDecl != NULL) && (extsubset != NULL))
5692 if (elemDecl == NULL) {
5693 xmlErrValidNode(ctxt, elem,
5694 XML_DTD_UNKNOWN_ELEM,
5695 "No declaration for element %s\n",
5696 elem->name, NULL, NULL);
5701 #ifdef LIBXML_REGEXP_ENABLED
5703 * xmlValidatePushElement:
5704 * @ctxt: the validation context
5705 * @doc: a document instance
5706 * @elem: an element instance
5707 * @qname: the qualified name as appearing in the serialization
5709 * Push a new element start on the validation stack.
5711 * returns 1 if no validation problem was found or 0 otherwise
5714 xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5715 xmlNodePtr elem, const xmlChar *qname) {
5717 xmlElementPtr eDecl;
5722 /* printf("PushElem %s\n", qname); */
5723 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5724 xmlValidStatePtr state = ctxt->vstate;
5725 xmlElementPtr elemDecl;
5728 * Check the new element agaisnt the content model of the new elem.
5730 if (state->elemDecl != NULL) {
5731 elemDecl = state->elemDecl;
5733 switch(elemDecl->etype) {
5734 case XML_ELEMENT_TYPE_UNDEFINED:
5737 case XML_ELEMENT_TYPE_EMPTY:
5738 xmlErrValidNode(ctxt, state->node,
5740 "Element %s was declared EMPTY this one has content\n",
5741 state->node->name, NULL, NULL);
5744 case XML_ELEMENT_TYPE_ANY:
5745 /* I don't think anything is required then */
5747 case XML_ELEMENT_TYPE_MIXED:
5748 /* simple case of declared as #PCDATA */
5749 if ((elemDecl->content != NULL) &&
5750 (elemDecl->content->type ==
5751 XML_ELEMENT_CONTENT_PCDATA)) {
5752 xmlErrValidNode(ctxt, state->node,
5754 "Element %s was declared #PCDATA but contains non text nodes\n",
5755 state->node->name, NULL, NULL);
5758 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5761 xmlErrValidNode(ctxt, state->node,
5762 XML_DTD_INVALID_CHILD,
5763 "Element %s is not declared in %s list of possible children\n",
5764 qname, state->node->name, NULL);
5768 case XML_ELEMENT_TYPE_ELEMENT:
5771 * VC: Standalone Document Declaration
5772 * - element types with element content, if white space
5773 * occurs directly within any instance of those types.
5775 if (state->exec != NULL) {
5776 ret = xmlRegExecPushString(state->exec, qname, NULL);
5778 xmlErrValidNode(ctxt, state->node,
5779 XML_DTD_CONTENT_MODEL,
5780 "Element %s content does not follow the DTD, Misplaced %s\n",
5781 state->node->name, qname, NULL);
5791 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5792 vstateVPush(ctxt, eDecl, elem);
5797 * xmlValidatePushCData:
5798 * @ctxt: the validation context
5799 * @data: some character data read
5800 * @len: the length of the data
5802 * check the CData parsed for validation in the current stack
5804 * returns 1 if no validation problem was found or 0 otherwise
5807 xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5810 /* printf("CDATA %s %d\n", data, len); */
5815 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5816 xmlValidStatePtr state = ctxt->vstate;
5817 xmlElementPtr elemDecl;
5820 * Check the new element agaisnt the content model of the new elem.
5822 if (state->elemDecl != NULL) {
5823 elemDecl = state->elemDecl;
5825 switch(elemDecl->etype) {
5826 case XML_ELEMENT_TYPE_UNDEFINED:
5829 case XML_ELEMENT_TYPE_EMPTY:
5830 xmlErrValidNode(ctxt, state->node,
5832 "Element %s was declared EMPTY this one has content\n",
5833 state->node->name, NULL, NULL);
5836 case XML_ELEMENT_TYPE_ANY:
5838 case XML_ELEMENT_TYPE_MIXED:
5840 case XML_ELEMENT_TYPE_ELEMENT:
5844 for (i = 0;i < len;i++) {
5845 if (!IS_BLANK_CH(data[i])) {
5846 xmlErrValidNode(ctxt, state->node,
5847 XML_DTD_CONTENT_MODEL,
5848 "Element %s content does not follow the DTD, Text not allowed\n",
5849 state->node->name, NULL, NULL);
5856 * VC: Standalone Document Declaration
5857 * element types with element content, if white space
5858 * occurs directly within any instance of those types.
5870 * xmlValidatePopElement:
5871 * @ctxt: the validation context
5872 * @doc: a document instance
5873 * @elem: an element instance
5874 * @qname: the qualified name as appearing in the serialization
5876 * Pop the element end from the validation stack.
5878 * returns 1 if no validation problem was found or 0 otherwise
5881 xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
5882 xmlNodePtr elem ATTRIBUTE_UNUSED,
5883 const xmlChar *qname ATTRIBUTE_UNUSED) {
5888 /* printf("PopElem %s\n", qname); */
5889 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5890 xmlValidStatePtr state = ctxt->vstate;
5891 xmlElementPtr elemDecl;
5894 * Check the new element agaisnt the content model of the new elem.
5896 if (state->elemDecl != NULL) {
5897 elemDecl = state->elemDecl;
5899 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5900 if (state->exec != NULL) {
5901 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5903 xmlErrValidNode(ctxt, state->node,
5904 XML_DTD_CONTENT_MODEL,
5905 "Element %s content does not follow the DTD, Expecting more child\n",
5906 state->node->name, NULL,NULL);
5909 * previous validation errors should not generate
5921 #endif /* LIBXML_REGEXP_ENABLED */
5924 * xmlValidateOneElement:
5925 * @ctxt: the validation context
5926 * @doc: a document instance
5927 * @elem: an element instance
5929 * Try to validate a single element and it's attributes,
5930 * basically it does the following checks as described by the
5931 * XML-1.0 recommendation:
5932 * - [ VC: Element Valid ]
5933 * - [ VC: Required Attribute ]
5934 * Then call xmlValidateOneAttribute() for each attribute present.
5936 * The ID/IDREF checkings are done separately
5938 * returns 1 if valid or 0 otherwise
5942 xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5944 xmlElementPtr elemDecl = NULL;
5945 xmlElementContentPtr cont;
5946 xmlAttributePtr attr;
5949 const xmlChar *name;
5954 if (elem == NULL) return(0);
5955 switch (elem->type) {
5956 case XML_ATTRIBUTE_NODE:
5957 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5958 "Attribute element not expected\n", NULL, NULL ,NULL);
5961 if (elem->children != NULL) {
5962 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5963 "Text element has children !\n",
5967 if (elem->ns != NULL) {
5968 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5969 "Text element has namespace !\n",
5973 if (elem->content == NULL) {
5974 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5975 "Text element has no content !\n",
5980 case XML_XINCLUDE_START:
5981 case XML_XINCLUDE_END:
5983 case XML_CDATA_SECTION_NODE:
5984 case XML_ENTITY_REF_NODE:
5986 case XML_COMMENT_NODE:
5988 case XML_ENTITY_NODE:
5989 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5990 "Entity element not expected\n", NULL, NULL ,NULL);
5992 case XML_NOTATION_NODE:
5993 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5994 "Notation element not expected\n", NULL, NULL ,NULL);
5996 case XML_DOCUMENT_NODE:
5997 case XML_DOCUMENT_TYPE_NODE:
5998 case XML_DOCUMENT_FRAG_NODE:
5999 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6000 "Document element not expected\n", NULL, NULL ,NULL);
6002 case XML_HTML_DOCUMENT_NODE:
6003 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6004 "HTML Document not expected\n", NULL, NULL ,NULL);
6006 case XML_ELEMENT_NODE:
6009 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6010 "unknown element type\n", NULL, NULL ,NULL);
6015 * Fetch the declaration
6017 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
6018 if (elemDecl == NULL)
6022 * If vstateNr is not zero that means continuous validation is
6023 * activated, do not try to check the content model at that level.
6025 if (ctxt->vstateNr == 0) {
6026 /* Check that the element content matches the definition */
6027 switch (elemDecl->etype) {
6028 case XML_ELEMENT_TYPE_UNDEFINED:
6029 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
6030 "No declaration for element %s\n",
6031 elem->name, NULL, NULL);
6033 case XML_ELEMENT_TYPE_EMPTY:
6034 if (elem->children != NULL) {
6035 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
6036 "Element %s was declared EMPTY this one has content\n",
6037 elem->name, NULL, NULL);
6041 case XML_ELEMENT_TYPE_ANY:
6042 /* I don't think anything is required then */
6044 case XML_ELEMENT_TYPE_MIXED:
6046 /* simple case of declared as #PCDATA */
6047 if ((elemDecl->content != NULL) &&
6048 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
6049 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
6051 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
6052 "Element %s was declared #PCDATA but contains non text nodes\n",
6053 elem->name, NULL, NULL);
6057 child = elem->children;
6058 /* Hum, this start to get messy */
6059 while (child != NULL) {
6060 if (child->type == XML_ELEMENT_NODE) {
6062 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
6066 fullname = xmlBuildQName(child->name, child->ns->prefix,
6068 if (fullname == NULL)
6070 cont = elemDecl->content;
6071 while (cont != NULL) {
6072 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
6073 if (xmlStrEqual(cont->name, fullname))
6075 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6076 (cont->c1 != NULL) &&
6077 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
6078 if (xmlStrEqual(cont->c1->name, fullname))
6080 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6081 (cont->c1 == NULL) ||
6082 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
6083 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
6084 "Internal: MIXED struct corrupted\n",
6090 if ((fullname != fn) && (fullname != child->name))
6095 cont = elemDecl->content;
6096 while (cont != NULL) {
6097 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
6098 if (xmlStrEqual(cont->name, name)) break;
6099 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6100 (cont->c1 != NULL) &&
6101 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
6102 if (xmlStrEqual(cont->c1->name, name)) break;
6103 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6104 (cont->c1 == NULL) ||
6105 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
6106 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
6107 "Internal: MIXED struct corrupted\n",
6114 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
6115 "Element %s is not declared in %s list of possible children\n",
6116 name, elem->name, NULL);
6121 child = child->next;
6124 case XML_ELEMENT_TYPE_ELEMENT:
6125 if ((doc->standalone == 1) && (extsubset == 1)) {
6127 * VC: Standalone Document Declaration
6128 * - element types with element content, if white space
6129 * occurs directly within any instance of those types.
6131 child = elem->children;
6132 while (child != NULL) {
6133 if (child->type == XML_TEXT_NODE) {
6134 const xmlChar *content = child->content;
6136 while (IS_BLANK_CH(*content))
6138 if (*content == 0) {
6139 xmlErrValidNode(ctxt, elem,
6140 XML_DTD_STANDALONE_WHITE_SPACE,
6141 "standalone: %s declared in the external subset contains white spaces nodes\n",
6142 elem->name, NULL, NULL);
6150 child = elem->children;
6151 cont = elemDecl->content;
6152 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
6157 } /* not continuous */
6159 /* [ VC: Required Attribute ] */
6160 attr = elemDecl->attributes;
6161 while (attr != NULL) {
6162 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
6165 if ((attr->prefix == NULL) &&
6166 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6170 while (ns != NULL) {
6171 if (ns->prefix == NULL)
6175 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6179 while (ns != NULL) {
6180 if (xmlStrEqual(attr->name, ns->prefix))
6187 attrib = elem->properties;
6188 while (attrib != NULL) {
6189 if (xmlStrEqual(attrib->name, attr->name)) {
6190 if (attr->prefix != NULL) {
6191 xmlNsPtr nameSpace = attrib->ns;
6193 if (nameSpace == NULL)
6194 nameSpace = elem->ns;
6196 * qualified names handling is problematic, having a
6197 * different prefix should be possible but DTDs don't
6198 * allow to define the URI instead of the prefix :-(
6200 if (nameSpace == NULL) {
6203 } else if (!xmlStrEqual(nameSpace->prefix,
6211 * We should allow applications to define namespaces
6212 * for their application even if the DTD doesn't
6213 * carry one, otherwise, basically we would always
6219 attrib = attrib->next;
6222 if (qualified == -1) {
6223 if (attr->prefix == NULL) {
6224 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
6225 "Element %s does not carry attribute %s\n",
6226 elem->name, attr->name, NULL);
6229 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
6230 "Element %s does not carry attribute %s:%s\n",
6231 elem->name, attr->prefix,attr->name);
6234 } else if (qualified == 0) {
6235 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
6236 "Element %s required attribute %s:%s has no prefix\n",
6237 elem->name, attr->prefix, attr->name);
6238 } else if (qualified == 1) {
6239 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
6240 "Element %s required attribute %s:%s has different prefix\n",
6241 elem->name, attr->prefix, attr->name);
6243 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
6245 * Special tests checking #FIXED namespace declarations
6246 * have the right value since this is not done as an
6247 * attribute checking
6249 if ((attr->prefix == NULL) &&
6250 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6254 while (ns != NULL) {
6255 if (ns->prefix == NULL) {
6256 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
6257 xmlErrValidNode(ctxt, elem,
6258 XML_DTD_ELEM_DEFAULT_NAMESPACE,
6259 "Element %s namespace name for default namespace does not match the DTD\n",
6260 elem->name, NULL, NULL);
6267 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6271 while (ns != NULL) {
6272 if (xmlStrEqual(attr->name, ns->prefix)) {
6273 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
6274 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
6275 "Element %s namespace name for %s does not match the DTD\n",
6276 elem->name, ns->prefix, NULL);
6293 * @ctxt: the validation context
6294 * @doc: a document instance
6296 * Try to validate a the root element
6297 * basically it does the following check as described by the
6298 * XML-1.0 recommendation:
6299 * - [ VC: Root Element Type ]
6300 * it doesn't try to recurse or apply other check to the element
6302 * returns 1 if valid or 0 otherwise
6306 xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6310 if (doc == NULL) return(0);
6312 root = xmlDocGetRootElement(doc);
6313 if ((root == NULL) || (root->name == NULL)) {
6314 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6315 "no root element\n", NULL);
6320 * When doing post validation against a separate DTD, those may
6321 * no internal subset has been generated
6323 if ((doc->intSubset != NULL) &&
6324 (doc->intSubset->name != NULL)) {
6326 * Check first the document root against the NQName
6328 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6329 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
6333 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6334 if (fullname == NULL) {
6335 xmlVErrMemory(ctxt, NULL);
6338 ret = xmlStrEqual(doc->intSubset->name, fullname);
6339 if ((fullname != fn) && (fullname != root->name))
6344 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6345 (xmlStrEqual(root->name, BAD_CAST "html")))
6347 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6348 "root and DTD name do not match '%s' and '%s'\n",
6349 root->name, doc->intSubset->name, NULL);
6359 * xmlValidateElement:
6360 * @ctxt: the validation context
6361 * @doc: a document instance
6362 * @elem: an element instance
6364 * Try to validate the subtree under an element
6366 * returns 1 if valid or 0 otherwise
6370 xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6374 const xmlChar *value;
6377 if (elem == NULL) return(0);
6380 * XInclude elements were added after parsing in the infoset,
6381 * they don't really mean anything validation wise.
6383 if ((elem->type == XML_XINCLUDE_START) ||
6384 (elem->type == XML_XINCLUDE_END) ||
6385 (elem->type == XML_NAMESPACE_DECL))
6391 * Entities references have to be handled separately
6393 if (elem->type == XML_ENTITY_REF_NODE) {
6397 ret &= xmlValidateOneElement(ctxt, doc, elem);
6398 if (elem->type == XML_ELEMENT_NODE) {
6399 attr = elem->properties;
6400 while (attr != NULL) {
6401 value = xmlNodeListGetString(doc, attr->children, 0);
6402 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6404 xmlFree((char *)value);
6408 while (ns != NULL) {
6409 if (elem->ns == NULL)
6410 ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6413 ret &= xmlValidateOneNamespace(ctxt, doc, elem,
6414 elem->ns->prefix, ns, ns->href);
6418 child = elem->children;
6419 while (child != NULL) {
6420 ret &= xmlValidateElement(ctxt, doc, child);
6421 child = child->next;
6429 * @ref: A reference to be validated
6430 * @ctxt: Validation context
6431 * @name: Name of ID we are searching for
6435 xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
6436 const xmlChar *name) {
6442 if ((ref->attr == NULL) && (ref->name == NULL))
6446 xmlChar *dup, *str = NULL, *cur, save;
6448 dup = xmlStrdup(name);
6456 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
6459 id = xmlGetID(ctxt->doc, str);
6461 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
6462 "attribute %s line %d references an unknown ID \"%s\"\n",
6463 ref->name, ref->lineno, str);
6469 while (IS_BLANK_CH(*cur)) cur++;
6472 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
6473 id = xmlGetID(ctxt->doc, name);
6475 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
6476 "IDREF attribute %s references an unknown ID \"%s\"\n",
6477 attr->name, name, NULL);
6480 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6481 xmlChar *dup, *str = NULL, *cur, save;
6483 dup = xmlStrdup(name);
6485 xmlVErrMemory(ctxt, "IDREFS split");
6492 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
6495 id = xmlGetID(ctxt->doc, str);
6497 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
6498 "IDREFS attribute %s references an unknown ID \"%s\"\n",
6499 attr->name, str, NULL);
6505 while (IS_BLANK_CH(*cur)) cur++;
6512 * xmlWalkValidateList:
6513 * @data: Contents of current link
6514 * @user: Value supplied by the user
6516 * Returns 0 to abort the walk or 1 to continue
6519 xmlWalkValidateList(const void *data, const void *user)
6521 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6522 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6527 * xmlValidateCheckRefCallback:
6528 * @ref_list: List of references
6529 * @ctxt: Validation context
6530 * @name: Name of ID we are searching for
6534 xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6535 const xmlChar *name) {
6536 xmlValidateMemo memo;
6538 if (ref_list == NULL)
6543 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6548 * xmlValidateDocumentFinal:
6549 * @ctxt: the validation context
6550 * @doc: a document instance
6552 * Does the final step for the document validation once all the
6553 * incremental validation steps have been completed
6555 * basically it does the following checks described by the XML Rec
6557 * Check all the IDREF/IDREFS attributes definition for validity
6559 * returns 1 if valid or 0 otherwise
6563 xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6564 xmlRefTablePtr table;
6570 xmlErrValid(ctxt, XML_DTD_NO_DOC,
6571 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
6575 /* trick to get correct line id report */
6576 save = ctxt->finishDtd;
6577 ctxt->finishDtd = 0;
6580 * Check all the NOTATION/NOTATIONS attributes
6583 * Check all the ENTITY/ENTITIES attributes definition for validity
6586 * Check all the IDREF/IDREFS attributes definition for validity
6588 table = (xmlRefTablePtr) doc->refs;
6591 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6593 ctxt->finishDtd = save;
6594 return(ctxt->valid);
6599 * @ctxt: the validation context
6600 * @doc: a document instance
6601 * @dtd: a dtd instance
6603 * Try to validate the document against the dtd instance
6605 * Basically it does check all the definitions in the DtD.
6606 * Note the the internal subset (if present) is de-coupled
6607 * (i.e. not used), which could give problems if ID or IDREF
6610 * returns 1 if valid or 0 otherwise
6614 xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6616 xmlDtdPtr oldExt, oldInt;
6619 if (dtd == NULL) return(0);
6620 if (doc == NULL) return(0);
6621 oldExt = doc->extSubset;
6622 oldInt = doc->intSubset;
6623 doc->extSubset = dtd;
6624 doc->intSubset = NULL;
6625 ret = xmlValidateRoot(ctxt, doc);
6627 doc->extSubset = oldExt;
6628 doc->intSubset = oldInt;
6631 if (doc->ids != NULL) {
6632 xmlFreeIDTable(doc->ids);
6635 if (doc->refs != NULL) {
6636 xmlFreeRefTable(doc->refs);
6639 root = xmlDocGetRootElement(doc);
6640 ret = xmlValidateElement(ctxt, doc, root);
6641 ret &= xmlValidateDocumentFinal(ctxt, doc);
6642 doc->extSubset = oldExt;
6643 doc->intSubset = oldInt;
6648 xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6649 const xmlChar *name ATTRIBUTE_UNUSED) {
6652 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6653 xmlChar *notation = cur->content;
6655 if (notation != NULL) {
6658 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6667 xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
6668 const xmlChar *name ATTRIBUTE_UNUSED) {
6671 xmlElementPtr elem = NULL;
6675 switch (cur->atype) {
6676 case XML_ATTRIBUTE_CDATA:
6677 case XML_ATTRIBUTE_ID:
6678 case XML_ATTRIBUTE_IDREF :
6679 case XML_ATTRIBUTE_IDREFS:
6680 case XML_ATTRIBUTE_NMTOKEN:
6681 case XML_ATTRIBUTE_NMTOKENS:
6682 case XML_ATTRIBUTE_ENUMERATION:
6684 case XML_ATTRIBUTE_ENTITY:
6685 case XML_ATTRIBUTE_ENTITIES:
6686 case XML_ATTRIBUTE_NOTATION:
6687 if (cur->defaultValue != NULL) {
6689 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6690 cur->atype, cur->defaultValue);
6691 if ((ret == 0) && (ctxt->valid == 1))
6694 if (cur->tree != NULL) {
6695 xmlEnumerationPtr tree = cur->tree;
6696 while (tree != NULL) {
6697 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
6698 cur->name, cur->atype, tree->name);
6699 if ((ret == 0) && (ctxt->valid == 1))
6705 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6707 if (cur->elem == NULL) {
6708 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
6709 "xmlValidateAttributeCallback(%s): internal error\n",
6710 (const char *) cur->name);
6715 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6716 if ((elem == NULL) && (doc != NULL))
6717 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6718 if ((elem == NULL) && (cur->parent != NULL) &&
6719 (cur->parent->type == XML_DTD_NODE))
6720 elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
6722 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
6723 "attribute %s: could not find decl for element %s\n",
6724 cur->name, cur->elem, NULL);
6727 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6728 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
6729 "NOTATION attribute %s declared for EMPTY element %s\n",
6730 cur->name, cur->elem, NULL);
6737 * xmlValidateDtdFinal:
6738 * @ctxt: the validation context
6739 * @doc: a document instance
6741 * Does the final step for the dtds validation once all the
6742 * subsets have been parsed
6744 * basically it does the following checks described by the XML Rec
6745 * - check that ENTITY and ENTITIES type attributes default or
6746 * possible values matches one of the defined entities.
6747 * - check that NOTATION type attributes default or
6748 * possible values matches one of the defined notations.
6750 * returns 1 if valid or 0 if invalid and -1 if not well-formed
6754 xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6756 xmlAttributeTablePtr table;
6757 xmlEntitiesTablePtr entities;
6759 if ((doc == NULL) || (ctxt == NULL)) return(0);
6760 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6764 dtd = doc->intSubset;
6765 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6766 table = (xmlAttributeTablePtr) dtd->attributes;
6767 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
6769 if ((dtd != NULL) && (dtd->entities != NULL)) {
6770 entities = (xmlEntitiesTablePtr) dtd->entities;
6771 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6774 dtd = doc->extSubset;
6775 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6776 table = (xmlAttributeTablePtr) dtd->attributes;
6777 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
6779 if ((dtd != NULL) && (dtd->entities != NULL)) {
6780 entities = (xmlEntitiesTablePtr) dtd->entities;
6781 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6784 return(ctxt->valid);
6788 * xmlValidateDocument:
6789 * @ctxt: the validation context
6790 * @doc: a document instance
6792 * Try to validate the document instance
6794 * basically it does the all the checks described by the XML Rec
6795 * i.e. validates the internal and external subset (if present)
6796 * and validate the document tree.
6798 * returns 1 if valid or 0 otherwise
6802 xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6808 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6809 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6810 "no DTD found!\n", NULL);
6813 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6814 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6816 if (doc->intSubset->SystemID != NULL) {
6817 sysID = xmlBuildURI(doc->intSubset->SystemID,
6819 if (sysID == NULL) {
6820 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6821 "Could not build URI for external subset \"%s\"\n",
6822 (const char *) doc->intSubset->SystemID);
6827 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6828 (const xmlChar *)sysID);
6831 if (doc->extSubset == NULL) {
6832 if (doc->intSubset->SystemID != NULL) {
6833 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6834 "Could not load the external subset \"%s\"\n",
6835 (const char *) doc->intSubset->SystemID);
6837 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6838 "Could not load the external subset \"%s\"\n",
6839 (const char *) doc->intSubset->ExternalID);
6845 if (doc->ids != NULL) {
6846 xmlFreeIDTable(doc->ids);
6849 if (doc->refs != NULL) {
6850 xmlFreeRefTable(doc->refs);
6853 ret = xmlValidateDtdFinal(ctxt, doc);
6854 if (!xmlValidateRoot(ctxt, doc)) return(0);
6856 root = xmlDocGetRootElement(doc);
6857 ret &= xmlValidateElement(ctxt, doc, root);
6858 ret &= xmlValidateDocumentFinal(ctxt, doc);
6862 /************************************************************************
6864 * Routines for dynamic validation editing *
6866 ************************************************************************/
6869 * xmlValidGetPotentialChildren:
6870 * @ctree: an element content tree
6871 * @names: an array to store the list of child names
6872 * @len: a pointer to the number of element in the list
6873 * @max: the size of the array
6875 * Build/extend a list of potential children allowed by the content tree
6877 * returns the number of element in the list, or -1 in case of error.
6881 xmlValidGetPotentialChildren(xmlElementContent *ctree,
6882 const xmlChar **names,
6883 int *len, int max) {
6886 if ((ctree == NULL) || (names == NULL) || (len == NULL))
6888 if (*len >= max) return(*len);
6890 switch (ctree->type) {
6891 case XML_ELEMENT_CONTENT_PCDATA:
6892 for (i = 0; i < *len;i++)
6893 if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len);
6894 names[(*len)++] = BAD_CAST "#PCDATA";
6896 case XML_ELEMENT_CONTENT_ELEMENT:
6897 for (i = 0; i < *len;i++)
6898 if (xmlStrEqual(ctree->name, names[i])) return(*len);
6899 names[(*len)++] = ctree->name;
6901 case XML_ELEMENT_CONTENT_SEQ:
6902 xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6903 xmlValidGetPotentialChildren(ctree->c2, names, len, max);
6905 case XML_ELEMENT_CONTENT_OR:
6906 xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6907 xmlValidGetPotentialChildren(ctree->c2, names, len, max);
6915 * Dummy function to suppress messages while we try out valid elements
6917 static void XMLCDECL xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
6918 const char *msg ATTRIBUTE_UNUSED, ...) {
6923 * xmlValidGetValidElements:
6924 * @prev: an element to insert after
6925 * @next: an element to insert next
6926 * @names: an array to store the list of child names
6927 * @max: the size of the array
6929 * This function returns the list of authorized children to insert
6930 * within an existing tree while respecting the validity constraints
6931 * forced by the Dtd. The insertion point is defined using @prev and
6932 * @next in the following ways:
6933 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6934 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6935 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6936 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6937 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6939 * pointers to the element names are inserted at the beginning of the array
6940 * and do not need to be freed.
6942 * returns the number of element in the list, or -1 in case of error. If
6943 * the function returns the value @max the caller is invited to grow the
6944 * receiving array and retry.
6948 xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
6951 int nb_valid_elements = 0;
6952 const xmlChar *elements[256]={0};
6953 int nb_elements = 0, i;
6954 const xmlChar *name;
6962 xmlNode *parent_childs;
6963 xmlNode *parent_last;
6965 xmlElement *element_desc;
6967 if (prev == NULL && next == NULL)
6970 if (names == NULL) return(-1);
6971 if (max <= 0) return(-1);
6973 memset(&vctxt, 0, sizeof (xmlValidCtxt));
6974 vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6976 nb_valid_elements = 0;
6977 ref_node = prev ? prev : next;
6978 parent = ref_node->parent;
6981 * Retrieves the parent element declaration
6983 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6985 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6986 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6988 if (element_desc == NULL) return(-1);
6991 * Do a backup of the current tree structure
6993 prev_next = prev ? prev->next : NULL;
6994 next_prev = next ? next->prev : NULL;
6995 parent_childs = parent->children;
6996 parent_last = parent->last;
6999 * Creates a dummy node and insert it into the tree
7001 test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
7002 if (test_node == NULL)
7005 test_node->parent = parent;
7006 test_node->prev = prev;
7007 test_node->next = next;
7008 name = test_node->name;
7010 if (prev) prev->next = test_node;
7011 else parent->children = test_node;
7013 if (next) next->prev = test_node;
7014 else parent->last = test_node;
7017 * Insert each potential child node and check if the parent is
7020 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
7021 elements, &nb_elements, 256);
7023 for (i = 0;i < nb_elements;i++) {
7024 test_node->name = elements[i];
7025 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
7028 for (j = 0; j < nb_valid_elements;j++)
7029 if (xmlStrEqual(elements[i], names[j])) break;
7030 names[nb_valid_elements++] = elements[i];
7031 if (nb_valid_elements >= max) break;
7036 * Restore the tree structure
7038 if (prev) prev->next = prev_next;
7039 if (next) next->prev = next_prev;
7040 parent->children = parent_childs;
7041 parent->last = parent_last;
7044 * Free up the dummy node
7046 test_node->name = name;
7047 xmlFreeNode(test_node);
7049 return(nb_valid_elements);
7051 #endif /* LIBXML_VALID_ENABLED */
7053 #define bottom_valid
7054 #include "elfgcchack.h"