libxml2 出现 xsd 验证错误

libxml2 get xsd validation errors

我正在使用 xmlTextReader 处理大型 xml 文件。现在我需要根据 xsd 模式验证实例。来自 libxml2 的 api 有点令人困惑,这是如何完成的。

使用我的方法,我在 schemaParseErrorHandler 函数中收到验证错误,但没有任何行号或列号。 我怎样才能得到这些信息?

#include <stdio.h>
#include <libxml/xmlreader.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>

static void schemaParseErrorHandler(void *arg, xmlErrorPtr error)
{
    fprintf( stderr, "Error at line %d, column %d\n%s",
        error->line, error->int2, error->message);

    *((bool*)arg) = true;
}

int main( int argc, char **argv )
{
    xmlInitParser();

    xmlSchemaPtr schema = NULL;
    xmlSchemaParserCtxtPtr schema_parser_ctxt = NULL;

    int has_schema_errors = 0;
    int ret = -1;

    xmlSchemaValidCtxtPtr valid_ctxt = NULL;
    if ((schema_parser_ctxt = xmlSchemaNewParserCtxt("example.xsd")))
    {
        schema = xmlSchemaParse(schema_parser_ctxt);
        xmlSchemaFreeParserCtxt(schema_parser_ctxt);

        if (schema)
        {
            valid_ctxt = xmlSchemaNewValidCtxt(schema);
        }
    }

    xmlTextReaderPtr reader = NULL;
    reader = xmlReaderForFile(filename, RPCXmlStream::STD_ENCODING, 0);

    if (reader != NULL)
    {
        if (valid_ctxt)
        {
            xmlTextReaderSchemaValidateCtxt(reader, valid_ctxt, 0);
            xmlSchemaSetValidStructuredErrors(valid_ctxt, schemaParseErrorHandler, &has_schema_errors);
        }

        ret = xmlTextReaderRead(reader);

        while (ret == 1 && !has_schema_errors)
        {
            //... procesing informations
            ret = xmlTextReaderRead(reader);
        }
    }

    if (ret != 0)
    {
        xmlErrorPtr err = xmlGetLastError();

        TRACE("%s: failed to parse in line %d, col %d. Error %d: %s\n",
            err->file,
            err->line,
            err->int2,
            err->code,
            err->message);
    }

    xmlFreeTextReader(reader);
    xmlCleanupParser();     

    return 0;
}

另一个尝试是使用函数 xmlTextReaderSchemaValidate(reader, "example.xsd"); 不是创建 xmlSchemaNewValidCtxt,而是程序在第一次调用 xmlTextReaderRead.

时崩溃

那么验证是如何正确完成的,以便错误信息包括行号和列号?

所以,你的问题让我开始思考,当我查看 libxml2 documentation

Structure xmlError
struct _xmlError {
    int domain  : What part of the library raised this er
    int code    : The error code, e.g. an xmlParserError
    char *  message : human-readable informative error messag
    xmlErrorLevel   level   : how consequent is the error
    char *  file    : the filename
    int line    : the line number if available
    char *  str1    : extra string information
    char *  str2    : extra string information
    char *  str3    : extra string information
    int int1    : extra number information
    int int2    : error column # or 0 if N/A (todo: renam
    void *  ctxt    : the parser context if available
    void *  node    : the node in the tree
}

这里我们可以清楚的看到函数xmlGetLastError()返回的xmlErrorPtr清楚的包含了文件名以及行号和列号的信息。

    char *  file    : the filename
    int line    : the line number if available
    ...
    int int2    : error column

因此,为了测试这是否可行,这是我使用的代码(基本上是您的代码,稍作改动以使其在我的系统上 运行):

#include <stdio.h>
#include <stdbool.h>
#include <libxml/xmlreader.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>

static void schemaParseErrorHandler(void *arg, xmlErrorPtr error)
{
    fprintf(stderr, "Error at line %d, column %d\n%s", error->line, error->int2, error->message);
    *((bool*)arg) = true;
}

int main( int argc, char **argv )
{
    xmlInitParser();
    xmlSchemaPtr schema = NULL;
    xmlSchemaParserCtxtPtr schema_parser_ctxt = NULL;
    int has_schema_errors = 0;
    int ret = -1;
    xmlSchemaValidCtxtPtr valid_ctxt = NULL;
    if ((schema_parser_ctxt = xmlSchemaNewParserCtxt("/home/junglefox/shiporder.xsd")))
    {
        schema = xmlSchemaParse(schema_parser_ctxt);
        xmlSchemaFreeParserCtxt(schema_parser_ctxt);
        if (schema)
        {
            valid_ctxt = xmlSchemaNewValidCtxt(schema);
        }
    }
    xmlTextReaderPtr reader = NULL;
    const char* filename = "/home/junglefox/shiporder.xml";
    reader = xmlReaderForFile(filename, /*RPCXmlStream::STD_ENCODING,*/ NULL, 0);

    if (reader != NULL)
    {
        if (valid_ctxt)
        {
            xmlTextReaderSchemaValidateCtxt(reader, valid_ctxt, 0);
            xmlSchemaSetValidStructuredErrors(valid_ctxt, schemaParseErrorHandler, &has_schema_errors);
        }
        ret = xmlTextReaderRead(reader);
        while (ret == 1 && !has_schema_errors)
        {
            //... procesing informations
            ret = xmlTextReaderRead(reader);
        }
    }

    if (ret != 0)
    {
        xmlErrorPtr err = xmlGetLastError();
        fprintf(stdout, "%s: failed to parse in line %d, col %d. Error %d: %s\n",
                err->file,
                err->line,
                err->int2,
                err->code,
                err->message);
    }
    xmlFreeTextReader(reader);
    xmlCleanupParser();
    return 0;
}

其中,该程序中使用的 shiporder.xmlshiporder.xsd 是从 url 并保存在本地。

我编译 运行 代码如下:

junglefox@ubuntu:~$ gcc -o test_xsd main.c -I/usr/include/libxml2/ -lxml2 -L/usr/lib/x86_64-linux-gnu/
junglefox@ubuntu:~$ ./test_xsd
junglefox@ubuntu:~$

这次没有输出。应该是因为没有错误。

如果现在我在shiporder.xml文件中故意出错,如下图:

  • 这是部分-来自马车shiporder.xml:

    的片段

    <?xml version="1.0" encoding="UTF-8"?> ... <item> <title>Hide your heart</title> <quantity>1</quantity> price>9.90</price> </item> </shiporder>

  • 注意price!

  • 前面少了<

现在我又运行程序了,

junglefox@ubuntu:~$ ./test_xsd 
Error at line 22, column 0
Element 'item': Character content other than whitespace is not allowed because the content type is 'element-only'.

它回答了您的问题:

With my approach, im getting the validation errors in the schemaParseErrorHandler function, but without any line numbers or column numbers. How can i get these informations?

并且,

So how is validation done right, so that the error informations includes line and column numbers?

因为输出清楚地显示了 行号 22列 0,其中出现了意外的 empty space 由于失踪的 <.