A DTD grammar is cached by its system id. You need to make sure that in your xml document the DOCTYPE declaration uses the same system id.
 
Khaled




Frank Zhou <[EMAIL PROTECTED]>

05/19/2006 01:28 AM

Please respond to
[email protected]

To
[email protected]
cc
Subject
Re: validate XML against DTD using SAX2XMLReader





Hi All,

Would like to confirm what I found:

I just did some debugging of the Xerces library about
this, and noticed that it seems loadGrammar(...) will
never work in validating XML against a given DTD if
using SAX2XMLreader.

The reason is this, when SAX2XMLReader calls

  parser->parse(inputXMLInstanceSource),

It calls scanDocument(inputXMLInstanceSource)
which calls scanReset(inputXMLInstanceSource).

Inside scanReset(..), there is following lines at the
begining:

 
fGrammarResolver->cacheGrammarFromParse(fToCacheGrammar);
 
fGrammarResolver->useCachedGrammarInParse(fUseCachedGrammar);

   // fModel may need updating, as fGrammarResolver
could have cleaned it
   if(fModel && getPSVIHandler())
       fModel = fGrammarResolver->getXSModel();

   {
       XMLDTDDescriptionImpl  
theDTDDescription(XMLUni::fgDTDEntityString,
fMemoryManager);
       fDTDGrammar = (DTDGrammar*)
fGrammarResolver->getGrammar(&theDTDDescription);
   }

   if (!fDTDGrammar) {

       fDTDGrammar = new (fGrammarPoolMemoryManager)
DTDGrammar(fGrammarPoolMemoryManager);
       fGrammarResolver->putGrammar(fDTDGrammar);
   }
   else
       fDTDGrammar->reset();

If we look at the last few lines, it seems that every
time when we parse am XML input, the DTDGrammar object
is either re-created or gets re-set, in another work,
the original DTDGrammar created by loadGrammar(...) is
gone. That's why the validation does not work, and
never will work.

Does this make sense? Just wonder, if this is the
case, then why do we still need those features like
XMLUni::fgXercesCacheGrammarFromParse, they are not
going to work anyway, right?

Thanks for clarification in aadvance.

Frank

>  
>
> -----Original Message-----
> From: Frank Zhou [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, May 17, 2006 9:05 PM
> To: [email protected]
> Cc: [EMAIL PROTECTED]
> Subject: validate XML against DTD using
> SAX2XMLReader
>
> Hi All,
>
> I am using Xerces-c 2.7.  I am having a problem
> validating XML against a
> given DTD (a string).
>
> I am using SAX2XMLReader and using loadGrammar(...)
> to load the DTD. My
> code is something like this:
>
>     SAX2XMLReader* parser =
> XMLReaderFactory::createXMLReader();
>     parser->setFeature(XMLUni::fgSAX2CoreValidation,
> m_bValidate);
>     parser->setFeature(XMLUni::fgXercesDynamic,
> true);
>
>     parser->loagGrammar(DTDInputSource,
> Grammar::DTDGrammarType);
>    
>     parser->setErrorHandler(errorHandler);
>     parser->setContentHandler(contentHandler);
>
>     parser->parse(XMLinputsource);
>
> I noticed that the validation is not happening,
> invalid xml gets parsed
> without problem. However, if I don't use
> loadGrammar(...), instead, I
> embedded the dtd content into XML input using
> <!DOCTYPE ... ]>, then the
> validation happens correctly.
>
> Do I need to do some special settings? any problem
> with
> loadGrammar(...)?
>
> Thanks much.
> Frank
>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam
> protection around
> http://mail.yahoo.com
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to