How to validate an XML using inline DTD using libxml2 (SAX parser)?

1.1k Views Asked by At

I am writing an XML parser in C++ using libxml library which requires me to validate the XML against a DTD which is specified inline. I don't want to use system() in my program. Otherwise I could have used the xmllint command.

I came across the xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) function specified in the http://xmlsoft.org/ API page. But I feel it is DOM based function since in SAX parsing there is no xmlDocPtr.

Are there are any other methods to validate the XML document against the inline DTD?

1

There are 1 best solutions below

1
On BEST ANSWER

Usually, you simply provide the XML_PARSE_DTDVALID parser option, so the document will be validated when parsed.

If the document has already been parsed without validation, you can use xmlValidateDocument:

int xmlValidateDocument (xmlValidCtxtPtr ctxt, 
                         xmlDocPtr doc)

Try to validate the document instance basically it does the all the checks described by the XML Rec i.e. validates the internal and external subset (if present) and validate the document tree.

ctxt: the validation context
doc: a document instance
Returns:1 if valid or 0 otherwise