验证XPath表达式 [英] Validate XPath expression
本文介绍了验证XPath表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想验证用户提供的XPath查询,以确保它在语法上正确.
I would like to validate a user-provided XPath query to ensure that it is syntactically correct.
在没有XML文档的情况下验证" XPath查询是否有意义?
Does it make sense to "validate" an XPath query without an XML document?
我如何在libxml中实现这样的目标?
How can i achieve such a thing in libxml?
推荐答案
您可以使用libxml编译XPath表达式,而无需对其进行评估.请参见 xmlXPathCompile .对于无效的表达式,返回值应为NULL.
You can compile an XPath expression using libxml without evaluating it. See xmlXPathCompile. The return value should be NULL for invalid expressions.
#include <stdio.h>
#include <libxml/xpath.h>
#include <libxml/xmlerror.h>
void
handle_structured_error(void *userdata, xmlErrorPtr err)
{
printf("%s", err->message);
}
int
main(int argc, char **argv)
{
if (argc < 2)
return 1;
const char *expr = argv[1];
xmlXPathContextPtr ctx = xmlXPathNewContext(NULL);
xmlSetStructuredErrorFunc(ctx, &handle_structured_error);
xmlXPathCompExprPtr p = xmlXPathCtxtCompile(ctx, (xmlChar *)expr);
if (p) {
printf("Valid: %s\n", expr);
xmlXPathFreeCompExpr(p);
} else {
printf("Invalid: %s\n", expr);
}
xmlXPathFreeContext(ctx);
return 0;
}
这篇关于验证XPath表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文