Search code examples
xmldtdxerces

Xerces dtd validation doesn't work


I have a DTD data that I want to do XML validation with. Here is the external DTD:

<!-- root element. must be there, must contain only one users element -->
<!ELEMENT root (users)>
<!-- users element contains user elements, one or more but can't have no elements-->
<!ELEMENT users (user)+>
<!--user element will include simple text only, no sub elements -->
<!ELEMENT user (#PCDATA)>

here is the users.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<users>
    <user>some data</user>
    <user>some data</user>
    <user>some data</user>
</users>
</root>

After creating the parser, here is what I do to validate:

Grammar* g= parser->loadGrammar("users.dtd",Grammar::DTDGrammarType,false);
parser->setValidationScheme(XercesDOMParser::Val_Auto);

Now, if I'm using Val_Auto anything goes. there is no validation whats so ever, I'm using different files and it doesn't catch anything. If I'm switching to Val_Always it always fails with the above file, outputting "no declaration found for element 'user' ". If I'm setting the DTD in the header for the xml file like so: < !DOCTYPE root SYSTEM "users.dtd" > it's working just fine. Of course, I can't use this in the real world, Since I need both in the memory (I'm getting the info from the network). I can even cancel the whole loadGrammar line, won't change the results.

Any ideas?


Solution

  • OK. it seems DTD is not playing very well with Xerces. However, moving the xsd (much better format) and using catch solved this problem.