[Opendnssec-develop] libxml2 xsd:durations

John Dickinson jad at jadickinson.co.uk
Tue Apr 14 16:35:07 UTC 2009


Before I write a function to do it myself, does anyone know how to  
deal with xsd:durations in C (convert them to seconds). I hoped that  
libxml2 would do it for me but I can not find the function :)

Also for anyone else who needs to read, validate and parse with xpath  
the opendnssec xml files using libxml2 here is some example code that  
once much improved will end up in the enforcer.



#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/relaxng.h>

int main()
{
     xmlDocPtr doc;
     xmlDocPtr rngdoc;
     xmlXPathContextPtr xpathCtx;
     xmlXPathObjectPtr xpathObj;
     xmlRelaxNGParserCtxtPtr rngpctx;
     xmlRelaxNGValidCtxtPtr rngctx;
     xmlRelaxNGPtr schema;
     xmlChar *xexpr;
     xexpr = "//Configuration/Enforcer/KeygenInterval";
     int status;
     char* filename = "/tmp/conf.xml";
     char* rngfilename = "/tmp/conf.rng";

     /* Load XML document */
     doc = xmlParseFile(filename);
     if (doc == NULL) {
   	  fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
	    return(-1);
     }

     /* Load rng document */
     rngdoc = xmlParseFile(rngfilename);
     if (rngdoc == NULL) {
   	  fprintf(stderr, "Error: unable to parse file \"%s\"\n",  
rngfilename);
	    return(-1);
     }

     /* Create an XML RelaxNGs parser context for the relax-ng  
document. */
     rngpctx = xmlRelaxNGNewDocParserCtxt(rngdoc);

     /* parse a schema definition resource and build an internal XML  
Shema struture which can be used to validate instances. */
     schema = xmlRelaxNGParse(rngpctx);

     /* Create an XML RelaxNGs validation context based on the given  
schema */
     rngctx = xmlRelaxNGNewValidCtxt(schema);

     /* Validate a document tree in memory. */
     status = xmlRelaxNGValidateDoc(rngctx,doc);
     if (status != 0) {
       fprintf(stderr, "Error validating file \"%s\"\n", filename);
     }

     /* Now parse a value out of the conf */
     /* lets try and get the keygeninterval */

     /* Create xpath evaluation context */
     xpathCtx = xmlXPathNewContext(doc);
     if(xpathCtx == NULL) {
         fprintf(stderr,"Error: unable to create new XPath context\n");
         xmlFreeDoc(doc);
         return(-1);
     }

     /* Evaluate xpath expression */
     xpathObj = xmlXPathEvalExpression(xexpr, xpathCtx);
     if(xpathObj == NULL) {
         fprintf(stderr,"Error: unable to evaluate xpath expression\n");
         xmlXPathFreeContext(xpathCtx);
         xmlFreeDoc(doc);
         return(-1);
     }
     printf("keygeninterval is %s\n", xmlXPathCastToString(xpathObj));

     /* Cleanup */
     /* some other frees are needed */
     xmlXPathFreeObject(xpathObj);
     xmlXPathFreeContext(xpathCtx);
     xmlFreeDoc(doc);

     return(0);
}



---
John Dickinson
http://www.jadickinson.co.uk

I am riding from Lands end to John O'Groats to raise money for  
Parkinson's Disease Research. Please sponsor me here http://justgiving.com/pedalforparkinsons2009






More information about the Opendnssec-develop mailing list