c - Yang 的 XPath current()

标签 c xml xpath libxml2 ietf-netmod-yang

这是我对 Xpath current() in Yang 的跟进问题在前面的讨论之后,我为 libxml2 实现了一个自定义函数 current(),其实现如下所示。该函数在计算 Xpath 表达式时按预期工作正常:

leaf test-string {
  type string;
  must "current() > 0";
}

但是当涉及更复杂的表达式时,如 Xpath 表达式“interface[name=current()/supporting-interface]/type = 'optical'”,它似乎无法正常工作。我在 current() 实现中遗漏的任何内容。 下面给出的是我的实现。还有一种方法可以打印由 xmlXPathEvalExpression() API 构造的最终 Xpath 表达式吗?

/*
 * ext_current () -- Defines the Xpath extension current(),
 * defined by Yang RFC.
 *
 * From Yang RFC 7950:
 *
 * The current() function takes no input parameters and returns a node
 * set with the initial context node as its only member.
 */
static void
ext_current (xmlXPathParserContextPtr ctxt, int nargs)
{
    /*
     * This function takes 0 args.
     */
    if (nargs != 0) {
        return;
    }

    /* Push the current context node onto Xpath Stack */
    valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
}

/*
 * register_yang_xpath_extensions () -- Registers extensions defined by Yang
 * RFC.
 */
static void
register_yang_xpath_extensions (xmlXPathContextPtr ctxt)
{
    int rc = 0;

    rc = xmlXPathRegisterFunc(ctxt, (const xmlChar *)"current",
                              ext_current);
    if (rc != 0)
        fprintf(stderr, "Error in registering current() func\n");
}

最佳答案

.current() 在 YANG 中是不一样的,尽管它们可能在 certain situations 中返回相同的结果。 . . 的结果随 XPath 路径步骤(current 上下文节点,正如您自己在代码中引用的那样)而变化,而 current()无论在同一 XPath 表达式中的何处使用,始终返回同一节点 - 初始 上下文节点。

YANG 中 XPath 表达式的初始上下文节点取决于此表达式相对于 YANG 模式定义的位置。 RFC7950, Section 6.4.1中的规则组合这些用于确定初始上下文节点:

  • when 语句:

The XPath expression is conceptually evaluated in the following context, in addition to the definition in Section 6.4.1:

o 如果“when”语句是“augment”语句的子语句,则 上下文节点是数据树中扩充的目标节点,如果 目标节点是数据节点。否则,上下文节点是 距离目标节点最近的祖先节点也是一个数据 节点。如果不存在这样的节点,则上下文节点 是根节点。 可访问树在处理过程中被暂时更改 通过删除 XPath 表达式的所有实例(如果有的话) 由“augment”语句添加的节点。

o 如果“when”语句是“uses”、“choice”或“uses”、“choice”或 “case”语句,则上下文节点是最近的祖先 节点到具有“when”语句的节点也是一个数据 节点。如果不存在这样的节点,则上下文节点 是根节点。 可访问树在处理过程中被暂时更改 通过删除 XPath 表达式的所有实例(如果有的话) 由“uses”、“choice”或“case”语句添加的节点。

o 如果“when”语句是任何其他数据定义的子项 声明中,可访问树在 通过替换所有实例来处理 XPath 表达式 为其定义了“when”语句的数据节点 具有相同名称但没有值且没有的单个虚拟节点 children 。如果不存在这样的实例,则虚拟节点是 暂定创建。 上下文节点就是这个虚拟节点。

RFC7950, Section 7.21.5

  • 必须声明:

The XPath expression is conceptually evaluated in the following context, in addition to the definition in Section 6.4.1:

o 如果“必须”语句是“通知”的子语句 声明中,上下文节点是表示 可访问树中的通知。

o 如果“必须”语句是“输入”语句的子语句, 上下文节点是表示操作中的节点 可访问的树。

o 如果“必须”语句是“输出”的子语句 语句,上下文节点是表示操作的节点 在可访问树中。

o 否则,上下文节点是可访问树中的节点 其中定义了“必须”语句。

RFC7950, Section 7.5.3

  • path语句:

The "path" XPath expression is conceptually evaluated in the following context, in addition to the definition in Section 6.4.1:

o 如果在 typedef 中定义了“路径”语句,上下文 node 是数据树中的叶节点或叶列表节点 引用 typedef。

o 否则,上下文节点是数据树中的节点 “路径”语句已定义。

RFC7950, Section 9.9.2

为了支持标准的 YANG XPath,需要实现所有这些规则,包括 current() 函数。

关于c - Yang 的 XPath current(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56375878/

相关文章:

c - APUE 对 linux 线程的描述有误吗?

xpath - 需要一些关于在 XPath 中获取最大值的解释

ruby - Hpricot XML 文本搜索

python - 在Python中使用Selenium选择子元素

csv - Google Sheets - Importxml - csv 文件解析

在 C 中从 float 转换为 unsigned char

objective-c - 如何创建具有 5 个 View 的基于选项卡栏的程序

C 程序将控制权交给 TCL/Tk GUI - 如何在关闭应用程序时运行代码?

c# - 命名空间 'xxx' 中的子元素 'http://yyy' 无效,而实际上 xml 中没有额外的命名空间设置

javascript - Node.js 中的 xml 宽度架构为 json