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 != NULL) &&
1176 ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1177 (content->c1->type == XML_ELEMENT_CONTENT_SEQ)))
1178 xmlDumpElementContent(buf, content->c1, 1);
1180 xmlDumpElementContent(buf, content->c1, 0);
1181 xmlBufferWriteChar(buf, " , ");
1182 if ((content->c2 != NULL) &&
1183 ((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1184 ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) &&
1185 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE))))
1186 xmlDumpElementContent(buf, content->c2, 1);
1188 xmlDumpElementContent(buf, content->c2, 0);
1190 case XML_ELEMENT_CONTENT_OR:
1191 if ((content->c1 != NULL) &&
1192 ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1193 (content->c1->type == XML_ELEMENT_CONTENT_SEQ)))
1194 xmlDumpElementContent(buf, content->c1, 1);
1196 xmlDumpElementContent(buf, content->c1, 0);
1197 xmlBufferWriteChar(buf, " | ");
1198 if ((content->c2 != NULL) &&
1199 ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1200 ((content->c2->type == XML_ELEMENT_CONTENT_OR) &&
1201 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE))))
1202 xmlDumpElementContent(buf, content->c2, 1);
1204 xmlDumpElementContent(buf, content->c2, 0);
1207 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1208 "Internal: ELEMENT content corrupted invalid type\n",
1212 xmlBufferWriteChar(buf, ")");
1213 switch (content->ocur) {
1214 case XML_ELEMENT_CONTENT_ONCE:
1216 case XML_ELEMENT_CONTENT_OPT:
1217 xmlBufferWriteChar(buf, "?");
1219 case XML_ELEMENT_CONTENT_MULT:
1220 xmlBufferWriteChar(buf, "*");
1222 case XML_ELEMENT_CONTENT_PLUS:
1223 xmlBufferWriteChar(buf, "+");
1229 * xmlSprintfElementContent:
1230 * @buf: an output buffer
1231 * @content: An element table
1232 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
1234 * Deprecated, unsafe, use xmlSnprintfElementContent
1237 xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1238 xmlElementContentPtr content ATTRIBUTE_UNUSED,
1239 int englob ATTRIBUTE_UNUSED) {
1241 #endif /* LIBXML_OUTPUT_ENABLED */
1244 * xmlSnprintfElementContent:
1245 * @buf: an output buffer
1246 * @size: the buffer size
1247 * @content: An element table
1248 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
1250 * This will dump the content of the element content definition
1251 * Intended just for the debug routine
1254 xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) {
1257 if (content == NULL) return;
1259 if (size - len < 50) {
1260 if ((size - len > 4) && (buf[len - 1] != '.'))
1261 strcat(buf, " ...");
1264 if (englob) strcat(buf, "(");
1265 switch (content->type) {
1266 case XML_ELEMENT_CONTENT_PCDATA:
1267 strcat(buf, "#PCDATA");
1269 case XML_ELEMENT_CONTENT_ELEMENT: {
1270 int qnameLen = xmlStrlen(content->name);
1272 if (content->prefix != NULL)
1273 qnameLen += xmlStrlen(content->prefix) + 1;
1274 if (size - len < qnameLen + 10) {
1275 strcat(buf, " ...");
1278 if (content->prefix != NULL) {
1279 strcat(buf, (char *) content->prefix);
1282 if (content->name != NULL)
1283 strcat(buf, (char *) content->name);
1286 case XML_ELEMENT_CONTENT_SEQ:
1287 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1288 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1289 xmlSnprintfElementContent(buf, size, content->c1, 1);
1291 xmlSnprintfElementContent(buf, size, content->c1, 0);
1293 if (size - len < 50) {
1294 if ((size - len > 4) && (buf[len - 1] != '.'))
1295 strcat(buf, " ...");
1299 if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1300 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1301 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1302 xmlSnprintfElementContent(buf, size, content->c2, 1);
1304 xmlSnprintfElementContent(buf, size, content->c2, 0);
1306 case XML_ELEMENT_CONTENT_OR:
1307 if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1308 (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1309 xmlSnprintfElementContent(buf, size, content->c1, 1);
1311 xmlSnprintfElementContent(buf, size, content->c1, 0);
1313 if (size - len < 50) {
1314 if ((size - len > 4) && (buf[len - 1] != '.'))
1315 strcat(buf, " ...");
1319 if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1320 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1321 (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1322 xmlSnprintfElementContent(buf, size, content->c2, 1);
1324 xmlSnprintfElementContent(buf, size, content->c2, 0);
1327 if (size - strlen(buf) <= 2) return;
1330 switch (content->ocur) {
1331 case XML_ELEMENT_CONTENT_ONCE:
1333 case XML_ELEMENT_CONTENT_OPT:
1336 case XML_ELEMENT_CONTENT_MULT:
1339 case XML_ELEMENT_CONTENT_PLUS:
1345 /****************************************************************
1347 * Registration of DTD declarations *
1349 ****************************************************************/
1355 * Deallocate the memory used by an element definition
1358 xmlFreeElement(xmlElementPtr elem) {
1359 if (elem == NULL) return;
1360 xmlUnlinkNode((xmlNodePtr) elem);
1361 xmlFreeDocElementContent(elem->doc, elem->content);
1362 if (elem->name != NULL)
1363 xmlFree((xmlChar *) elem->name);
1364 if (elem->prefix != NULL)
1365 xmlFree((xmlChar *) elem->prefix);
1366 #ifdef LIBXML_REGEXP_ENABLED
1367 if (elem->contModel != NULL)
1368 xmlRegFreeRegexp(elem->contModel);
1375 * xmlAddElementDecl:
1376 * @ctxt: the validation context
1377 * @dtd: pointer to the DTD
1378 * @name: the entity name
1379 * @type: the element type
1380 * @content: the element content tree or NULL
1382 * Register a new element declaration
1384 * Returns NULL if not, otherwise the entity
1387 xmlAddElementDecl(xmlValidCtxtPtr ctxt,
1388 xmlDtdPtr dtd, const xmlChar *name,
1389 xmlElementTypeVal type,
1390 xmlElementContentPtr content) {
1392 xmlElementTablePtr table;
1393 xmlAttributePtr oldAttributes = NULL;
1394 xmlChar *ns, *uqname;
1404 case XML_ELEMENT_TYPE_EMPTY:
1405 if (content != NULL) {
1406 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1407 "xmlAddElementDecl: content != NULL for EMPTY\n",
1412 case XML_ELEMENT_TYPE_ANY:
1413 if (content != NULL) {
1414 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1415 "xmlAddElementDecl: content != NULL for ANY\n",
1420 case XML_ELEMENT_TYPE_MIXED:
1421 if (content == NULL) {
1422 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1423 "xmlAddElementDecl: content == NULL for MIXED\n",
1428 case XML_ELEMENT_TYPE_ELEMENT:
1429 if (content == NULL) {
1430 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1431 "xmlAddElementDecl: content == NULL for ELEMENT\n",
1437 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1438 "Internal: ELEMENT decl corrupted invalid type\n",
1444 * check if name is a QName
1446 uqname = xmlSplitQName2(name, &ns);
1451 * Create the Element table if needed.
1453 table = (xmlElementTablePtr) dtd->elements;
1454 if (table == NULL) {
1455 xmlDictPtr dict = NULL;
1457 if (dtd->doc != NULL)
1458 dict = dtd->doc->dict;
1459 table = xmlHashCreateDict(0, dict);
1460 dtd->elements = (void *) table;
1462 if (table == NULL) {
1464 "xmlAddElementDecl: Table creation failed!\n");
1473 * lookup old attributes inserted on an undefined element in the
1476 if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1477 ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1478 if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1479 oldAttributes = ret->attributes;
1480 ret->attributes = NULL;
1481 xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1482 xmlFreeElement(ret);
1487 * The element may already be present if one of its attribute
1488 * was registered first
1490 ret = xmlHashLookup2(table, name, ns);
1492 if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1493 #ifdef LIBXML_VALID_ENABLED
1495 * The element is already defined in this DTD.
1497 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1498 "Redefinition of element %s\n",
1500 #endif /* LIBXML_VALID_ENABLED */
1512 ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1514 xmlVErrMemory(ctxt, "malloc failed");
1521 memset(ret, 0, sizeof(xmlElement));
1522 ret->type = XML_ELEMENT_DECL;
1525 * fill the structure.
1527 ret->name = xmlStrdup(name);
1528 if (ret->name == NULL) {
1529 xmlVErrMemory(ctxt, "malloc failed");
1541 * Insertion must not fail
1543 if (xmlHashAddEntry2(table, name, ns, ret)) {
1544 #ifdef LIBXML_VALID_ENABLED
1546 * The element is already defined in this DTD.
1548 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1549 "Redefinition of element %s\n",
1551 #endif /* LIBXML_VALID_ENABLED */
1552 xmlFreeElement(ret);
1558 * For new element, may have attributes from earlier
1559 * definition in internal subset
1561 ret->attributes = oldAttributes;
1565 * Finish to fill the structure.
1569 * Avoid a stupid copy when called by the parser
1570 * and flag it by setting a special parent value
1571 * so the parser doesn't unallocate it.
1573 if ((ctxt != NULL) &&
1574 ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
1575 (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1))) {
1576 ret->content = content;
1577 if (content != NULL)
1578 content->parent = (xmlElementContentPtr) 1;
1580 ret->content = xmlCopyDocElementContent(dtd->doc, content);
1584 * Link it to the DTD
1587 ret->doc = dtd->doc;
1588 if (dtd->last == NULL) {
1589 dtd->children = dtd->last = (xmlNodePtr) ret;
1591 dtd->last->next = (xmlNodePtr) ret;
1592 ret->prev = dtd->last;
1593 dtd->last = (xmlNodePtr) ret;
1601 * xmlFreeElementTable:
1602 * @table: An element table
1604 * Deallocate the memory used by an element hash table.
1607 xmlFreeElementTable(xmlElementTablePtr table) {
1608 xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement);
1611 #ifdef LIBXML_TREE_ENABLED
1616 * Build a copy of an element.
1618 * Returns the new xmlElementPtr or NULL in case of error.
1620 static xmlElementPtr
1621 xmlCopyElement(xmlElementPtr elem) {
1624 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1626 xmlVErrMemory(NULL, "malloc failed");
1629 memset(cur, 0, sizeof(xmlElement));
1630 cur->type = XML_ELEMENT_DECL;
1631 cur->etype = elem->etype;
1632 if (elem->name != NULL)
1633 cur->name = xmlStrdup(elem->name);
1636 if (elem->prefix != NULL)
1637 cur->prefix = xmlStrdup(elem->prefix);
1640 cur->content = xmlCopyElementContent(elem->content);
1641 /* TODO : rebuild the attribute list on the copy */
1642 cur->attributes = NULL;
1647 * xmlCopyElementTable:
1648 * @table: An element table
1650 * Build a copy of an element table.
1652 * Returns the new xmlElementTablePtr or NULL in case of error.
1655 xmlCopyElementTable(xmlElementTablePtr table) {
1656 return((xmlElementTablePtr) xmlHashCopy(table,
1657 (xmlHashCopier) xmlCopyElement));
1659 #endif /* LIBXML_TREE_ENABLED */
1661 #ifdef LIBXML_OUTPUT_ENABLED
1663 * xmlDumpElementDecl:
1664 * @buf: the XML buffer output
1665 * @elem: An element table
1667 * This will dump the content of the element declaration as an XML
1671 xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1672 if ((buf == NULL) || (elem == NULL))
1674 switch (elem->etype) {
1675 case XML_ELEMENT_TYPE_EMPTY:
1676 xmlBufferWriteChar(buf, "<!ELEMENT ");
1677 if (elem->prefix != NULL) {
1678 xmlBufferWriteCHAR(buf, elem->prefix);
1679 xmlBufferWriteChar(buf, ":");
1681 xmlBufferWriteCHAR(buf, elem->name);
1682 xmlBufferWriteChar(buf, " EMPTY>\n");
1684 case XML_ELEMENT_TYPE_ANY:
1685 xmlBufferWriteChar(buf, "<!ELEMENT ");
1686 if (elem->prefix != NULL) {
1687 xmlBufferWriteCHAR(buf, elem->prefix);
1688 xmlBufferWriteChar(buf, ":");
1690 xmlBufferWriteCHAR(buf, elem->name);
1691 xmlBufferWriteChar(buf, " ANY>\n");
1693 case XML_ELEMENT_TYPE_MIXED:
1694 xmlBufferWriteChar(buf, "<!ELEMENT ");
1695 if (elem->prefix != NULL) {
1696 xmlBufferWriteCHAR(buf, elem->prefix);
1697 xmlBufferWriteChar(buf, ":");
1699 xmlBufferWriteCHAR(buf, elem->name);
1700 xmlBufferWriteChar(buf, " ");
1701 xmlDumpElementContent(buf, elem->content, 1);
1702 xmlBufferWriteChar(buf, ">\n");
1704 case XML_ELEMENT_TYPE_ELEMENT:
1705 xmlBufferWriteChar(buf, "<!ELEMENT ");
1706 if (elem->prefix != NULL) {
1707 xmlBufferWriteCHAR(buf, elem->prefix);
1708 xmlBufferWriteChar(buf, ":");
1710 xmlBufferWriteCHAR(buf, elem->name);
1711 xmlBufferWriteChar(buf, " ");
1712 xmlDumpElementContent(buf, elem->content, 1);
1713 xmlBufferWriteChar(buf, ">\n");
1716 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1717 "Internal: ELEMENT struct corrupted invalid type\n",
1723 * xmlDumpElementDeclScan:
1724 * @elem: An element table
1725 * @buf: the XML buffer output
1727 * This routine is used by the hash scan function. It just reverses
1731 xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) {
1732 xmlDumpElementDecl(buf, elem);
1736 * xmlDumpElementTable:
1737 * @buf: the XML buffer output
1738 * @table: An element table
1740 * This will dump the content of the element table as an XML DTD definition
1743 xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1744 if ((buf == NULL) || (table == NULL))
1746 xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf);
1748 #endif /* LIBXML_OUTPUT_ENABLED */
1751 * xmlCreateEnumeration:
1752 * @name: the enumeration name or NULL
1754 * create and initialize an enumeration attribute node.
1756 * Returns the xmlEnumerationPtr just created or NULL in case
1760 xmlCreateEnumeration(const xmlChar *name) {
1761 xmlEnumerationPtr ret;
1763 ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1765 xmlVErrMemory(NULL, "malloc failed");
1768 memset(ret, 0, sizeof(xmlEnumeration));
1771 ret->name = xmlStrdup(name);
1776 * xmlFreeEnumeration:
1777 * @cur: the tree to free.
1779 * free an enumeration attribute node (recursive).
1782 xmlFreeEnumeration(xmlEnumerationPtr cur) {
1783 if (cur == NULL) return;
1785 if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1787 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
1791 #ifdef LIBXML_TREE_ENABLED
1793 * xmlCopyEnumeration:
1794 * @cur: the tree to copy.
1796 * Copy an enumeration attribute node (recursive).
1798 * Returns the xmlEnumerationPtr just created or NULL in case
1802 xmlCopyEnumeration(xmlEnumerationPtr cur) {
1803 xmlEnumerationPtr ret;
1805 if (cur == NULL) return(NULL);
1806 ret = xmlCreateEnumeration((xmlChar *) cur->name);
1807 if (ret == NULL) return(NULL);
1809 if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1810 else ret->next = NULL;
1814 #endif /* LIBXML_TREE_ENABLED */
1816 #ifdef LIBXML_OUTPUT_ENABLED
1818 * xmlDumpEnumeration:
1819 * @buf: the XML buffer output
1820 * @enum: An enumeration
1822 * This will dump the content of the enumeration
1825 xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1826 if ((buf == NULL) || (cur == NULL))
1829 xmlBufferWriteCHAR(buf, cur->name);
1830 if (cur->next == NULL)
1831 xmlBufferWriteChar(buf, ")");
1833 xmlBufferWriteChar(buf, " | ");
1834 xmlDumpEnumeration(buf, cur->next);
1837 #endif /* LIBXML_OUTPUT_ENABLED */
1839 #ifdef LIBXML_VALID_ENABLED
1841 * xmlScanIDAttributeDecl:
1842 * @ctxt: the validation context
1843 * @elem: the element name
1844 * @err: whether to raise errors here
1846 * Verify that the element don't have too many ID attributes
1849 * Returns the number of ID attributes found.
1852 xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
1853 xmlAttributePtr cur;
1856 if (elem == NULL) return(0);
1857 cur = elem->attributes;
1858 while (cur != NULL) {
1859 if (cur->atype == XML_ATTRIBUTE_ID) {
1861 if ((ret > 1) && (err))
1862 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
1863 "Element %s has too many ID attributes defined : %s\n",
1864 elem->name, cur->name, NULL);
1870 #endif /* LIBXML_VALID_ENABLED */
1874 * @elem: An attribute
1876 * Deallocate the memory used by an attribute definition
1879 xmlFreeAttribute(xmlAttributePtr attr) {
1882 if (attr == NULL) return;
1883 if (attr->doc != NULL)
1884 dict = attr->doc->dict;
1887 xmlUnlinkNode((xmlNodePtr) attr);
1888 if (attr->tree != NULL)
1889 xmlFreeEnumeration(attr->tree);
1891 if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
1892 xmlFree((xmlChar *) attr->elem);
1893 if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
1894 xmlFree((xmlChar *) attr->name);
1895 if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
1896 xmlFree((xmlChar *) attr->prefix);
1897 if ((attr->defaultValue != NULL) &&
1898 (!xmlDictOwns(dict, attr->defaultValue)))
1899 xmlFree((xmlChar *) attr->defaultValue);
1901 if (attr->elem != NULL)
1902 xmlFree((xmlChar *) attr->elem);
1903 if (attr->name != NULL)
1904 xmlFree((xmlChar *) attr->name);
1905 if (attr->defaultValue != NULL)
1906 xmlFree((xmlChar *) attr->defaultValue);
1907 if (attr->prefix != NULL)
1908 xmlFree((xmlChar *) attr->prefix);
1915 * xmlAddAttributeDecl:
1916 * @ctxt: the validation context
1917 * @dtd: pointer to the DTD
1918 * @elem: the element name
1919 * @name: the attribute name
1920 * @ns: the attribute namespace prefix
1921 * @type: the attribute type
1922 * @def: the attribute default type
1923 * @defaultValue: the attribute default value
1924 * @tree: if it's an enumeration, the associated list
1926 * Register a new attribute declaration
1927 * Note that @tree becomes the ownership of the DTD
1929 * Returns NULL if not new, otherwise the attribute decl
1932 xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
1933 xmlDtdPtr dtd, const xmlChar *elem,
1934 const xmlChar *name, const xmlChar *ns,
1935 xmlAttributeType type, xmlAttributeDefault def,
1936 const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1937 xmlAttributePtr ret;
1938 xmlAttributeTablePtr table;
1939 xmlElementPtr elemDef;
1940 xmlDictPtr dict = NULL;
1943 xmlFreeEnumeration(tree);
1947 xmlFreeEnumeration(tree);
1951 xmlFreeEnumeration(tree);
1954 if (dtd->doc != NULL)
1955 dict = dtd->doc->dict;
1957 #ifdef LIBXML_VALID_ENABLED
1959 * Check the type and possibly the default value.
1962 case XML_ATTRIBUTE_CDATA:
1964 case XML_ATTRIBUTE_ID:
1966 case XML_ATTRIBUTE_IDREF:
1968 case XML_ATTRIBUTE_IDREFS:
1970 case XML_ATTRIBUTE_ENTITY:
1972 case XML_ATTRIBUTE_ENTITIES:
1974 case XML_ATTRIBUTE_NMTOKEN:
1976 case XML_ATTRIBUTE_NMTOKENS:
1978 case XML_ATTRIBUTE_ENUMERATION:
1980 case XML_ATTRIBUTE_NOTATION:
1983 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1984 "Internal: ATTRIBUTE struct corrupted invalid type\n",
1986 xmlFreeEnumeration(tree);
1989 if ((defaultValue != NULL) &&
1990 (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) {
1991 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1992 "Attribute %s of %s: invalid default value\n",
1993 elem, name, defaultValue);
1994 defaultValue = NULL;
1998 #endif /* LIBXML_VALID_ENABLED */
2001 * Check first that an attribute defined in the external subset wasn't
2002 * already defined in the internal subset
2004 if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
2005 (dtd->doc->intSubset != NULL) &&
2006 (dtd->doc->intSubset->attributes != NULL)) {
2007 ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
2009 xmlFreeEnumeration(tree);
2015 * Create the Attribute table if needed.
2017 table = (xmlAttributeTablePtr) dtd->attributes;
2018 if (table == NULL) {
2019 table = xmlHashCreateDict(0, dict);
2020 dtd->attributes = (void *) table;
2022 if (table == NULL) {
2024 "xmlAddAttributeDecl: Table creation failed!\n");
2025 xmlFreeEnumeration(tree);
2030 ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2032 xmlVErrMemory(ctxt, "malloc failed");
2033 xmlFreeEnumeration(tree);
2036 memset(ret, 0, sizeof(xmlAttribute));
2037 ret->type = XML_ATTRIBUTE_DECL;
2040 * fill the structure.
2044 * doc must be set before possible error causes call
2045 * to xmlFreeAttribute (because it's used to check on
2048 ret->doc = dtd->doc;
2050 ret->name = xmlDictLookup(dict, name, -1);
2051 ret->prefix = xmlDictLookup(dict, ns, -1);
2052 ret->elem = xmlDictLookup(dict, elem, -1);
2054 ret->name = xmlStrdup(name);
2055 ret->prefix = xmlStrdup(ns);
2056 ret->elem = xmlStrdup(elem);
2060 if (defaultValue != NULL) {
2062 ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
2064 ret->defaultValue = xmlStrdup(defaultValue);
2069 * Search the DTD for previous declarations of the ATTLIST
2071 if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
2072 #ifdef LIBXML_VALID_ENABLED
2074 * The attribute is already defined in this DTD.
2076 xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
2077 "Attribute %s of element %s: already defined\n",
2079 #endif /* LIBXML_VALID_ENABLED */
2080 xmlFreeAttribute(ret);
2086 * Multiple ID per element
2088 elemDef = xmlGetDtdElementDesc2(dtd, elem, 1);
2089 if (elemDef != NULL) {
2091 #ifdef LIBXML_VALID_ENABLED
2092 if ((type == XML_ATTRIBUTE_ID) &&
2093 (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) {
2094 xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
2095 "Element %s has too may ID attributes defined : %s\n",
2100 #endif /* LIBXML_VALID_ENABLED */
2103 * Insert namespace default def first they need to be
2106 if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
2107 ((ret->prefix != NULL &&
2108 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
2109 ret->nexth = elemDef->attributes;
2110 elemDef->attributes = ret;
2112 xmlAttributePtr tmp = elemDef->attributes;
2114 while ((tmp != NULL) &&
2115 ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
2116 ((ret->prefix != NULL &&
2117 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
2118 if (tmp->nexth == NULL)
2123 ret->nexth = tmp->nexth;
2126 ret->nexth = elemDef->attributes;
2127 elemDef->attributes = ret;
2133 * Link it to the DTD
2136 if (dtd->last == NULL) {
2137 dtd->children = dtd->last = (xmlNodePtr) ret;
2139 dtd->last->next = (xmlNodePtr) ret;
2140 ret->prev = dtd->last;
2141 dtd->last = (xmlNodePtr) ret;
2147 * xmlFreeAttributeTable:
2148 * @table: An attribute table
2150 * Deallocate the memory used by an entities hash table.
2153 xmlFreeAttributeTable(xmlAttributeTablePtr table) {
2154 xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute);
2157 #ifdef LIBXML_TREE_ENABLED
2160 * @attr: An attribute
2162 * Build a copy of an attribute.
2164 * Returns the new xmlAttributePtr or NULL in case of error.
2166 static xmlAttributePtr
2167 xmlCopyAttribute(xmlAttributePtr attr) {
2168 xmlAttributePtr cur;
2170 cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2172 xmlVErrMemory(NULL, "malloc failed");
2175 memset(cur, 0, sizeof(xmlAttribute));
2176 cur->type = XML_ATTRIBUTE_DECL;
2177 cur->atype = attr->atype;
2178 cur->def = attr->def;
2179 cur->tree = xmlCopyEnumeration(attr->tree);
2180 if (attr->elem != NULL)
2181 cur->elem = xmlStrdup(attr->elem);
2182 if (attr->name != NULL)
2183 cur->name = xmlStrdup(attr->name);
2184 if (attr->prefix != NULL)
2185 cur->prefix = xmlStrdup(attr->prefix);
2186 if (attr->defaultValue != NULL)
2187 cur->defaultValue = xmlStrdup(attr->defaultValue);
2192 * xmlCopyAttributeTable:
2193 * @table: An attribute table
2195 * Build a copy of an attribute table.
2197 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2199 xmlAttributeTablePtr
2200 xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2201 return((xmlAttributeTablePtr) xmlHashCopy(table,
2202 (xmlHashCopier) xmlCopyAttribute));
2204 #endif /* LIBXML_TREE_ENABLED */
2206 #ifdef LIBXML_OUTPUT_ENABLED
2208 * xmlDumpAttributeDecl:
2209 * @buf: the XML buffer output
2210 * @attr: An attribute declaration
2212 * This will dump the content of the attribute declaration as an XML
2216 xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
2217 if ((buf == NULL) || (attr == NULL))
2219 xmlBufferWriteChar(buf, "<!ATTLIST ");
2220 xmlBufferWriteCHAR(buf, attr->elem);
2221 xmlBufferWriteChar(buf, " ");
2222 if (attr->prefix != NULL) {
2223 xmlBufferWriteCHAR(buf, attr->prefix);
2224 xmlBufferWriteChar(buf, ":");
2226 xmlBufferWriteCHAR(buf, attr->name);
2227 switch (attr->atype) {
2228 case XML_ATTRIBUTE_CDATA:
2229 xmlBufferWriteChar(buf, " CDATA");
2231 case XML_ATTRIBUTE_ID:
2232 xmlBufferWriteChar(buf, " ID");
2234 case XML_ATTRIBUTE_IDREF:
2235 xmlBufferWriteChar(buf, " IDREF");
2237 case XML_ATTRIBUTE_IDREFS:
2238 xmlBufferWriteChar(buf, " IDREFS");
2240 case XML_ATTRIBUTE_ENTITY:
2241 xmlBufferWriteChar(buf, " ENTITY");
2243 case XML_ATTRIBUTE_ENTITIES:
2244 xmlBufferWriteChar(buf, " ENTITIES");
2246 case XML_ATTRIBUTE_NMTOKEN:
2247 xmlBufferWriteChar(buf, " NMTOKEN");
2249 case XML_ATTRIBUTE_NMTOKENS:
2250 xmlBufferWriteChar(buf, " NMTOKENS");
2252 case XML_ATTRIBUTE_ENUMERATION:
2253 xmlBufferWriteChar(buf, " (");
2254 xmlDumpEnumeration(buf, attr->tree);
2256 case XML_ATTRIBUTE_NOTATION:
2257 xmlBufferWriteChar(buf, " NOTATION (");
2258 xmlDumpEnumeration(buf, attr->tree);
2261 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2262 "Internal: ATTRIBUTE struct corrupted invalid type\n",
2265 switch (attr->def) {
2266 case XML_ATTRIBUTE_NONE:
2268 case XML_ATTRIBUTE_REQUIRED:
2269 xmlBufferWriteChar(buf, " #REQUIRED");
2271 case XML_ATTRIBUTE_IMPLIED:
2272 xmlBufferWriteChar(buf, " #IMPLIED");
2274 case XML_ATTRIBUTE_FIXED:
2275 xmlBufferWriteChar(buf, " #FIXED");
2278 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2279 "Internal: ATTRIBUTE struct corrupted invalid def\n",
2282 if (attr->defaultValue != NULL) {
2283 xmlBufferWriteChar(buf, " ");
2284 xmlBufferWriteQuotedString(buf, attr->defaultValue);
2286 xmlBufferWriteChar(buf, ">\n");
2290 * xmlDumpAttributeDeclScan:
2291 * @attr: An attribute declaration
2292 * @buf: the XML buffer output
2294 * This is used with the hash scan function - just reverses arguments
2297 xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) {
2298 xmlDumpAttributeDecl(buf, attr);
2302 * xmlDumpAttributeTable:
2303 * @buf: the XML buffer output
2304 * @table: An attribute table
2306 * This will dump the content of the attribute table as an XML DTD definition
2309 xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
2310 if ((buf == NULL) || (table == NULL))
2312 xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf);
2314 #endif /* LIBXML_OUTPUT_ENABLED */
2316 /************************************************************************
2320 ************************************************************************/
2325 * Deallocate the memory used by an notation definition
2328 xmlFreeNotation(xmlNotationPtr nota) {
2329 if (nota == NULL) return;
2330 if (nota->name != NULL)
2331 xmlFree((xmlChar *) nota->name);
2332 if (nota->PublicID != NULL)
2333 xmlFree((xmlChar *) nota->PublicID);
2334 if (nota->SystemID != NULL)
2335 xmlFree((xmlChar *) nota->SystemID);
2341 * xmlAddNotationDecl:
2342 * @dtd: pointer to the DTD
2343 * @ctxt: the validation context
2344 * @name: the entity name
2345 * @PublicID: the public identifier or NULL
2346 * @SystemID: the system identifier or NULL
2348 * Register a new notation declaration
2350 * Returns NULL if not, otherwise the entity
2353 xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
2354 const xmlChar *name,
2355 const xmlChar *PublicID, const xmlChar *SystemID) {
2357 xmlNotationTablePtr table;
2365 if ((PublicID == NULL) && (SystemID == NULL)) {
2370 * Create the Notation table if needed.
2372 table = (xmlNotationTablePtr) dtd->notations;
2373 if (table == NULL) {
2374 xmlDictPtr dict = NULL;
2375 if (dtd->doc != NULL)
2376 dict = dtd->doc->dict;
2378 dtd->notations = table = xmlHashCreateDict(0, dict);
2380 if (table == NULL) {
2382 "xmlAddNotationDecl: Table creation failed!\n");
2386 ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2388 xmlVErrMemory(ctxt, "malloc failed");
2391 memset(ret, 0, sizeof(xmlNotation));
2394 * fill the structure.
2396 ret->name = xmlStrdup(name);
2397 if (SystemID != NULL)
2398 ret->SystemID = xmlStrdup(SystemID);
2399 if (PublicID != NULL)
2400 ret->PublicID = xmlStrdup(PublicID);
2404 * Check the DTD for previous declarations of the ATTLIST
2406 if (xmlHashAddEntry(table, name, ret)) {
2407 #ifdef LIBXML_VALID_ENABLED
2408 xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2409 "xmlAddNotationDecl: %s already defined\n",
2410 (const char *) name);
2411 #endif /* LIBXML_VALID_ENABLED */
2412 xmlFreeNotation(ret);
2419 * xmlFreeNotationTable:
2420 * @table: An notation table
2422 * Deallocate the memory used by an entities hash table.
2425 xmlFreeNotationTable(xmlNotationTablePtr table) {
2426 xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation);
2429 #ifdef LIBXML_TREE_ENABLED
2434 * Build a copy of a notation.
2436 * Returns the new xmlNotationPtr or NULL in case of error.
2438 static xmlNotationPtr
2439 xmlCopyNotation(xmlNotationPtr nota) {
2442 cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2444 xmlVErrMemory(NULL, "malloc failed");
2447 if (nota->name != NULL)
2448 cur->name = xmlStrdup(nota->name);
2451 if (nota->PublicID != NULL)
2452 cur->PublicID = xmlStrdup(nota->PublicID);
2454 cur->PublicID = NULL;
2455 if (nota->SystemID != NULL)
2456 cur->SystemID = xmlStrdup(nota->SystemID);
2458 cur->SystemID = NULL;
2463 * xmlCopyNotationTable:
2464 * @table: A notation table
2466 * Build a copy of a notation table.
2468 * Returns the new xmlNotationTablePtr or NULL in case of error.
2471 xmlCopyNotationTable(xmlNotationTablePtr table) {
2472 return((xmlNotationTablePtr) xmlHashCopy(table,
2473 (xmlHashCopier) xmlCopyNotation));
2475 #endif /* LIBXML_TREE_ENABLED */
2477 #ifdef LIBXML_OUTPUT_ENABLED
2479 * xmlDumpNotationDecl:
2480 * @buf: the XML buffer output
2481 * @nota: A notation declaration
2483 * This will dump the content the notation declaration as an XML DTD definition
2486 xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2487 if ((buf == NULL) || (nota == NULL))
2489 xmlBufferWriteChar(buf, "<!NOTATION ");
2490 xmlBufferWriteCHAR(buf, nota->name);
2491 if (nota->PublicID != NULL) {
2492 xmlBufferWriteChar(buf, " PUBLIC ");
2493 xmlBufferWriteQuotedString(buf, nota->PublicID);
2494 if (nota->SystemID != NULL) {
2495 xmlBufferWriteChar(buf, " ");
2496 xmlBufferWriteQuotedString(buf, nota->SystemID);
2499 xmlBufferWriteChar(buf, " SYSTEM ");
2500 xmlBufferWriteQuotedString(buf, nota->SystemID);
2502 xmlBufferWriteChar(buf, " >\n");
2506 * xmlDumpNotationDeclScan:
2507 * @nota: A notation declaration
2508 * @buf: the XML buffer output
2510 * This is called with the hash scan function, and just reverses args
2513 xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) {
2514 xmlDumpNotationDecl(buf, nota);
2518 * xmlDumpNotationTable:
2519 * @buf: the XML buffer output
2520 * @table: A notation table
2522 * This will dump the content of the notation table as an XML DTD definition
2525 xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2526 if ((buf == NULL) || (table == NULL))
2528 xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf);
2530 #endif /* LIBXML_OUTPUT_ENABLED */
2532 /************************************************************************
2536 ************************************************************************/
2541 * Free a string if it is not owned by the "dict" dictionary in the
2544 #define DICT_FREE(str) \
2545 if ((str) && ((!dict) || \
2546 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
2547 xmlFree((char *)(str));
2553 * Deallocate the memory used by an id definition
2556 xmlFreeID(xmlIDPtr id) {
2557 xmlDictPtr dict = NULL;
2559 if (id == NULL) return;
2561 if (id->doc != NULL)
2562 dict = id->doc->dict;
2564 if (id->value != NULL)
2565 DICT_FREE(id->value)
2566 if (id->name != NULL)
2574 * @ctxt: the validation context
2575 * @doc: pointer to the document
2576 * @value: the value name
2577 * @attr: the attribute holding the ID
2579 * Register a new id declaration
2581 * Returns NULL if not, otherwise the new xmlIDPtr
2584 xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2587 xmlIDTablePtr table;
2592 if (value == NULL) {
2600 * Create the ID table if needed.
2602 table = (xmlIDTablePtr) doc->ids;
2603 if (table == NULL) {
2604 doc->ids = table = xmlHashCreateDict(0, doc->dict);
2606 if (table == NULL) {
2608 "xmlAddID: Table creation failed!\n");
2612 ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2614 xmlVErrMemory(ctxt, "malloc failed");
2619 * fill the structure.
2621 ret->value = xmlStrdup(value);
2623 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2625 * Operating in streaming mode, attr is gonna disapear
2627 if (doc->dict != NULL)
2628 ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2630 ret->name = xmlStrdup(attr->name);
2636 ret->lineno = xmlGetLineNo(attr->parent);
2638 if (xmlHashAddEntry(table, value, ret) < 0) {
2639 #ifdef LIBXML_VALID_ENABLED
2641 * The id is already defined in this DTD.
2644 xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2645 "ID %s already defined\n", value, NULL, NULL);
2647 #endif /* LIBXML_VALID_ENABLED */
2652 attr->atype = XML_ATTRIBUTE_ID;
2658 * @table: An id table
2660 * Deallocate the memory used by an ID hash table.
2663 xmlFreeIDTable(xmlIDTablePtr table) {
2664 xmlHashFree(table, (xmlHashDeallocator) xmlFreeID);
2669 * @doc: the document
2670 * @elem: the element carrying the attribute
2671 * @attr: the attribute
2673 * Determine whether an attribute is of type ID. In case we have DTD(s)
2674 * then this is done if DTD loading has been requested. In the case
2675 * of HTML documents parsed with the HTML parser, then ID detection is
2676 * done systematically.
2678 * Returns 0 or 1 depending on the lookup result
2681 xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2682 if ((attr == NULL) || (attr->name == NULL)) return(0);
2683 if ((attr->ns != NULL) && (attr->ns->prefix != NULL) &&
2684 (!strcmp((char *) attr->name, "id")) &&
2685 (!strcmp((char *) attr->ns->prefix, "xml")))
2687 if (doc == NULL) return(0);
2688 if ((doc->intSubset == NULL) && (doc->extSubset == NULL) &&
2689 (doc->type != XML_HTML_DOCUMENT_NODE)) {
2691 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2692 if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2693 ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
2694 ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a")))))
2697 } else if (elem == NULL) {
2700 xmlAttributePtr attrDecl = NULL;
2702 xmlChar felem[50], fattr[50];
2703 xmlChar *fullelemname, *fullattrname;
2705 fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
2706 xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
2707 (xmlChar *)elem->name;
2709 fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ?
2710 xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) :
2711 (xmlChar *)attr->name;
2713 if (fullelemname != NULL && fullattrname != NULL) {
2714 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname,
2716 if ((attrDecl == NULL) && (doc->extSubset != NULL))
2717 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname,
2721 if ((fullattrname != fattr) && (fullattrname != attr->name))
2722 xmlFree(fullattrname);
2723 if ((fullelemname != felem) && (fullelemname != elem->name))
2724 xmlFree(fullelemname);
2726 if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2734 * @doc: the document
2735 * @attr: the attribute
2737 * Remove the given attribute from the ID table maintained internally.
2739 * Returns -1 if the lookup failed and 0 otherwise
2742 xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2743 xmlIDTablePtr table;
2747 if (doc == NULL) return(-1);
2748 if (attr == NULL) return(-1);
2750 table = (xmlIDTablePtr) doc->ids;
2754 ID = xmlNodeListGetString(doc, attr->children, 1);
2758 id = xmlHashLookup(table, ID);
2759 if (id == NULL || id->attr != attr) {
2764 xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID);
2772 * @doc: pointer to the document
2775 * Search the attribute declaring the given ID
2777 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2780 xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2781 xmlIDTablePtr table;
2792 table = (xmlIDTablePtr) doc->ids;
2796 id = xmlHashLookup(table, ID);
2799 if (id->attr == NULL) {
2801 * We are operating on a stream, return a well known reference
2802 * since the attribute node doesn't exist anymore
2804 return((xmlAttrPtr) doc);
2809 /************************************************************************
2813 ************************************************************************/
2814 typedef struct xmlRemoveMemo_t
2820 typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2822 typedef struct xmlValidateMemo_t
2824 xmlValidCtxtPtr ctxt;
2825 const xmlChar *name;
2828 typedef xmlValidateMemo *xmlValidateMemoPtr;
2834 * Deallocate the memory used by a ref definition
2837 xmlFreeRef(xmlLinkPtr lk) {
2838 xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2839 if (ref == NULL) return;
2840 if (ref->value != NULL)
2841 xmlFree((xmlChar *)ref->value);
2842 if (ref->name != NULL)
2843 xmlFree((xmlChar *)ref->name);
2849 * @list_ref: A list of references.
2851 * Deallocate the memory used by a list of references
2854 xmlFreeRefList(xmlListPtr list_ref) {
2855 if (list_ref == NULL) return;
2856 xmlListDelete(list_ref);
2861 * @data: Contents of current link
2862 * @user: Value supplied by the user
2864 * Returns 0 to abort the walk or 1 to continue
2867 xmlWalkRemoveRef(const void *data, const void *user)
2869 xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2870 xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2871 xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
2873 if (attr0 == attr1) { /* Matched: remove and terminate walk */
2874 xmlListRemoveFirst(ref_list, (void *)data);
2882 * @data0: Value supplied by the user
2883 * @data1: Value supplied by the user
2885 * Do nothing, return 0. Used to create unordered lists.
2888 xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2889 const void *data1 ATTRIBUTE_UNUSED)
2896 * @ctxt: the validation context
2897 * @doc: pointer to the document
2898 * @value: the value name
2899 * @attr: the attribute holding the Ref
2901 * Register a new ref declaration
2903 * Returns NULL if not, otherwise the new xmlRefPtr
2906 xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2909 xmlRefTablePtr table;
2910 xmlListPtr ref_list;
2915 if (value == NULL) {
2923 * Create the Ref table if needed.
2925 table = (xmlRefTablePtr) doc->refs;
2926 if (table == NULL) {
2927 doc->refs = table = xmlHashCreateDict(0, doc->dict);
2929 if (table == NULL) {
2931 "xmlAddRef: Table creation failed!\n");
2935 ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2937 xmlVErrMemory(ctxt, "malloc failed");
2942 * fill the structure.
2944 ret->value = xmlStrdup(value);
2945 if ((ctxt != NULL) && (ctxt->vstateNr != 0)) {
2947 * Operating in streaming mode, attr is gonna disapear
2949 ret->name = xmlStrdup(attr->name);
2955 ret->lineno = xmlGetLineNo(attr->parent);
2957 /* To add a reference :-
2958 * References are maintained as a list of references,
2959 * Lookup the entry, if no entry create new nodelist
2960 * Add the owning node to the NodeList
2964 if (NULL == (ref_list = xmlHashLookup(table, value))) {
2965 if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
2966 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2967 "xmlAddRef: Reference list creation failed!\n",
2971 if (xmlHashAddEntry(table, value, ref_list) < 0) {
2972 xmlListDelete(ref_list);
2973 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2974 "xmlAddRef: Reference list insertion failed!\n",
2979 if (xmlListAppend(ref_list, ret) != 0) {
2980 xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2981 "xmlAddRef: Reference list insertion failed!\n",
2988 if (ret->value != NULL)
2989 xmlFree((char *)ret->value);
2990 if (ret->name != NULL)
2991 xmlFree((char *)ret->name);
2999 * @table: An ref table
3001 * Deallocate the memory used by an Ref hash table.
3004 xmlFreeRefTable(xmlRefTablePtr table) {
3005 xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList);
3010 * @doc: the document
3011 * @elem: the element carrying the attribute
3012 * @attr: the attribute
3014 * Determine whether an attribute is of type Ref. In case we have DTD(s)
3015 * then this is simple, otherwise we use an heuristic: name Ref (upper
3018 * Returns 0 or 1 depending on the lookup result
3021 xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
3026 if (doc == NULL) return(0);
3029 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
3031 } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
3035 xmlAttributePtr attrDecl;
3037 if (elem == NULL) return(0);
3038 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
3039 if ((attrDecl == NULL) && (doc->extSubset != NULL))
3040 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
3041 elem->name, attr->name);
3043 if ((attrDecl != NULL) &&
3044 (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
3045 attrDecl->atype == XML_ATTRIBUTE_IDREFS))
3053 * @doc: the document
3054 * @attr: the attribute
3056 * Remove the given attribute from the Ref table maintained internally.
3058 * Returns -1 if the lookup failed and 0 otherwise
3061 xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
3062 xmlListPtr ref_list;
3063 xmlRefTablePtr table;
3065 xmlRemoveMemo target;
3067 if (doc == NULL) return(-1);
3068 if (attr == NULL) return(-1);
3070 table = (xmlRefTablePtr) doc->refs;
3074 ID = xmlNodeListGetString(doc, attr->children, 1);
3078 ref_list = xmlHashLookup(table, ID);
3079 if(ref_list == NULL) {
3084 /* At this point, ref_list refers to a list of references which
3085 * have the same key as the supplied attr. Our list of references
3086 * is ordered by reference address and we don't have that information
3087 * here to use when removing. We'll have to walk the list and
3088 * check for a matching attribute, when we find one stop the walk
3089 * and remove the entry.
3090 * The list is ordered by reference, so that means we don't have the
3091 * key. Passing the list and the reference to the walker means we
3092 * will have enough data to be able to remove the entry.
3094 target.l = ref_list;
3097 /* Remove the supplied attr from our list */
3098 xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
3100 /*If the list is empty then remove the list entry in the hash */
3101 if (xmlListEmpty(ref_list))
3102 xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator)
3110 * @doc: pointer to the document
3113 * Find the set of references for the supplied ID.
3115 * Returns NULL if not found, otherwise node set for the ID.
3118 xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
3119 xmlRefTablePtr table;
3129 table = (xmlRefTablePtr) doc->refs;
3133 return (xmlHashLookup(table, ID));
3136 /************************************************************************
3138 * Routines for validity checking *
3140 ************************************************************************/
3143 * xmlGetDtdElementDesc:
3144 * @dtd: a pointer to the DtD to search
3145 * @name: the element name
3147 * Search the DTD for the description of this element
3149 * returns the xmlElementPtr if found or NULL
3153 xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
3154 xmlElementTablePtr table;
3156 xmlChar *uqname = NULL, *prefix = NULL;
3158 if ((dtd == NULL) || (name == NULL)) return(NULL);
3159 if (dtd->elements == NULL)
3161 table = (xmlElementTablePtr) dtd->elements;
3163 uqname = xmlSplitQName2(name, &prefix);
3166 cur = xmlHashLookup2(table, name, prefix);
3167 if (prefix != NULL) xmlFree(prefix);
3168 if (uqname != NULL) xmlFree(uqname);
3172 * xmlGetDtdElementDesc2:
3173 * @dtd: a pointer to the DtD to search
3174 * @name: the element name
3175 * @create: create an empty description if not found
3177 * Search the DTD for the description of this element
3179 * returns the xmlElementPtr if found or NULL
3182 static xmlElementPtr
3183 xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) {
3184 xmlElementTablePtr table;
3186 xmlChar *uqname = NULL, *prefix = NULL;
3188 if (dtd == NULL) return(NULL);
3189 if (dtd->elements == NULL) {
3190 xmlDictPtr dict = NULL;
3192 if (dtd->doc != NULL)
3193 dict = dtd->doc->dict;
3198 * Create the Element table if needed.
3200 table = (xmlElementTablePtr) dtd->elements;
3201 if (table == NULL) {
3202 table = xmlHashCreateDict(0, dict);
3203 dtd->elements = (void *) table;
3205 if (table == NULL) {
3206 xmlVErrMemory(NULL, "element table allocation failed");
3210 table = (xmlElementTablePtr) dtd->elements;
3212 uqname = xmlSplitQName2(name, &prefix);
3215 cur = xmlHashLookup2(table, name, prefix);
3216 if ((cur == NULL) && (create)) {
3217 cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3219 xmlVErrMemory(NULL, "malloc failed");
3222 memset(cur, 0, sizeof(xmlElement));
3223 cur->type = XML_ELEMENT_DECL;
3226 * fill the structure.
3228 cur->name = xmlStrdup(name);
3229 cur->prefix = xmlStrdup(prefix);
3230 cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3232 xmlHashAddEntry2(table, name, prefix, cur);
3234 if (prefix != NULL) xmlFree(prefix);
3235 if (uqname != NULL) xmlFree(uqname);
3240 * xmlGetDtdQElementDesc:
3241 * @dtd: a pointer to the DtD to search
3242 * @name: the element name
3243 * @prefix: the element namespace prefix
3245 * Search the DTD for the description of this element
3247 * returns the xmlElementPtr if found or NULL
3251 xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3252 const xmlChar *prefix) {
3253 xmlElementTablePtr table;
3255 if (dtd == NULL) return(NULL);
3256 if (dtd->elements == NULL) return(NULL);
3257 table = (xmlElementTablePtr) dtd->elements;
3259 return(xmlHashLookup2(table, name, prefix));
3263 * xmlGetDtdAttrDesc:
3264 * @dtd: a pointer to the DtD to search
3265 * @elem: the element name
3266 * @name: the attribute name
3268 * Search the DTD for the description of this attribute on
3271 * returns the xmlAttributePtr if found or NULL
3275 xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3276 xmlAttributeTablePtr table;
3277 xmlAttributePtr cur;
3278 xmlChar *uqname = NULL, *prefix = NULL;
3280 if (dtd == NULL) return(NULL);
3281 if (dtd->attributes == NULL) return(NULL);
3283 table = (xmlAttributeTablePtr) dtd->attributes;
3287 uqname = xmlSplitQName2(name, &prefix);
3289 if (uqname != NULL) {
3290 cur = xmlHashLookup3(table, uqname, prefix, elem);
3291 if (prefix != NULL) xmlFree(prefix);
3292 if (uqname != NULL) xmlFree(uqname);
3294 cur = xmlHashLookup3(table, name, NULL, elem);
3299 * xmlGetDtdQAttrDesc:
3300 * @dtd: a pointer to the DtD to search
3301 * @elem: the element name
3302 * @name: the attribute name
3303 * @prefix: the attribute namespace prefix
3305 * Search the DTD for the description of this qualified attribute on
3308 * returns the xmlAttributePtr if found or NULL
3312 xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3313 const xmlChar *prefix) {
3314 xmlAttributeTablePtr table;
3316 if (dtd == NULL) return(NULL);
3317 if (dtd->attributes == NULL) return(NULL);
3318 table = (xmlAttributeTablePtr) dtd->attributes;
3320 return(xmlHashLookup3(table, name, prefix, elem));
3324 * xmlGetDtdNotationDesc:
3325 * @dtd: a pointer to the DtD to search
3326 * @name: the notation name
3328 * Search the DTD for the description of this notation
3330 * returns the xmlNotationPtr if found or NULL
3334 xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3335 xmlNotationTablePtr table;
3337 if (dtd == NULL) return(NULL);
3338 if (dtd->notations == NULL) return(NULL);
3339 table = (xmlNotationTablePtr) dtd->notations;
3341 return(xmlHashLookup(table, name));
3344 #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
3346 * xmlValidateNotationUse:
3347 * @ctxt: the validation context
3348 * @doc: the document
3349 * @notationName: the notation name to check
3351 * Validate that the given name match a notation declaration.
3352 * - [ VC: Notation Declared ]
3354 * returns 1 if valid or 0 otherwise
3358 xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3359 const xmlChar *notationName) {
3360 xmlNotationPtr notaDecl;
3361 if ((doc == NULL) || (doc->intSubset == NULL) ||
3362 (notationName == NULL)) return(-1);
3364 notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3365 if ((notaDecl == NULL) && (doc->extSubset != NULL))
3366 notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3368 if ((notaDecl == NULL) && (ctxt != NULL)) {
3369 xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3370 "NOTATION %s is not declared\n",
3371 notationName, NULL, NULL);
3376 #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
3379 * xmlIsMixedElement:
3380 * @doc: the document
3381 * @name: the element name
3383 * Search in the DtDs whether an element accept Mixed content (or ANY)
3384 * basically if it is supposed to accept text childs
3386 * returns 0 if no, 1 if yes, and -1 if no element description is available
3390 xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3391 xmlElementPtr elemDecl;
3393 if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3395 elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3396 if ((elemDecl == NULL) && (doc->extSubset != NULL))
3397 elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3398 if (elemDecl == NULL) return(-1);
3399 switch (elemDecl->etype) {
3400 case XML_ELEMENT_TYPE_UNDEFINED:
3402 case XML_ELEMENT_TYPE_ELEMENT:
3404 case XML_ELEMENT_TYPE_EMPTY:
3406 * return 1 for EMPTY since we want VC error to pop up
3407 * on <empty> </empty> for example
3409 case XML_ELEMENT_TYPE_ANY:
3410 case XML_ELEMENT_TYPE_MIXED:
3416 #ifdef LIBXML_VALID_ENABLED
3419 xmlIsDocNameStartChar(xmlDocPtr doc, int c) {
3420 if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3422 * Use the new checks of production [4] [4a] amd [5] of the
3423 * Update 5 of XML-1.0
3425 if (((c >= 'a') && (c <= 'z')) ||
3426 ((c >= 'A') && (c <= 'Z')) ||
3427 (c == '_') || (c == ':') ||
3428 ((c >= 0xC0) && (c <= 0xD6)) ||
3429 ((c >= 0xD8) && (c <= 0xF6)) ||
3430 ((c >= 0xF8) && (c <= 0x2FF)) ||
3431 ((c >= 0x370) && (c <= 0x37D)) ||
3432 ((c >= 0x37F) && (c <= 0x1FFF)) ||
3433 ((c >= 0x200C) && (c <= 0x200D)) ||
3434 ((c >= 0x2070) && (c <= 0x218F)) ||
3435 ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3436 ((c >= 0x3001) && (c <= 0xD7FF)) ||
3437 ((c >= 0xF900) && (c <= 0xFDCF)) ||
3438 ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3439 ((c >= 0x10000) && (c <= 0xEFFFF)))
3442 if (IS_LETTER(c) || (c == '_') || (c == ':'))
3449 xmlIsDocNameChar(xmlDocPtr doc, int c) {
3450 if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3452 * Use the new checks of production [4] [4a] amd [5] of the
3453 * Update 5 of XML-1.0
3455 if (((c >= 'a') && (c <= 'z')) ||
3456 ((c >= 'A') && (c <= 'Z')) ||
3457 ((c >= '0') && (c <= '9')) || /* !start */
3458 (c == '_') || (c == ':') ||
3459 (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
3460 ((c >= 0xC0) && (c <= 0xD6)) ||
3461 ((c >= 0xD8) && (c <= 0xF6)) ||
3462 ((c >= 0xF8) && (c <= 0x2FF)) ||
3463 ((c >= 0x300) && (c <= 0x36F)) || /* !start */
3464 ((c >= 0x370) && (c <= 0x37D)) ||
3465 ((c >= 0x37F) && (c <= 0x1FFF)) ||
3466 ((c >= 0x200C) && (c <= 0x200D)) ||
3467 ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
3468 ((c >= 0x2070) && (c <= 0x218F)) ||
3469 ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3470 ((c >= 0x3001) && (c <= 0xD7FF)) ||
3471 ((c >= 0xF900) && (c <= 0xFDCF)) ||
3472 ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3473 ((c >= 0x10000) && (c <= 0xEFFFF)))
3476 if ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
3477 (c == '.') || (c == '-') ||
3478 (c == '_') || (c == ':') ||
3479 (IS_COMBINING(c)) ||
3487 * xmlValidateNameValue:
3488 * @doc: pointer to the document or NULL
3489 * @value: an Name value
3491 * Validate that the given value match Name production
3493 * returns 1 if valid or 0 otherwise
3497 xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) {
3501 if (value == NULL) return(0);
3503 val = xmlStringCurrentChar(NULL, cur, &len);
3505 if (!xmlIsDocNameStartChar(doc, val))
3508 val = xmlStringCurrentChar(NULL, cur, &len);
3510 while (xmlIsDocNameChar(doc, val)) {
3511 val = xmlStringCurrentChar(NULL, cur, &len);
3515 if (val != 0) return(0);
3521 * xmlValidateNameValue:
3522 * @value: an Name value
3524 * Validate that the given value match Name production
3526 * returns 1 if valid or 0 otherwise
3530 xmlValidateNameValue(const xmlChar *value) {
3531 return(xmlValidateNameValueInternal(NULL, value));
3535 * xmlValidateNamesValueInternal:
3536 * @doc: pointer to the document or NULL
3537 * @value: an Names value
3539 * Validate that the given value match Names production
3541 * returns 1 if valid or 0 otherwise
3545 xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) {
3549 if (value == NULL) return(0);
3551 val = xmlStringCurrentChar(NULL, cur, &len);
3554 if (!xmlIsDocNameStartChar(doc, val))
3557 val = xmlStringCurrentChar(NULL, cur, &len);
3559 while (xmlIsDocNameChar(doc, val)) {
3560 val = xmlStringCurrentChar(NULL, cur, &len);
3564 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3565 while (val == 0x20) {
3566 while (val == 0x20) {
3567 val = xmlStringCurrentChar(NULL, cur, &len);
3571 if (!xmlIsDocNameStartChar(doc, val))
3574 val = xmlStringCurrentChar(NULL, cur, &len);
3577 while (xmlIsDocNameChar(doc, val)) {
3578 val = xmlStringCurrentChar(NULL, cur, &len);
3583 if (val != 0) return(0);
3589 * xmlValidateNamesValue:
3590 * @value: an Names value
3592 * Validate that the given value match Names production
3594 * returns 1 if valid or 0 otherwise
3598 xmlValidateNamesValue(const xmlChar *value) {
3599 return(xmlValidateNamesValueInternal(NULL, value));
3603 * xmlValidateNmtokenValueInternal:
3604 * @doc: pointer to the document or NULL
3605 * @value: an Nmtoken value
3607 * Validate that the given value match Nmtoken production
3609 * [ VC: Name Token ]
3611 * returns 1 if valid or 0 otherwise
3615 xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) {
3619 if (value == NULL) return(0);
3621 val = xmlStringCurrentChar(NULL, cur, &len);
3624 if (!xmlIsDocNameChar(doc, val))
3627 val = xmlStringCurrentChar(NULL, cur, &len);
3629 while (xmlIsDocNameChar(doc, val)) {
3630 val = xmlStringCurrentChar(NULL, cur, &len);
3634 if (val != 0) return(0);
3640 * xmlValidateNmtokenValue:
3641 * @value: an Nmtoken value
3643 * Validate that the given value match Nmtoken production
3645 * [ VC: Name Token ]
3647 * returns 1 if valid or 0 otherwise
3651 xmlValidateNmtokenValue(const xmlChar *value) {
3652 return(xmlValidateNmtokenValueInternal(NULL, value));
3656 * xmlValidateNmtokensValueInternal:
3657 * @doc: pointer to the document or NULL
3658 * @value: an Nmtokens value
3660 * Validate that the given value match Nmtokens production
3662 * [ VC: Name Token ]
3664 * returns 1 if valid or 0 otherwise
3668 xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) {
3672 if (value == NULL) return(0);
3674 val = xmlStringCurrentChar(NULL, cur, &len);
3677 while (IS_BLANK(val)) {
3678 val = xmlStringCurrentChar(NULL, cur, &len);
3682 if (!xmlIsDocNameChar(doc, val))
3685 while (xmlIsDocNameChar(doc, val)) {
3686 val = xmlStringCurrentChar(NULL, cur, &len);
3690 /* Should not test IS_BLANK(val) here -- see erratum E20*/
3691 while (val == 0x20) {
3692 while (val == 0x20) {
3693 val = xmlStringCurrentChar(NULL, cur, &len);
3696 if (val == 0) return(1);
3698 if (!xmlIsDocNameChar(doc, val))
3701 val = xmlStringCurrentChar(NULL, cur, &len);
3704 while (xmlIsDocNameChar(doc, val)) {
3705 val = xmlStringCurrentChar(NULL, cur, &len);
3710 if (val != 0) return(0);
3716 * xmlValidateNmtokensValue:
3717 * @value: an Nmtokens value
3719 * Validate that the given value match Nmtokens production
3721 * [ VC: Name Token ]
3723 * returns 1 if valid or 0 otherwise
3727 xmlValidateNmtokensValue(const xmlChar *value) {
3728 return(xmlValidateNmtokensValueInternal(NULL, value));
3732 * xmlValidateNotationDecl:
3733 * @ctxt: the validation context
3734 * @doc: a document instance
3735 * @nota: a notation definition
3737 * Try to validate a single notation definition
3738 * basically it does the following checks as described by the
3739 * XML-1.0 recommendation:
3740 * - it seems that no validity constraint exists on notation declarations
3741 * But this function get called anyway ...
3743 * returns 1 if valid or 0 otherwise
3747 xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3748 xmlNotationPtr nota ATTRIBUTE_UNUSED) {
3755 * xmlValidateAttributeValueInternal:
3756 * @doc: the document
3757 * @type: an attribute type
3758 * @value: an attribute value
3760 * Validate that the given attribute value match the proper production
3762 * returns 1 if valid or 0 otherwise
3766 xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
3767 const xmlChar *value) {
3769 case XML_ATTRIBUTE_ENTITIES:
3770 case XML_ATTRIBUTE_IDREFS:
3771 return(xmlValidateNamesValueInternal(doc, value));
3772 case XML_ATTRIBUTE_ENTITY:
3773 case XML_ATTRIBUTE_IDREF:
3774 case XML_ATTRIBUTE_ID:
3775 case XML_ATTRIBUTE_NOTATION:
3776 return(xmlValidateNameValueInternal(doc, value));
3777 case XML_ATTRIBUTE_NMTOKENS:
3778 case XML_ATTRIBUTE_ENUMERATION:
3779 return(xmlValidateNmtokensValueInternal(doc, value));
3780 case XML_ATTRIBUTE_NMTOKEN:
3781 return(xmlValidateNmtokenValueInternal(doc, value));
3782 case XML_ATTRIBUTE_CDATA:
3789 * xmlValidateAttributeValue:
3790 * @type: an attribute type
3791 * @value: an attribute value
3793 * Validate that the given attribute value match the proper production
3796 * Values of type ID must match the Name production....
3799 * Values of type IDREF must match the Name production, and values
3800 * of type IDREFS must match Names ...
3802 * [ VC: Entity Name ]
3803 * Values of type ENTITY must match the Name production, values
3804 * of type ENTITIES must match Names ...
3806 * [ VC: Name Token ]
3807 * Values of type NMTOKEN must match the Nmtoken production; values
3808 * of type NMTOKENS must match Nmtokens.
3810 * returns 1 if valid or 0 otherwise
3813 xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3814 return(xmlValidateAttributeValueInternal(NULL, type, value));
3818 * xmlValidateAttributeValue2:
3819 * @ctxt: the validation context
3820 * @doc: the document
3821 * @name: the attribute name (used for error reporting only)
3822 * @type: the attribute type
3823 * @value: the attribute value
3825 * Validate that the given attribute value match a given type.
3826 * This typically cannot be done before having finished parsing
3830 * Values of type IDREF must match one of the declared IDs
3831 * Values of type IDREFS must match a sequence of the declared IDs
3832 * each Name must match the value of an ID attribute on some element
3833 * in the XML document; i.e. IDREF values must match the value of
3836 * [ VC: Entity Name ]
3837 * Values of type ENTITY must match one declared entity
3838 * Values of type ENTITIES must match a sequence of declared entities
3840 * [ VC: Notation Attributes ]
3841 * all notation names in the declaration must be declared.
3843 * returns 1 if valid or 0 otherwise
3847 xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3848 const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3851 case XML_ATTRIBUTE_IDREFS:
3852 case XML_ATTRIBUTE_IDREF:
3853 case XML_ATTRIBUTE_ID:
3854 case XML_ATTRIBUTE_NMTOKENS:
3855 case XML_ATTRIBUTE_ENUMERATION:
3856 case XML_ATTRIBUTE_NMTOKEN:
3857 case XML_ATTRIBUTE_CDATA:
3859 case XML_ATTRIBUTE_ENTITY: {
3862 ent = xmlGetDocEntity(doc, value);
3863 /* yeah it's a bit messy... */
3864 if ((ent == NULL) && (doc->standalone == 1)) {
3865 doc->standalone = 0;
3866 ent = xmlGetDocEntity(doc, value);
3869 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3870 XML_DTD_UNKNOWN_ENTITY,
3871 "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3874 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3875 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3876 XML_DTD_ENTITY_TYPE,
3877 "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3883 case XML_ATTRIBUTE_ENTITIES: {
3884 xmlChar *dup, *nam = NULL, *cur, save;
3887 dup = xmlStrdup(value);
3893 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
3896 ent = xmlGetDocEntity(doc, nam);
3898 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3899 XML_DTD_UNKNOWN_ENTITY,
3900 "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3903 } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3904 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3905 XML_DTD_ENTITY_TYPE,
3906 "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3913 while (IS_BLANK_CH(*cur)) cur++;
3918 case XML_ATTRIBUTE_NOTATION: {
3919 xmlNotationPtr nota;
3921 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3922 if ((nota == NULL) && (doc->extSubset != NULL))
3923 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3926 xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3927 XML_DTD_UNKNOWN_NOTATION,
3928 "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3939 * xmlValidCtxtNormalizeAttributeValue:
3940 * @ctxt: the validation context
3941 * @doc: the document
3943 * @name: the attribute name
3944 * @value: the attribute value
3945 * @ctxt: the validation context or NULL
3947 * Does the validation related extra step of the normalization of attribute
3950 * If the declared value is not CDATA, then the XML processor must further
3951 * process the normalized attribute value by discarding any leading and
3952 * trailing space (#x20) characters, and by replacing sequences of space
3953 * (#x20) characters by single space (#x20) character.
3955 * Also check VC: Standalone Document Declaration in P32, and update
3956 * ctxt->valid accordingly
3958 * returns a new normalized string if normalization is needed, NULL otherwise
3959 * the caller must free the returned value.
3963 xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3964 xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3967 xmlAttributePtr attrDecl = NULL;
3970 if (doc == NULL) return(NULL);
3971 if (elem == NULL) return(NULL);
3972 if (name == NULL) return(NULL);
3973 if (value == NULL) return(NULL);
3975 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3979 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3980 if (fullname == NULL)
3982 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
3983 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3984 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3985 if (attrDecl != NULL)
3988 if ((fullname != fn) && (fullname != elem->name))
3991 if ((attrDecl == NULL) && (doc->intSubset != NULL))
3992 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3993 if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3994 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3995 if (attrDecl != NULL)
3999 if (attrDecl == NULL)
4001 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
4004 ret = xmlStrdup(value);
4009 while (*src == 0x20) src++;
4012 while (*src == 0x20) src++;
4020 if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
4021 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
4022 "standalone: %s on %s value had to be normalized based on external subset declaration\n",
4023 name, elem->name, NULL);
4030 * xmlValidNormalizeAttributeValue:
4031 * @doc: the document
4033 * @name: the attribute name
4034 * @value: the attribute value
4036 * Does the validation related extra step of the normalization of attribute
4039 * If the declared value is not CDATA, then the XML processor must further
4040 * process the normalized attribute value by discarding any leading and
4041 * trailing space (#x20) characters, and by replacing sequences of space
4042 * (#x20) characters by single space (#x20) character.
4044 * Returns a new normalized string if normalization is needed, NULL otherwise
4045 * the caller must free the returned value.
4049 xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
4050 const xmlChar *name, const xmlChar *value) {
4053 xmlAttributePtr attrDecl = NULL;
4055 if (doc == NULL) return(NULL);
4056 if (elem == NULL) return(NULL);
4057 if (name == NULL) return(NULL);
4058 if (value == NULL) return(NULL);
4060 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4064 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4065 if (fullname == NULL)
4067 if ((fullname != fn) && (fullname != elem->name))
4070 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
4071 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4072 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
4074 if (attrDecl == NULL)
4076 if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
4079 ret = xmlStrdup(value);
4084 while (*src == 0x20) src++;
4087 while (*src == 0x20) src++;
4099 xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count,
4100 const xmlChar* name ATTRIBUTE_UNUSED) {
4101 if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
4105 * xmlValidateAttributeDecl:
4106 * @ctxt: the validation context
4107 * @doc: a document instance
4108 * @attr: an attribute definition
4110 * Try to validate a single attribute definition
4111 * basically it does the following checks as described by the
4112 * XML-1.0 recommendation:
4113 * - [ VC: Attribute Default Legal ]
4114 * - [ VC: Enumeration ]
4115 * - [ VC: ID Attribute Default ]
4117 * The ID/IDREF uniqueness and matching are done separately
4119 * returns 1 if valid or 0 otherwise
4123 xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4124 xmlAttributePtr attr) {
4128 if(attr == NULL) return(1);
4130 /* Attribute Default Legal */
4132 if (attr->defaultValue != NULL) {
4133 val = xmlValidateAttributeValueInternal(doc, attr->atype,
4134 attr->defaultValue);
4136 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
4137 "Syntax of default value for attribute %s of %s is not valid\n",
4138 attr->name, attr->elem, NULL);
4143 /* ID Attribute Default */
4144 if ((attr->atype == XML_ATTRIBUTE_ID)&&
4145 (attr->def != XML_ATTRIBUTE_IMPLIED) &&
4146 (attr->def != XML_ATTRIBUTE_REQUIRED)) {
4147 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
4148 "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
4149 attr->name, attr->elem, NULL);
4153 /* One ID per Element Type */
4154 if (attr->atype == XML_ATTRIBUTE_ID) {
4157 /* the trick is that we parse DtD as their own internal subset */
4158 xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
4161 nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
4163 xmlAttributeTablePtr table;
4166 * The attribute may be declared in the internal subset and the
4167 * element in the external subset.
4170 if (doc->intSubset != NULL) {
4171 table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4172 xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner)
4173 xmlValidateAttributeIdCallback, &nbId);
4178 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4179 "Element %s has %d ID attribute defined in the internal subset : %s\n",
4180 attr->elem, nbId, attr->name);
4181 } else if (doc->extSubset != NULL) {
4183 elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
4185 extId = xmlScanIDAttributeDecl(NULL, elem, 0);
4188 xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4189 "Element %s has %d ID attribute defined in the external subset : %s\n",
4190 attr->elem, extId, attr->name);
4191 } else if (extId + nbId > 1) {
4192 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4193 "Element %s has ID attributes defined in the internal and external subset : %s\n",
4194 attr->elem, attr->name, NULL);
4199 /* Validity Constraint: Enumeration */
4200 if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4201 xmlEnumerationPtr tree = attr->tree;
4202 while (tree != NULL) {
4203 if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4207 xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
4208 "Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4209 attr->defaultValue, attr->name, attr->elem);
4218 * xmlValidateElementDecl:
4219 * @ctxt: the validation context
4220 * @doc: a document instance
4221 * @elem: an element definition
4223 * Try to validate a single element definition
4224 * basically it does the following checks as described by the
4225 * XML-1.0 recommendation:
4226 * - [ VC: One ID per Element Type ]
4227 * - [ VC: No Duplicate Types ]
4228 * - [ VC: Unique Element Type Declaration ]
4230 * returns 1 if valid or 0 otherwise
4234 xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4235 xmlElementPtr elem) {
4241 if (elem == NULL) return(1);
4244 #ifdef LIBXML_REGEXP_ENABLED
4245 /* Build the regexp associated to the content model */
4246 ret = xmlValidBuildContentModel(ctxt, elem);
4250 /* No Duplicate Types */
4251 if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4252 xmlElementContentPtr cur, next;
4253 const xmlChar *name;
4255 cur = elem->content;
4256 while (cur != NULL) {
4257 if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4258 if (cur->c1 == NULL) break;
4259 if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4260 name = cur->c1->name;
4262 while (next != NULL) {
4263 if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
4264 if ((xmlStrEqual(next->name, name)) &&
4265 (xmlStrEqual(next->prefix, cur->c1->prefix))) {
4266 if (cur->c1->prefix == NULL) {
4267 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4268 "Definition of %s has duplicate references of %s\n",
4269 elem->name, name, NULL);
4271 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4272 "Definition of %s has duplicate references of %s:%s\n",
4273 elem->name, cur->c1->prefix, name);
4279 if (next->c1 == NULL) break;
4280 if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
4281 if ((xmlStrEqual(next->c1->name, name)) &&
4282 (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) {
4283 if (cur->c1->prefix == NULL) {
4284 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4285 "Definition of %s has duplicate references to %s\n",
4286 elem->name, name, NULL);
4288 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4289 "Definition of %s has duplicate references to %s:%s\n",
4290 elem->name, cur->c1->prefix, name);
4301 /* VC: Unique Element Type Declaration */
4302 tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4303 if ((tst != NULL ) && (tst != elem) &&
4304 ((tst->prefix == elem->prefix) ||
4305 (xmlStrEqual(tst->prefix, elem->prefix))) &&
4306 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
4307 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4308 "Redefinition of element %s\n",
4309 elem->name, NULL, NULL);
4312 tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4313 if ((tst != NULL ) && (tst != elem) &&
4314 ((tst->prefix == elem->prefix) ||
4315 (xmlStrEqual(tst->prefix, elem->prefix))) &&
4316 (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
4317 xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4318 "Redefinition of element %s\n",
4319 elem->name, NULL, NULL);
4322 /* One ID per Element Type
4323 * already done when registering the attribute
4324 if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4331 * xmlValidateOneAttribute:
4332 * @ctxt: the validation context
4333 * @doc: a document instance
4334 * @elem: an element instance
4335 * @attr: an attribute instance
4336 * @value: the attribute value (without entities processing)
4338 * Try to validate a single attribute for an element
4339 * basically it does the following checks as described by the
4340 * XML-1.0 recommendation:
4341 * - [ VC: Attribute Value Type ]
4342 * - [ VC: Fixed Attribute Default ]
4343 * - [ VC: Entity Name ]
4344 * - [ VC: Name Token ]
4347 * - [ VC: Entity Name ]
4348 * - [ VC: Notation Attributes ]
4350 * The ID/IDREF uniqueness and matching are done separately
4352 * returns 1 if valid or 0 otherwise
4356 xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4357 xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4359 xmlAttributePtr attrDecl = NULL;
4364 if ((elem == NULL) || (elem->name == NULL)) return(0);
4365 if ((attr == NULL) || (attr->name == NULL)) return(0);
4367 if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4371 fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4372 if (fullname == NULL)
4374 if (attr->ns != NULL) {
4375 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4376 attr->name, attr->ns->prefix);
4377 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4378 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4379 attr->name, attr->ns->prefix);
4381 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
4382 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4383 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4384 fullname, attr->name);
4386 if ((fullname != fn) && (fullname != elem->name))
4389 if (attrDecl == NULL) {
4390 if (attr->ns != NULL) {
4391 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4392 attr->name, attr->ns->prefix);
4393 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4394 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4395 attr->name, attr->ns->prefix);
4397 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4398 elem->name, attr->name);
4399 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4400 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4401 elem->name, attr->name);
4406 /* Validity Constraint: Attribute Value Type */
4407 if (attrDecl == NULL) {
4408 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4409 "No declaration for attribute %s of element %s\n",
4410 attr->name, elem->name, NULL);
4413 attr->atype = attrDecl->atype;
4415 val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4417 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4418 "Syntax of value for attribute %s of %s is not valid\n",
4419 attr->name, elem->name, NULL);
4423 /* Validity constraint: Fixed Attribute Default */
4424 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4425 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4426 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4427 "Value for attribute %s of %s is different from default \"%s\"\n",
4428 attr->name, elem->name, attrDecl->defaultValue);
4433 /* Validity Constraint: ID uniqueness */
4434 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4435 if (xmlAddID(ctxt, doc, value, attr) == NULL)
4439 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4440 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4441 if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4445 /* Validity Constraint: Notation Attributes */
4446 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4447 xmlEnumerationPtr tree = attrDecl->tree;
4448 xmlNotationPtr nota;
4450 /* First check that the given NOTATION was declared */
4451 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4453 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4456 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4457 "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
4458 value, attr->name, elem->name);
4462 /* Second, verify that it's among the list */
4463 while (tree != NULL) {
4464 if (xmlStrEqual(tree->name, value)) break;
4468 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4469 "Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
4470 value, attr->name, elem->name);
4475 /* Validity Constraint: Enumeration */
4476 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4477 xmlEnumerationPtr tree = attrDecl->tree;
4478 while (tree != NULL) {
4479 if (xmlStrEqual(tree->name, value)) break;
4483 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4484 "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4485 value, attr->name, elem->name);
4490 /* Fixed Attribute Default */
4491 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4492 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4493 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4494 "Value for attribute %s of %s must be \"%s\"\n",
4495 attr->name, elem->name, attrDecl->defaultValue);
4499 /* Extra check for the attribute value */
4500 ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4501 attrDecl->atype, value);
4507 * xmlValidateOneNamespace:
4508 * @ctxt: the validation context
4509 * @doc: a document instance
4510 * @elem: an element instance
4511 * @prefix: the namespace prefix
4512 * @ns: an namespace declaration instance
4513 * @value: the attribute value (without entities processing)
4515 * Try to validate a single namespace declaration for an element
4516 * basically it does the following checks as described by the
4517 * XML-1.0 recommendation:
4518 * - [ VC: Attribute Value Type ]
4519 * - [ VC: Fixed Attribute Default ]
4520 * - [ VC: Entity Name ]
4521 * - [ VC: Name Token ]
4524 * - [ VC: Entity Name ]
4525 * - [ VC: Notation Attributes ]
4527 * The ID/IDREF uniqueness and matching are done separately
4529 * returns 1 if valid or 0 otherwise
4533 xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4534 xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4535 /* xmlElementPtr elemDecl; */
4536 xmlAttributePtr attrDecl = NULL;
4541 if ((elem == NULL) || (elem->name == NULL)) return(0);
4542 if ((ns == NULL) || (ns->href == NULL)) return(0);
4544 if (prefix != NULL) {
4548 fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4549 if (fullname == NULL) {
4550 xmlVErrMemory(ctxt, "Validating namespace");
4553 if (ns->prefix != NULL) {
4554 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4555 ns->prefix, BAD_CAST "xmlns");
4556 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4557 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4558 ns->prefix, BAD_CAST "xmlns");
4560 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
4562 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4563 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
4566 if ((fullname != fn) && (fullname != elem->name))
4569 if (attrDecl == NULL) {
4570 if (ns->prefix != NULL) {
4571 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4572 ns->prefix, BAD_CAST "xmlns");
4573 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4574 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4575 ns->prefix, BAD_CAST "xmlns");
4577 attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4578 elem->name, BAD_CAST "xmlns");
4579 if ((attrDecl == NULL) && (doc->extSubset != NULL))
4580 attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4581 elem->name, BAD_CAST "xmlns");
4586 /* Validity Constraint: Attribute Value Type */
4587 if (attrDecl == NULL) {
4588 if (ns->prefix != NULL) {
4589 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4590 "No declaration for attribute xmlns:%s of element %s\n",
4591 ns->prefix, elem->name, NULL);
4593 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4594 "No declaration for attribute xmlns of element %s\n",
4595 elem->name, NULL, NULL);
4600 val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4602 if (ns->prefix != NULL) {
4603 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4604 "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4605 ns->prefix, elem->name, NULL);
4607 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4608 "Syntax of value for attribute xmlns of %s is not valid\n",
4609 elem->name, NULL, NULL);
4614 /* Validity constraint: Fixed Attribute Default */
4615 if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4616 if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4617 if (ns->prefix != NULL) {
4618 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4619 "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4620 ns->prefix, elem->name, attrDecl->defaultValue);
4622 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4623 "Value for attribute xmlns of %s is different from default \"%s\"\n",
4624 elem->name, attrDecl->defaultValue, NULL);
4631 * Casting ns to xmlAttrPtr is wrong. We'd need separate functions
4632 * xmlAddID and xmlAddRef for namespace declarations, but it makes
4633 * no practical sense to use ID types anyway.
4636 /* Validity Constraint: ID uniqueness */
4637 if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4638 if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4642 if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4643 (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4644 if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4649 /* Validity Constraint: Notation Attributes */
4650 if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4651 xmlEnumerationPtr tree = attrDecl->tree;
4652 xmlNotationPtr nota;
4654 /* First check that the given NOTATION was declared */
4655 nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4657 nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4660 if (ns->prefix != NULL) {
4661 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4662 "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4663 value, ns->prefix, elem->name);
4665 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4666 "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4667 value, elem->name, NULL);
4672 /* Second, verify that it's among the list */
4673 while (tree != NULL) {
4674 if (xmlStrEqual(tree->name, value)) break;
4678 if (ns->prefix != NULL) {
4679 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4680 "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4681 value, ns->prefix, elem->name);
4683 xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4684 "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4685 value, elem->name, NULL);
4691 /* Validity Constraint: Enumeration */
4692 if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4693 xmlEnumerationPtr tree = attrDecl->tree;
4694 while (tree != NULL) {
4695 if (xmlStrEqual(tree->name, value)) break;
4699 if (ns->prefix != NULL) {
4700 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4701 "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4702 value, ns->prefix, elem->name);
4704 xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4705 "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4706 value, elem->name, NULL);
4712 /* Fixed Attribute Default */
4713 if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4714 (!xmlStrEqual(attrDecl->defaultValue, value))) {
4715 if (ns->prefix != NULL) {
4716 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4717 "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4718 ns->prefix, elem->name, attrDecl->defaultValue);
4720 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4721 "Value for attribute xmlns of %s must be \"%s\"\n",
4722 elem->name, attrDecl->defaultValue, NULL);
4727 /* Extra check for the attribute value */
4728 if (ns->prefix != NULL) {
4729 ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4730 attrDecl->atype, value);
4732 ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4733 attrDecl->atype, value);
4739 #ifndef LIBXML_REGEXP_ENABLED
4741 * xmlValidateSkipIgnorable:
4742 * @ctxt: the validation context
4743 * @child: the child list
4745 * Skip ignorable elements w.r.t. the validation process
4747 * returns the first element to consider for validation of the content model
4751 xmlValidateSkipIgnorable(xmlNodePtr child) {
4752 while (child != NULL) {
4753 switch (child->type) {
4754 /* These things are ignored (skipped) during validation. */
4756 case XML_COMMENT_NODE:
4757 case XML_XINCLUDE_START:
4758 case XML_XINCLUDE_END:
4759 child = child->next;
4762 if (xmlIsBlankNode(child))
4763 child = child->next;
4767 /* keep current node */
4776 * xmlValidateElementType:
4777 * @ctxt: the validation context
4779 * Try to validate the content model of an element internal function
4781 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4782 * reference is found and -3 if the validation succeeded but
4783 * the content model is not determinist.
4787 xmlValidateElementType(xmlValidCtxtPtr ctxt) {
4789 int determinist = 1;
4791 NODE = xmlValidateSkipIgnorable(NODE);
4792 if ((NODE == NULL) && (CONT == NULL))
4794 if ((NODE == NULL) &&
4795 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4796 (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4799 if (CONT == NULL) return(-1);
4800 if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
4804 * We arrive here when more states need to be examined
4809 * We just recovered from a rollback generated by a possible
4810 * epsilon transition, go directly to the analysis phase
4812 if (STATE == ROLLBACK_PARENT) {
4813 DEBUG_VALID_MSG("restored parent branch");
4814 DEBUG_VALID_STATE(NODE, CONT)
4819 DEBUG_VALID_STATE(NODE, CONT)
4821 * we may have to save a backup state here. This is the equivalent
4822 * of handling epsilon transition in NFAs.
4824 if ((CONT != NULL) &&
4825 ((CONT->parent == NULL) ||
4826 (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
4827 ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4828 (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
4829 ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
4830 DEBUG_VALID_MSG("saving parent branch");
4831 if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4837 * Check first if the content matches
4839 switch (CONT->type) {
4840 case XML_ELEMENT_CONTENT_PCDATA:
4842 DEBUG_VALID_MSG("pcdata failed no node");
4846 if (NODE->type == XML_TEXT_NODE) {
4847 DEBUG_VALID_MSG("pcdata found, skip to next");
4849 * go to next element in the content model
4850 * skipping ignorable elems
4854 NODE = xmlValidateSkipIgnorable(NODE);
4855 if ((NODE != NULL) &&
4856 (NODE->type == XML_ENTITY_REF_NODE))
4858 } while ((NODE != NULL) &&
4859 ((NODE->type != XML_ELEMENT_NODE) &&
4860 (NODE->type != XML_TEXT_NODE) &&
4861 (NODE->type != XML_CDATA_SECTION_NODE)));
4865 DEBUG_VALID_MSG("pcdata failed");
4870 case XML_ELEMENT_CONTENT_ELEMENT:
4872 DEBUG_VALID_MSG("element failed no node");
4876 ret = ((NODE->type == XML_ELEMENT_NODE) &&
4877 (xmlStrEqual(NODE->name, CONT->name)));
4879 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4880 ret = (CONT->prefix == NULL);
4881 } else if (CONT->prefix == NULL) {
4884 ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4888 DEBUG_VALID_MSG("element found, skip to next");
4890 * go to next element in the content model
4891 * skipping ignorable elems
4895 NODE = xmlValidateSkipIgnorable(NODE);
4896 if ((NODE != NULL) &&
4897 (NODE->type == XML_ENTITY_REF_NODE))
4899 } while ((NODE != NULL) &&
4900 ((NODE->type != XML_ELEMENT_NODE) &&
4901 (NODE->type != XML_TEXT_NODE) &&
4902 (NODE->type != XML_CDATA_SECTION_NODE)));
4904 DEBUG_VALID_MSG("element failed");
4909 case XML_ELEMENT_CONTENT_OR:
4911 * Small optimization.
4913 if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4914 if ((NODE == NULL) ||
4915 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4920 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4921 ret = (CONT->c1->prefix == NULL);
4922 } else if (CONT->c1->prefix == NULL) {
4925 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4935 * save the second branch 'or' branch
4937 DEBUG_VALID_MSG("saving 'or' branch");
4938 if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1),
4939 OCCURS, ROLLBACK_OR) < 0)
4944 case XML_ELEMENT_CONTENT_SEQ:
4946 * Small optimization.
4948 if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4949 ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4950 (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4951 if ((NODE == NULL) ||
4952 (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4957 if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4958 ret = (CONT->c1->prefix == NULL);
4959 } else if (CONT->c1->prefix == NULL) {
4962 ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4976 * At this point handle going up in the tree
4979 DEBUG_VALID_MSG("error found returning");
4983 while (CONT != NULL) {
4985 * First do the analysis depending on the occurrence model at
4989 switch (CONT->ocur) {
4992 case XML_ELEMENT_CONTENT_ONCE:
4993 cur = ctxt->vstate->node;
4994 DEBUG_VALID_MSG("Once branch failed, rollback");
4995 if (vstateVPop(ctxt) < 0 ) {
4996 DEBUG_VALID_MSG("exhaustion, failed");
4999 if (cur != ctxt->vstate->node)
5002 case XML_ELEMENT_CONTENT_PLUS:
5003 if (OCCURRENCE == 0) {
5004 cur = ctxt->vstate->node;
5005 DEBUG_VALID_MSG("Plus branch failed, rollback");
5006 if (vstateVPop(ctxt) < 0 ) {
5007 DEBUG_VALID_MSG("exhaustion, failed");
5010 if (cur != ctxt->vstate->node)
5014 DEBUG_VALID_MSG("Plus branch found");
5017 case XML_ELEMENT_CONTENT_MULT:
5018 #ifdef DEBUG_VALID_ALGO
5019 if (OCCURRENCE == 0) {
5020 DEBUG_VALID_MSG("Mult branch failed");
5022 DEBUG_VALID_MSG("Mult branch found");
5027 case XML_ELEMENT_CONTENT_OPT:
5028 DEBUG_VALID_MSG("Option branch failed");
5033 switch (CONT->ocur) {
5034 case XML_ELEMENT_CONTENT_OPT:
5035 DEBUG_VALID_MSG("Option branch succeeded");
5038 case XML_ELEMENT_CONTENT_ONCE:
5039 DEBUG_VALID_MSG("Once branch succeeded");
5042 case XML_ELEMENT_CONTENT_PLUS:
5043 if (STATE == ROLLBACK_PARENT) {
5044 DEBUG_VALID_MSG("Plus branch rollback");
5049 DEBUG_VALID_MSG("Plus branch exhausted");
5053 DEBUG_VALID_MSG("Plus branch succeeded, continuing");
5056 case XML_ELEMENT_CONTENT_MULT:
5057 if (STATE == ROLLBACK_PARENT) {
5058 DEBUG_VALID_MSG("Mult branch rollback");
5063 DEBUG_VALID_MSG("Mult branch exhausted");
5067 DEBUG_VALID_MSG("Mult branch succeeded, continuing");
5068 /* SET_OCCURRENCE; */
5075 * Then act accordingly at the parent level
5078 if (CONT->parent == NULL)
5081 switch (CONT->parent->type) {
5082 case XML_ELEMENT_CONTENT_PCDATA:
5083 DEBUG_VALID_MSG("Error: parent pcdata");
5085 case XML_ELEMENT_CONTENT_ELEMENT:
5086 DEBUG_VALID_MSG("Error: parent element");
5088 case XML_ELEMENT_CONTENT_OR:
5090 DEBUG_VALID_MSG("Or succeeded");
5091 CONT = CONT->parent;
5094 DEBUG_VALID_MSG("Or failed");
5095 CONT = CONT->parent;
5099 case XML_ELEMENT_CONTENT_SEQ:
5101 DEBUG_VALID_MSG("Sequence failed");
5102 CONT = CONT->parent;
5104 } else if (CONT == CONT->parent->c1) {
5105 DEBUG_VALID_MSG("Sequence testing 2nd branch");
5106 CONT = CONT->parent->c2;
5109 DEBUG_VALID_MSG("Sequence succeeded");
5110 CONT = CONT->parent;
5118 cur = ctxt->vstate->node;
5119 DEBUG_VALID_MSG("Failed, remaining input, rollback");
5120 if (vstateVPop(ctxt) < 0 ) {
5121 DEBUG_VALID_MSG("exhaustion, failed");
5124 if (cur != ctxt->vstate->node)
5131 cur = ctxt->vstate->node;
5132 DEBUG_VALID_MSG("Failure, rollback");
5133 if (vstateVPop(ctxt) < 0 ) {
5134 DEBUG_VALID_MSG("exhaustion, failed");
5137 if (cur != ctxt->vstate->node)
5141 return(determinist);
5146 * xmlSnprintfElements:
5147 * @buf: an output buffer
5148 * @size: the size of the buffer
5149 * @content: An element
5150 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
5152 * This will dump the list of elements to the buffer
5153 * Intended just for the debug routine
5156 xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
5160 if (node == NULL) return;
5161 if (glob) strcat(buf, "(");
5163 while (cur != NULL) {
5165 if (size - len < 50) {
5166 if ((size - len > 4) && (buf[len - 1] != '.'))
5167 strcat(buf, " ...");
5170 switch (cur->type) {
5171 case XML_ELEMENT_NODE:
5172 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5173 if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
5174 if ((size - len > 4) && (buf[len - 1] != '.'))
5175 strcat(buf, " ...");
5178 strcat(buf, (char *) cur->ns->prefix);
5181 if (size - len < xmlStrlen(cur->name) + 10) {
5182 if ((size - len > 4) && (buf[len - 1] != '.'))
5183 strcat(buf, " ...");
5186 strcat(buf, (char *) cur->name);
5187 if (cur->next != NULL)
5191 if (xmlIsBlankNode(cur))
5193 /* Falls through. */
5194 case XML_CDATA_SECTION_NODE:
5195 case XML_ENTITY_REF_NODE:
5196 strcat(buf, "CDATA");
5197 if (cur->next != NULL)
5200 case XML_ATTRIBUTE_NODE:
5201 case XML_DOCUMENT_NODE:
5202 #ifdef LIBXML_DOCB_ENABLED
5203 case XML_DOCB_DOCUMENT_NODE:
5205 case XML_HTML_DOCUMENT_NODE:
5206 case XML_DOCUMENT_TYPE_NODE:
5207 case XML_DOCUMENT_FRAG_NODE:
5208 case XML_NOTATION_NODE:
5209 case XML_NAMESPACE_DECL:
5211 if (cur->next != NULL)
5214 case XML_ENTITY_NODE:
5217 case XML_COMMENT_NODE:
5218 case XML_ELEMENT_DECL:
5219 case XML_ATTRIBUTE_DECL:
5220 case XML_ENTITY_DECL:
5221 case XML_XINCLUDE_START:
5222 case XML_XINCLUDE_END:
5227 if (glob) strcat(buf, ")");
5231 * xmlValidateElementContent:
5232 * @ctxt: the validation context
5233 * @child: the child list
5234 * @elemDecl: pointer to the element declaration
5235 * @warn: emit the error message
5236 * @parent: the parent element (for error reporting)
5238 * Try to validate the content model of an element
5240 * returns 1 if valid or 0 if not and -1 in case of error
5244 xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
5245 xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
5247 #ifndef LIBXML_REGEXP_ENABLED
5248 xmlNodePtr repl = NULL, last = NULL, tmp;
5251 xmlElementContentPtr cont;
5252 const xmlChar *name;
5254 if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL))
5256 cont = elemDecl->content;
5257 name = elemDecl->name;
5259 #ifdef LIBXML_REGEXP_ENABLED
5260 /* Build the regexp associated to the content model */
5261 if (elemDecl->contModel == NULL)
5262 ret = xmlValidBuildContentModel(ctxt, elemDecl);
5263 if (elemDecl->contModel == NULL) {
5266 xmlRegExecCtxtPtr exec;
5268 if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5273 ctxt->nodeTab = NULL;
5274 exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5277 while (cur != NULL) {
5278 switch (cur->type) {
5279 case XML_ENTITY_REF_NODE:
5281 * Push the current node to be able to roll back
5282 * and process within the entity
5284 if ((cur->children != NULL) &&
5285 (cur->children->children != NULL)) {
5286 nodeVPush(ctxt, cur);
5287 cur = cur->children->children;
5292 if (xmlIsBlankNode(cur))
5296 case XML_CDATA_SECTION_NODE:
5300 case XML_ELEMENT_NODE:
5301 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5305 fullname = xmlBuildQName(cur->name,
5306 cur->ns->prefix, fn, 50);
5307 if (fullname == NULL) {
5311 ret = xmlRegExecPushString(exec, fullname, NULL);
5312 if ((fullname != fn) && (fullname != cur->name))
5315 ret = xmlRegExecPushString(exec, cur->name, NULL);
5322 * Switch to next element
5325 while (cur == NULL) {
5326 cur = nodeVPop(ctxt);
5332 ret = xmlRegExecPushString(exec, NULL, NULL);
5334 xmlRegFreeExecCtxt(exec);
5337 #else /* LIBXML_REGEXP_ENABLED */
5339 * Allocate the stack
5341 ctxt->vstateMax = 8;
5342 ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5343 ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5344 if (ctxt->vstateTab == NULL) {
5345 xmlVErrMemory(ctxt, "malloc failed");
5349 * The first entry in the stack is reserved to the current state
5353 ctxt->nodeTab = NULL;
5354 ctxt->vstate = &ctxt->vstateTab[0];
5361 ret = xmlValidateElementType(ctxt);
5362 if ((ret == -3) && (warn)) {
5363 xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST,
5364 "Content model for Element %s is ambiguous\n",
5366 } else if (ret == -2) {
5368 * An entities reference appeared at this level.
5369 * Buid a minimal representation of this node content
5370 * sufficient to run the validation process on it
5372 DEBUG_VALID_MSG("Found an entity reference, linearizing");
5374 while (cur != NULL) {
5375 switch (cur->type) {
5376 case XML_ENTITY_REF_NODE:
5378 * Push the current node to be able to roll back
5379 * and process within the entity
5381 if ((cur->children != NULL) &&
5382 (cur->children->children != NULL)) {
5383 nodeVPush(ctxt, cur);
5384 cur = cur->children->children;
5389 if (xmlIsBlankNode(cur))
5391 /* no break on purpose */
5392 case XML_CDATA_SECTION_NODE:
5393 /* no break on purpose */
5394 case XML_ELEMENT_NODE:
5396 * Allocate a new node and minimally fills in
5399 tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5401 xmlVErrMemory(ctxt, "malloc failed");
5402 xmlFreeNodeList(repl);
5406 tmp->type = cur->type;
5407 tmp->name = cur->name;
5410 tmp->content = NULL;
5417 if (cur->type == XML_CDATA_SECTION_NODE) {
5419 * E59 spaces in CDATA does not match the
5422 tmp->content = xmlStrdup(BAD_CAST "CDATA");
5429 * Switch to next element
5432 while (cur == NULL) {
5433 cur = nodeVPop(ctxt);
5441 * Relaunch the validation
5443 ctxt->vstate = &ctxt->vstateTab[0];
5450 ret = xmlValidateElementType(ctxt);
5452 #endif /* LIBXML_REGEXP_ENABLED */
5453 if ((warn) && ((ret != 1) && (ret != -3))) {
5459 xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
5461 #ifndef LIBXML_REGEXP_ENABLED
5463 xmlSnprintfElements(&list[0], 5000, repl, 1);
5465 #endif /* LIBXML_REGEXP_ENABLED */
5466 xmlSnprintfElements(&list[0], 5000, child, 1);
5469 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5470 "Element %s content does not follow the DTD, expecting %s, got %s\n",
5471 name, BAD_CAST expr, BAD_CAST list);
5473 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5474 "Element content does not follow the DTD, expecting %s, got %s\n",
5475 BAD_CAST expr, BAD_CAST list, NULL);
5479 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5480 "Element %s content does not follow the DTD\n",
5483 xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5484 "Element content does not follow the DTD\n",
5493 #ifndef LIBXML_REGEXP_ENABLED
5496 * Deallocate the copy if done, and free up the validation stack
5498 while (repl != NULL) {
5503 ctxt->vstateMax = 0;
5504 if (ctxt->vstateTab != NULL) {
5505 xmlFree(ctxt->vstateTab);
5506 ctxt->vstateTab = NULL;
5511 if (ctxt->nodeTab != NULL) {
5512 xmlFree(ctxt->nodeTab);
5513 ctxt->nodeTab = NULL;
5520 * xmlValidateCdataElement:
5521 * @ctxt: the validation context
5522 * @doc: a document instance
5523 * @elem: an element instance
5525 * Check that an element follows #CDATA
5527 * returns 1 if valid or 0 otherwise
5530 xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5533 xmlNodePtr cur, child;
5535 if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) ||
5536 (elem->type != XML_ELEMENT_NODE))
5539 child = elem->children;
5542 while (cur != NULL) {
5543 switch (cur->type) {
5544 case XML_ENTITY_REF_NODE:
5546 * Push the current node to be able to roll back
5547 * and process within the entity
5549 if ((cur->children != NULL) &&
5550 (cur->children->children != NULL)) {
5551 nodeVPush(ctxt, cur);
5552 cur = cur->children->children;
5556 case XML_COMMENT_NODE:
5559 case XML_CDATA_SECTION_NODE:
5566 * Switch to next element
5569 while (cur == NULL) {
5570 cur = nodeVPop(ctxt);
5579 if (ctxt->nodeTab != NULL) {
5580 xmlFree(ctxt->nodeTab);
5581 ctxt->nodeTab = NULL;
5587 * xmlValidateCheckMixed:
5588 * @ctxt: the validation context
5589 * @cont: the mixed content model
5590 * @qname: the qualified name as appearing in the serialization
5592 * Check if the given node is part of the content model.
5594 * Returns 1 if yes, 0 if no, -1 in case of error
5597 xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
5598 xmlElementContentPtr cont, const xmlChar *qname) {
5599 const xmlChar *name;
5601 name = xmlSplitQName3(qname, &plen);
5604 while (cont != NULL) {
5605 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5606 if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5608 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5609 (cont->c1 != NULL) &&
5610 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5611 if ((cont->c1->prefix == NULL) &&
5612 (xmlStrEqual(cont->c1->name, qname)))
5614 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5615 (cont->c1 == NULL) ||
5616 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5617 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5618 "Internal: MIXED struct corrupted\n",
5625 while (cont != NULL) {
5626 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5627 if ((cont->prefix != NULL) &&
5628 (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5629 (xmlStrEqual(cont->name, name)))
5631 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5632 (cont->c1 != NULL) &&
5633 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5634 if ((cont->c1->prefix != NULL) &&
5635 (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5636 (xmlStrEqual(cont->c1->name, name)))
5638 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5639 (cont->c1 == NULL) ||
5640 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5641 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5642 "Internal: MIXED struct corrupted\n",
5653 * xmlValidGetElemDecl:
5654 * @ctxt: the validation context
5655 * @doc: a document instance
5656 * @elem: an element instance
5657 * @extsubset: pointer, (out) indicate if the declaration was found
5658 * in the external subset.
5660 * Finds a declaration associated to an element in the document.
5662 * returns the pointer to the declaration or NULL if not found.
5664 static xmlElementPtr
5665 xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5666 xmlNodePtr elem, int *extsubset) {
5667 xmlElementPtr elemDecl = NULL;
5668 const xmlChar *prefix = NULL;
5670 if ((ctxt == NULL) || (doc == NULL) ||
5671 (elem == NULL) || (elem->name == NULL))
5673 if (extsubset != NULL)
5677 * Fetch the declaration for the qualified name
5679 if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5680 prefix = elem->ns->prefix;
5682 if (prefix != NULL) {
5683 elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5684 elem->name, prefix);
5685 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5686 elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5687 elem->name, prefix);
5688 if ((elemDecl != NULL) && (extsubset != NULL))
5694 * Fetch the declaration for the non qualified name
5695 * This is "non-strict" validation should be done on the
5696 * full QName but in that case being flexible makes sense.
5698 if (elemDecl == NULL) {
5699 elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5700 if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5701 elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5702 if ((elemDecl != NULL) && (extsubset != NULL))
5706 if (elemDecl == NULL) {
5707 xmlErrValidNode(ctxt, elem,
5708 XML_DTD_UNKNOWN_ELEM,
5709 "No declaration for element %s\n",
5710 elem->name, NULL, NULL);
5715 #ifdef LIBXML_REGEXP_ENABLED
5717 * xmlValidatePushElement:
5718 * @ctxt: the validation context
5719 * @doc: a document instance
5720 * @elem: an element instance
5721 * @qname: the qualified name as appearing in the serialization
5723 * Push a new element start on the validation stack.
5725 * returns 1 if no validation problem was found or 0 otherwise
5728 xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5729 xmlNodePtr elem, const xmlChar *qname) {
5731 xmlElementPtr eDecl;
5736 /* printf("PushElem %s\n", qname); */
5737 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5738 xmlValidStatePtr state = ctxt->vstate;
5739 xmlElementPtr elemDecl;
5742 * Check the new element against the content model of the new elem.
5744 if (state->elemDecl != NULL) {
5745 elemDecl = state->elemDecl;
5747 switch(elemDecl->etype) {
5748 case XML_ELEMENT_TYPE_UNDEFINED:
5751 case XML_ELEMENT_TYPE_EMPTY:
5752 xmlErrValidNode(ctxt, state->node,
5754 "Element %s was declared EMPTY this one has content\n",
5755 state->node->name, NULL, NULL);
5758 case XML_ELEMENT_TYPE_ANY:
5759 /* I don't think anything is required then */
5761 case XML_ELEMENT_TYPE_MIXED:
5762 /* simple case of declared as #PCDATA */
5763 if ((elemDecl->content != NULL) &&
5764 (elemDecl->content->type ==
5765 XML_ELEMENT_CONTENT_PCDATA)) {
5766 xmlErrValidNode(ctxt, state->node,
5768 "Element %s was declared #PCDATA but contains non text nodes\n",
5769 state->node->name, NULL, NULL);
5772 ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5775 xmlErrValidNode(ctxt, state->node,
5776 XML_DTD_INVALID_CHILD,
5777 "Element %s is not declared in %s list of possible children\n",
5778 qname, state->node->name, NULL);
5782 case XML_ELEMENT_TYPE_ELEMENT:
5785 * VC: Standalone Document Declaration
5786 * - element types with element content, if white space
5787 * occurs directly within any instance of those types.
5789 if (state->exec != NULL) {
5790 ret = xmlRegExecPushString(state->exec, qname, NULL);
5792 xmlErrValidNode(ctxt, state->node,
5793 XML_DTD_CONTENT_MODEL,
5794 "Element %s content does not follow the DTD, Misplaced %s\n",
5795 state->node->name, qname, NULL);
5805 eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5806 vstateVPush(ctxt, eDecl, elem);
5811 * xmlValidatePushCData:
5812 * @ctxt: the validation context
5813 * @data: some character data read
5814 * @len: the length of the data
5816 * check the CData parsed for validation in the current stack
5818 * returns 1 if no validation problem was found or 0 otherwise
5821 xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5824 /* printf("CDATA %s %d\n", data, len); */
5829 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5830 xmlValidStatePtr state = ctxt->vstate;
5831 xmlElementPtr elemDecl;
5834 * Check the new element against the content model of the new elem.
5836 if (state->elemDecl != NULL) {
5837 elemDecl = state->elemDecl;
5839 switch(elemDecl->etype) {
5840 case XML_ELEMENT_TYPE_UNDEFINED:
5843 case XML_ELEMENT_TYPE_EMPTY:
5844 xmlErrValidNode(ctxt, state->node,
5846 "Element %s was declared EMPTY this one has content\n",
5847 state->node->name, NULL, NULL);
5850 case XML_ELEMENT_TYPE_ANY:
5852 case XML_ELEMENT_TYPE_MIXED:
5854 case XML_ELEMENT_TYPE_ELEMENT:
5858 for (i = 0;i < len;i++) {
5859 if (!IS_BLANK_CH(data[i])) {
5860 xmlErrValidNode(ctxt, state->node,
5861 XML_DTD_CONTENT_MODEL,
5862 "Element %s content does not follow the DTD, Text not allowed\n",
5863 state->node->name, NULL, NULL);
5870 * VC: Standalone Document Declaration
5871 * element types with element content, if white space
5872 * occurs directly within any instance of those types.
5884 * xmlValidatePopElement:
5885 * @ctxt: the validation context
5886 * @doc: a document instance
5887 * @elem: an element instance
5888 * @qname: the qualified name as appearing in the serialization
5890 * Pop the element end from the validation stack.
5892 * returns 1 if no validation problem was found or 0 otherwise
5895 xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
5896 xmlNodePtr elem ATTRIBUTE_UNUSED,
5897 const xmlChar *qname ATTRIBUTE_UNUSED) {
5902 /* printf("PopElem %s\n", qname); */
5903 if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5904 xmlValidStatePtr state = ctxt->vstate;
5905 xmlElementPtr elemDecl;
5908 * Check the new element against the content model of the new elem.
5910 if (state->elemDecl != NULL) {
5911 elemDecl = state->elemDecl;
5913 if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5914 if (state->exec != NULL) {
5915 ret = xmlRegExecPushString(state->exec, NULL, NULL);
5917 xmlErrValidNode(ctxt, state->node,
5918 XML_DTD_CONTENT_MODEL,
5919 "Element %s content does not follow the DTD, Expecting more child\n",
5920 state->node->name, NULL,NULL);
5923 * previous validation errors should not generate
5935 #endif /* LIBXML_REGEXP_ENABLED */
5938 * xmlValidateOneElement:
5939 * @ctxt: the validation context
5940 * @doc: a document instance
5941 * @elem: an element instance
5943 * Try to validate a single element and it's attributes,
5944 * basically it does the following checks as described by the
5945 * XML-1.0 recommendation:
5946 * - [ VC: Element Valid ]
5947 * - [ VC: Required Attribute ]
5948 * Then call xmlValidateOneAttribute() for each attribute present.
5950 * The ID/IDREF checkings are done separately
5952 * returns 1 if valid or 0 otherwise
5956 xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5958 xmlElementPtr elemDecl = NULL;
5959 xmlElementContentPtr cont;
5960 xmlAttributePtr attr;
5963 const xmlChar *name;
5968 if (elem == NULL) return(0);
5969 switch (elem->type) {
5970 case XML_ATTRIBUTE_NODE:
5971 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5972 "Attribute element not expected\n", NULL, NULL ,NULL);
5975 if (elem->children != NULL) {
5976 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5977 "Text element has children !\n",
5981 if (elem->ns != NULL) {
5982 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5983 "Text element has namespace !\n",
5987 if (elem->content == NULL) {
5988 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5989 "Text element has no content !\n",
5994 case XML_XINCLUDE_START:
5995 case XML_XINCLUDE_END:
5997 case XML_CDATA_SECTION_NODE:
5998 case XML_ENTITY_REF_NODE:
6000 case XML_COMMENT_NODE:
6002 case XML_ENTITY_NODE:
6003 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6004 "Entity element not expected\n", NULL, NULL ,NULL);
6006 case XML_NOTATION_NODE:
6007 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6008 "Notation element not expected\n", NULL, NULL ,NULL);
6010 case XML_DOCUMENT_NODE:
6011 case XML_DOCUMENT_TYPE_NODE:
6012 case XML_DOCUMENT_FRAG_NODE:
6013 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6014 "Document element not expected\n", NULL, NULL ,NULL);
6016 case XML_HTML_DOCUMENT_NODE:
6017 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6018 "HTML Document not expected\n", NULL, NULL ,NULL);
6020 case XML_ELEMENT_NODE:
6023 xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
6024 "unknown element type\n", NULL, NULL ,NULL);
6029 * Fetch the declaration
6031 elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
6032 if (elemDecl == NULL)
6036 * If vstateNr is not zero that means continuous validation is
6037 * activated, do not try to check the content model at that level.
6039 if (ctxt->vstateNr == 0) {
6040 /* Check that the element content matches the definition */
6041 switch (elemDecl->etype) {
6042 case XML_ELEMENT_TYPE_UNDEFINED:
6043 xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
6044 "No declaration for element %s\n",
6045 elem->name, NULL, NULL);
6047 case XML_ELEMENT_TYPE_EMPTY:
6048 if (elem->children != NULL) {
6049 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
6050 "Element %s was declared EMPTY this one has content\n",
6051 elem->name, NULL, NULL);
6055 case XML_ELEMENT_TYPE_ANY:
6056 /* I don't think anything is required then */
6058 case XML_ELEMENT_TYPE_MIXED:
6060 /* simple case of declared as #PCDATA */
6061 if ((elemDecl->content != NULL) &&
6062 (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
6063 ret = xmlValidateOneCdataElement(ctxt, doc, elem);
6065 xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
6066 "Element %s was declared #PCDATA but contains non text nodes\n",
6067 elem->name, NULL, NULL);
6071 child = elem->children;
6072 /* Hum, this start to get messy */
6073 while (child != NULL) {
6074 if (child->type == XML_ELEMENT_NODE) {
6076 if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
6080 fullname = xmlBuildQName(child->name, child->ns->prefix,
6082 if (fullname == NULL)
6084 cont = elemDecl->content;
6085 while (cont != NULL) {
6086 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
6087 if (xmlStrEqual(cont->name, fullname))
6089 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6090 (cont->c1 != NULL) &&
6091 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
6092 if (xmlStrEqual(cont->c1->name, fullname))
6094 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6095 (cont->c1 == NULL) ||
6096 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
6097 xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
6098 "Internal: MIXED struct corrupted\n",
6104 if ((fullname != fn) && (fullname != child->name))
6109 cont = elemDecl->content;
6110 while (cont != NULL) {
6111 if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
6112 if (xmlStrEqual(cont->name, name)) break;
6113 } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6114 (cont->c1 != NULL) &&
6115 (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
6116 if (xmlStrEqual(cont->c1->name, name)) break;
6117 } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6118 (cont->c1 == NULL) ||
6119 (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
6120 xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
6121 "Internal: MIXED struct corrupted\n",
6128 xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
6129 "Element %s is not declared in %s list of possible children\n",
6130 name, elem->name, NULL);
6135 child = child->next;
6138 case XML_ELEMENT_TYPE_ELEMENT:
6139 if ((doc->standalone == 1) && (extsubset == 1)) {
6141 * VC: Standalone Document Declaration
6142 * - element types with element content, if white space
6143 * occurs directly within any instance of those types.
6145 child = elem->children;
6146 while (child != NULL) {
6147 if (child->type == XML_TEXT_NODE) {
6148 const xmlChar *content = child->content;
6150 while (IS_BLANK_CH(*content))
6152 if (*content == 0) {
6153 xmlErrValidNode(ctxt, elem,
6154 XML_DTD_STANDALONE_WHITE_SPACE,
6155 "standalone: %s declared in the external subset contains white spaces nodes\n",
6156 elem->name, NULL, NULL);
6164 child = elem->children;
6165 cont = elemDecl->content;
6166 tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
6171 } /* not continuous */
6173 /* [ VC: Required Attribute ] */
6174 attr = elemDecl->attributes;
6175 while (attr != NULL) {
6176 if (attr->def == XML_ATTRIBUTE_REQUIRED) {
6179 if ((attr->prefix == NULL) &&
6180 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6184 while (ns != NULL) {
6185 if (ns->prefix == NULL)
6189 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6193 while (ns != NULL) {
6194 if (xmlStrEqual(attr->name, ns->prefix))
6201 attrib = elem->properties;
6202 while (attrib != NULL) {
6203 if (xmlStrEqual(attrib->name, attr->name)) {
6204 if (attr->prefix != NULL) {
6205 xmlNsPtr nameSpace = attrib->ns;
6207 if (nameSpace == NULL)
6208 nameSpace = elem->ns;
6210 * qualified names handling is problematic, having a
6211 * different prefix should be possible but DTDs don't
6212 * allow to define the URI instead of the prefix :-(
6214 if (nameSpace == NULL) {
6217 } else if (!xmlStrEqual(nameSpace->prefix,
6225 * We should allow applications to define namespaces
6226 * for their application even if the DTD doesn't
6227 * carry one, otherwise, basically we would always
6233 attrib = attrib->next;
6236 if (qualified == -1) {
6237 if (attr->prefix == NULL) {
6238 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
6239 "Element %s does not carry attribute %s\n",
6240 elem->name, attr->name, NULL);
6243 xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
6244 "Element %s does not carry attribute %s:%s\n",
6245 elem->name, attr->prefix,attr->name);
6248 } else if (qualified == 0) {
6249 xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
6250 "Element %s required attribute %s:%s has no prefix\n",
6251 elem->name, attr->prefix, attr->name);
6252 } else if (qualified == 1) {
6253 xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
6254 "Element %s required attribute %s:%s has different prefix\n",
6255 elem->name, attr->prefix, attr->name);
6257 } else if (attr->def == XML_ATTRIBUTE_FIXED) {
6259 * Special tests checking #FIXED namespace declarations
6260 * have the right value since this is not done as an
6261 * attribute checking
6263 if ((attr->prefix == NULL) &&
6264 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6268 while (ns != NULL) {
6269 if (ns->prefix == NULL) {
6270 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
6271 xmlErrValidNode(ctxt, elem,
6272 XML_DTD_ELEM_DEFAULT_NAMESPACE,
6273 "Element %s namespace name for default namespace does not match the DTD\n",
6274 elem->name, NULL, NULL);
6281 } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6285 while (ns != NULL) {
6286 if (xmlStrEqual(attr->name, ns->prefix)) {
6287 if (!xmlStrEqual(attr->defaultValue, ns->href)) {
6288 xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
6289 "Element %s namespace name for %s does not match the DTD\n",
6290 elem->name, ns->prefix, NULL);
6307 * @ctxt: the validation context
6308 * @doc: a document instance
6310 * Try to validate a the root element
6311 * basically it does the following check as described by the
6312 * XML-1.0 recommendation:
6313 * - [ VC: Root Element Type ]
6314 * it doesn't try to recurse or apply other check to the element
6316 * returns 1 if valid or 0 otherwise
6320 xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6324 if (doc == NULL) return(0);
6326 root = xmlDocGetRootElement(doc);
6327 if ((root == NULL) || (root->name == NULL)) {
6328 xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6329 "no root element\n", NULL);
6334 * When doing post validation against a separate DTD, those may
6335 * no internal subset has been generated
6337 if ((doc->intSubset != NULL) &&
6338 (doc->intSubset->name != NULL)) {
6340 * Check first the document root against the NQName
6342 if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6343 if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
6347 fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6348 if (fullname == NULL) {
6349 xmlVErrMemory(ctxt, NULL);
6352 ret = xmlStrEqual(doc->intSubset->name, fullname);
6353 if ((fullname != fn) && (fullname != root->name))
6358 if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6359 (xmlStrEqual(root->name, BAD_CAST "html")))
6361 xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6362 "root and DTD name do not match '%s' and '%s'\n",
6363 root->name, doc->intSubset->name, NULL);
6373 * xmlValidateElement:
6374 * @ctxt: the validation context
6375 * @doc: a document instance
6376 * @elem: an element instance
6378 * Try to validate the subtree under an element
6380 * returns 1 if valid or 0 otherwise
6384 xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) {
6388 const xmlChar *value;
6391 if (elem == NULL) return(0);
6394 * XInclude elements were added after parsing in the infoset,
6395 * they don't really mean anything validation wise.
6397 if ((elem->type == XML_XINCLUDE_START) ||
6398 (elem->type == XML_XINCLUDE_END) ||
6399 (elem->type == XML_NAMESPACE_DECL))
6405 * Entities references have to be handled separately
6407 if (elem->type == XML_ENTITY_REF_NODE) {
6411 ret &= xmlValidateOneElement(ctxt, doc, elem);
6412 if (elem->type == XML_ELEMENT_NODE) {
6413 attr = elem->properties;
6414 while (attr != NULL) {
6415 value = xmlNodeListGetString(doc, attr->children, 0);
6416 ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6418 xmlFree((char *)value);
6422 while (ns != NULL) {
6423 if (elem->ns == NULL)
6424 ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6427 ret &= xmlValidateOneNamespace(ctxt, doc, elem,
6428 elem->ns->prefix, ns, ns->href);
6432 child = elem->children;
6433 while (child != NULL) {
6434 ret &= xmlValidateElement(ctxt, doc, child);
6435 child = child->next;
6443 * @ref: A reference to be validated
6444 * @ctxt: Validation context
6445 * @name: Name of ID we are searching for
6449 xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
6450 const xmlChar *name) {
6456 if ((ref->attr == NULL) && (ref->name == NULL))
6460 xmlChar *dup, *str = NULL, *cur, save;
6462 dup = xmlStrdup(name);
6470 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
6473 id = xmlGetID(ctxt->doc, str);
6475 xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
6476 "attribute %s line %d references an unknown ID \"%s\"\n",
6477 ref->name, ref->lineno, str);
6483 while (IS_BLANK_CH(*cur)) cur++;
6486 } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
6487 id = xmlGetID(ctxt->doc, name);
6489 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
6490 "IDREF attribute %s references an unknown ID \"%s\"\n",
6491 attr->name, name, NULL);
6494 } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6495 xmlChar *dup, *str = NULL, *cur, save;
6497 dup = xmlStrdup(name);
6499 xmlVErrMemory(ctxt, "IDREFS split");
6506 while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
6509 id = xmlGetID(ctxt->doc, str);
6511 xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
6512 "IDREFS attribute %s references an unknown ID \"%s\"\n",
6513 attr->name, str, NULL);
6519 while (IS_BLANK_CH(*cur)) cur++;
6526 * xmlWalkValidateList:
6527 * @data: Contents of current link
6528 * @user: Value supplied by the user
6530 * Returns 0 to abort the walk or 1 to continue
6533 xmlWalkValidateList(const void *data, const void *user)
6535 xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6536 xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6541 * xmlValidateCheckRefCallback:
6542 * @ref_list: List of references
6543 * @ctxt: Validation context
6544 * @name: Name of ID we are searching for
6548 xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt,
6549 const xmlChar *name) {
6550 xmlValidateMemo memo;
6552 if (ref_list == NULL)
6557 xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6562 * xmlValidateDocumentFinal:
6563 * @ctxt: the validation context
6564 * @doc: a document instance
6566 * Does the final step for the document validation once all the
6567 * incremental validation steps have been completed
6569 * basically it does the following checks described by the XML Rec
6571 * Check all the IDREF/IDREFS attributes definition for validity
6573 * returns 1 if valid or 0 otherwise
6577 xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6578 xmlRefTablePtr table;
6584 xmlErrValid(ctxt, XML_DTD_NO_DOC,
6585 "xmlValidateDocumentFinal: doc == NULL\n", NULL);
6589 /* trick to get correct line id report */
6590 save = ctxt->finishDtd;
6591 ctxt->finishDtd = 0;
6594 * Check all the NOTATION/NOTATIONS attributes
6597 * Check all the ENTITY/ENTITIES attributes definition for validity
6600 * Check all the IDREF/IDREFS attributes definition for validity
6602 table = (xmlRefTablePtr) doc->refs;
6605 xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt);
6607 ctxt->finishDtd = save;
6608 return(ctxt->valid);
6613 * @ctxt: the validation context
6614 * @doc: a document instance
6615 * @dtd: a dtd instance
6617 * Try to validate the document against the dtd instance
6619 * Basically it does check all the definitions in the DtD.
6620 * Note the the internal subset (if present) is de-coupled
6621 * (i.e. not used), which could give problems if ID or IDREF
6624 * returns 1 if valid or 0 otherwise
6628 xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6630 xmlDtdPtr oldExt, oldInt;
6633 if (dtd == NULL) return(0);
6634 if (doc == NULL) return(0);
6635 oldExt = doc->extSubset;
6636 oldInt = doc->intSubset;
6637 doc->extSubset = dtd;
6638 doc->intSubset = NULL;
6639 ret = xmlValidateRoot(ctxt, doc);
6641 doc->extSubset = oldExt;
6642 doc->intSubset = oldInt;
6645 if (doc->ids != NULL) {
6646 xmlFreeIDTable(doc->ids);
6649 if (doc->refs != NULL) {
6650 xmlFreeRefTable(doc->refs);
6653 root = xmlDocGetRootElement(doc);
6654 ret = xmlValidateElement(ctxt, doc, root);
6655 ret &= xmlValidateDocumentFinal(ctxt, doc);
6656 doc->extSubset = oldExt;
6657 doc->intSubset = oldInt;
6662 xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt,
6663 const xmlChar *name ATTRIBUTE_UNUSED) {
6666 if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6667 xmlChar *notation = cur->content;
6669 if (notation != NULL) {
6672 ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6681 xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt,
6682 const xmlChar *name ATTRIBUTE_UNUSED) {
6685 xmlElementPtr elem = NULL;
6689 switch (cur->atype) {
6690 case XML_ATTRIBUTE_CDATA:
6691 case XML_ATTRIBUTE_ID:
6692 case XML_ATTRIBUTE_IDREF :
6693 case XML_ATTRIBUTE_IDREFS:
6694 case XML_ATTRIBUTE_NMTOKEN:
6695 case XML_ATTRIBUTE_NMTOKENS:
6696 case XML_ATTRIBUTE_ENUMERATION:
6698 case XML_ATTRIBUTE_ENTITY:
6699 case XML_ATTRIBUTE_ENTITIES:
6700 case XML_ATTRIBUTE_NOTATION:
6701 if (cur->defaultValue != NULL) {
6703 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6704 cur->atype, cur->defaultValue);
6705 if ((ret == 0) && (ctxt->valid == 1))
6708 if (cur->tree != NULL) {
6709 xmlEnumerationPtr tree = cur->tree;
6710 while (tree != NULL) {
6711 ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
6712 cur->name, cur->atype, tree->name);
6713 if ((ret == 0) && (ctxt->valid == 1))
6719 if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6721 if (cur->elem == NULL) {
6722 xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
6723 "xmlValidateAttributeCallback(%s): internal error\n",
6724 (const char *) cur->name);
6729 elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6730 if ((elem == NULL) && (doc != NULL))
6731 elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6732 if ((elem == NULL) && (cur->parent != NULL) &&
6733 (cur->parent->type == XML_DTD_NODE))
6734 elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
6736 xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
6737 "attribute %s: could not find decl for element %s\n",
6738 cur->name, cur->elem, NULL);
6741 if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6742 xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
6743 "NOTATION attribute %s declared for EMPTY element %s\n",
6744 cur->name, cur->elem, NULL);
6751 * xmlValidateDtdFinal:
6752 * @ctxt: the validation context
6753 * @doc: a document instance
6755 * Does the final step for the dtds validation once all the
6756 * subsets have been parsed
6758 * basically it does the following checks described by the XML Rec
6759 * - check that ENTITY and ENTITIES type attributes default or
6760 * possible values matches one of the defined entities.
6761 * - check that NOTATION type attributes default or
6762 * possible values matches one of the defined notations.
6764 * returns 1 if valid or 0 if invalid and -1 if not well-formed
6768 xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6770 xmlAttributeTablePtr table;
6771 xmlEntitiesTablePtr entities;
6773 if ((doc == NULL) || (ctxt == NULL)) return(0);
6774 if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6778 dtd = doc->intSubset;
6779 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6780 table = (xmlAttributeTablePtr) dtd->attributes;
6781 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
6783 if ((dtd != NULL) && (dtd->entities != NULL)) {
6784 entities = (xmlEntitiesTablePtr) dtd->entities;
6785 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6788 dtd = doc->extSubset;
6789 if ((dtd != NULL) && (dtd->attributes != NULL)) {
6790 table = (xmlAttributeTablePtr) dtd->attributes;
6791 xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt);
6793 if ((dtd != NULL) && (dtd->entities != NULL)) {
6794 entities = (xmlEntitiesTablePtr) dtd->entities;
6795 xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback,
6798 return(ctxt->valid);
6802 * xmlValidateDocument:
6803 * @ctxt: the validation context
6804 * @doc: a document instance
6806 * Try to validate the document instance
6808 * basically it does the all the checks described by the XML Rec
6809 * i.e. validates the internal and external subset (if present)
6810 * and validate the document tree.
6812 * returns 1 if valid or 0 otherwise
6816 xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6822 if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6823 xmlErrValid(ctxt, XML_DTD_NO_DTD,
6824 "no DTD found!\n", NULL);
6827 if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6828 (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6830 if (doc->intSubset->SystemID != NULL) {
6831 sysID = xmlBuildURI(doc->intSubset->SystemID,
6833 if (sysID == NULL) {
6834 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6835 "Could not build URI for external subset \"%s\"\n",
6836 (const char *) doc->intSubset->SystemID);
6841 doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6842 (const xmlChar *)sysID);
6845 if (doc->extSubset == NULL) {
6846 if (doc->intSubset->SystemID != NULL) {
6847 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6848 "Could not load the external subset \"%s\"\n",
6849 (const char *) doc->intSubset->SystemID);
6851 xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6852 "Could not load the external subset \"%s\"\n",
6853 (const char *) doc->intSubset->ExternalID);
6859 if (doc->ids != NULL) {
6860 xmlFreeIDTable(doc->ids);
6863 if (doc->refs != NULL) {
6864 xmlFreeRefTable(doc->refs);
6867 ret = xmlValidateDtdFinal(ctxt, doc);
6868 if (!xmlValidateRoot(ctxt, doc)) return(0);
6870 root = xmlDocGetRootElement(doc);
6871 ret &= xmlValidateElement(ctxt, doc, root);
6872 ret &= xmlValidateDocumentFinal(ctxt, doc);
6876 /************************************************************************
6878 * Routines for dynamic validation editing *
6880 ************************************************************************/
6883 * xmlValidGetPotentialChildren:
6884 * @ctree: an element content tree
6885 * @names: an array to store the list of child names
6886 * @len: a pointer to the number of element in the list
6887 * @max: the size of the array
6889 * Build/extend a list of potential children allowed by the content tree
6891 * returns the number of element in the list, or -1 in case of error.
6895 xmlValidGetPotentialChildren(xmlElementContent *ctree,
6896 const xmlChar **names,
6897 int *len, int max) {
6900 if ((ctree == NULL) || (names == NULL) || (len == NULL))
6902 if (*len >= max) return(*len);
6904 switch (ctree->type) {
6905 case XML_ELEMENT_CONTENT_PCDATA:
6906 for (i = 0; i < *len;i++)
6907 if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len);
6908 names[(*len)++] = BAD_CAST "#PCDATA";
6910 case XML_ELEMENT_CONTENT_ELEMENT:
6911 for (i = 0; i < *len;i++)
6912 if (xmlStrEqual(ctree->name, names[i])) return(*len);
6913 names[(*len)++] = ctree->name;
6915 case XML_ELEMENT_CONTENT_SEQ:
6916 xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6917 xmlValidGetPotentialChildren(ctree->c2, names, len, max);
6919 case XML_ELEMENT_CONTENT_OR:
6920 xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6921 xmlValidGetPotentialChildren(ctree->c2, names, len, max);
6929 * Dummy function to suppress messages while we try out valid elements
6931 static void XMLCDECL xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
6932 const char *msg ATTRIBUTE_UNUSED, ...) {
6937 * xmlValidGetValidElements:
6938 * @prev: an element to insert after
6939 * @next: an element to insert next
6940 * @names: an array to store the list of child names
6941 * @max: the size of the array
6943 * This function returns the list of authorized children to insert
6944 * within an existing tree while respecting the validity constraints
6945 * forced by the Dtd. The insertion point is defined using @prev and
6946 * @next in the following ways:
6947 * to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6948 * to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6949 * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6950 * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6951 * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6953 * pointers to the element names are inserted at the beginning of the array
6954 * and do not need to be freed.
6956 * returns the number of element in the list, or -1 in case of error. If
6957 * the function returns the value @max the caller is invited to grow the
6958 * receiving array and retry.
6962 xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
6965 int nb_valid_elements = 0;
6966 const xmlChar *elements[256]={0};
6967 int nb_elements = 0, i;
6968 const xmlChar *name;
6976 xmlNode *parent_childs;
6977 xmlNode *parent_last;
6979 xmlElement *element_desc;
6981 if (prev == NULL && next == NULL)
6984 if (names == NULL) return(-1);
6985 if (max <= 0) return(-1);
6987 memset(&vctxt, 0, sizeof (xmlValidCtxt));
6988 vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6990 nb_valid_elements = 0;
6991 ref_node = prev ? prev : next;
6992 parent = ref_node->parent;
6995 * Retrieves the parent element declaration
6997 element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6999 if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
7000 element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
7002 if (element_desc == NULL) return(-1);
7005 * Do a backup of the current tree structure
7007 prev_next = prev ? prev->next : NULL;
7008 next_prev = next ? next->prev : NULL;
7009 parent_childs = parent->children;
7010 parent_last = parent->last;
7013 * Creates a dummy node and insert it into the tree
7015 test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
7016 if (test_node == NULL)
7019 test_node->parent = parent;
7020 test_node->prev = prev;
7021 test_node->next = next;
7022 name = test_node->name;
7024 if (prev) prev->next = test_node;
7025 else parent->children = test_node;
7027 if (next) next->prev = test_node;
7028 else parent->last = test_node;
7031 * Insert each potential child node and check if the parent is
7034 nb_elements = xmlValidGetPotentialChildren(element_desc->content,
7035 elements, &nb_elements, 256);
7037 for (i = 0;i < nb_elements;i++) {
7038 test_node->name = elements[i];
7039 if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
7042 for (j = 0; j < nb_valid_elements;j++)
7043 if (xmlStrEqual(elements[i], names[j])) break;
7044 names[nb_valid_elements++] = elements[i];
7045 if (nb_valid_elements >= max) break;
7050 * Restore the tree structure
7052 if (prev) prev->next = prev_next;
7053 if (next) next->prev = next_prev;
7054 parent->children = parent_childs;
7055 parent->last = parent_last;
7058 * Free up the dummy node
7060 test_node->name = name;
7061 xmlFreeNode(test_node);
7063 return(nb_valid_elements);
7065 #endif /* LIBXML_VALID_ENABLED */
7067 #define bottom_valid
7068 #include "elfgcchack.h"