在ANSI C中针对松弛验证xml [英] Validating xml against relax ng in ANSI C

查看:120
本文介绍了在ANSI C中针对松弛验证xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以针对ANSI C中的Relax NG模式验证xml文件?我遇到了一个名为libxml2的库,但是我从中可以获得的所有帮助都是关于如何解析xml文件的.请帮忙.

Is it possible to validate an xml file against a Relax NG schema in ANSI C? I have come across this library called libxml2 but all help I could get from it is with respect to how to parse an xml file. Please help.

如果可以完成,请问该怎么做?对此一无所知C环境.

And if it can be done, what are the steps? Utterly ignorant about this w.r.t. the C environment.

推荐答案

这是一个简单的示例(您当然应该添加自己的错误检查):

Here is a minimalistic example (you should of course add your own error checking):

 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>

 #include <libxml/xmlmemory.h>
 #include <libxml/parser.h>
 #include <libxml/relaxng.h>

 int
 main(int argc, char *argv[])
 {
    int status;
    xmlDoc *doc;
    xmlRelaxNGPtr schema;
    xmlRelaxNGValidCtxtPtr validctxt;
    xmlRelaxNGParserCtxtPtr rngparser;

    doc = xmlParseFile(argv[1]);

    rngparser = xmlRelaxNGNewParserCtxt(argv[2]);
    schema = xmlRelaxNGParse(rngparser);
    validctxt = xmlRelaxNGNewValidCtxt(schema);

    status = xmlRelaxNGValidateDoc(validctxt, doc);
    printf("status == %d\n", status);

    xmlRelaxNGFree(schema);
    xmlRelaxNGFreeValidCtxt(validctxt);
    xmlRelaxNGFreeParserCtxt(rngparser);
    xmlFreeDoc(doc);
    exit(EXIT_SUCCESS);
 }

使用gcc -I/usr/include/libxml2 rngval.c -o rngval -lxml2进行编译

Compile this with gcc -I/usr/include/libxml2 rngval.c -o rngval -lxml2

您可以在 http://xmlsoft.org/html/libxml-relaxng.html 中查看相关文档. >

You can check the relevant documentation at http://xmlsoft.org/html/libxml-relaxng.html

这篇关于在ANSI C中针对松弛验证xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆