[xml] Validating a previously validated and then modified document

Date view Thread view Subject view Author view

From: Gary Pennington (Gary.Pennington@uk.sun.com)
Date: Wed Feb 07 2001 - 09:49:50 EST


Hi,

I have a document with an associated DTD. I validate it. I then
programmatically make changes to the document, including adding new
elements, updating attributes, etc...

Now, before I write the document to disk I want to re-validate the
document to ensure that it is still valid and no errors have been
introduced. How to do this in libxml?

I tried calling xmlValidateDocument, however it complained that IDs were
already defined which means, I guess, that the data structures are still
there from the previous validation and thus causing problems for the
re-validation.

I did a quick hack to set ids and refs to NULL in the document when
calling xmlValidateDocument and this did the trick for me at the cost of
a memory leak.

I think the real answer is to find the core validation function(s) and
do an xmlFreeIDTable and an xmlFreeRefTable whenever validation is
called if the table pointers aren't null.

My patch is :-

(Sorry, patch is against version libxml2-2.2.11, I haven't got around to
compiling up 12 yet but I don't think there are any changes between 11
and 12 in valid.c)

*** ../ORIG/libxml2-2.2.11/valid.c Fri Jan 5 06:39:29 2001
--- valid.c Wed Feb 7 14:46:29 2001
***************
*** 3497,3502 ****
--- 3497,3510 ----
      xmlNodePtr root;
      if (doc == NULL) return(0);

+ if (doc->ids != NULL) {
+ xmlFreeIDTable(doc->ids);
+ doc->ids = NULL;
+ }
+ if (doc->refs != NULL) {
+ xmlFreeRefTable(doc->refs);
+ doc->refs = NULL;
+ }
      root = xmlDocGetRootElement(doc);
      if ((root == NULL) || (root->name == NULL)) {
        VERROR(ctxt->userData, "Not valid: no root element\n");

I'm not certain that this patch is catching all areas where it's
required, so it needs some scrutiny. It's working fine for me in the
scenario I descrbed above. I decided to put the check in xmlValidateRoot
since I was pretty sure that this would be called whenever an entire
document was validated, however it may be the wrong place for this
check.

Yours,

Gary

----
Message from the list xml@rpmfind.net
Archived at : http://xmlsoft.org/messages/
to unsubscribe: echo "unsubscribe xml" | mail  majordomo@rpmfind.net


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Wed Feb 07 2001 - 10:44:08 EST