问题
This is my follow up question for Xpath current() in Yang After the earlier discussion, I implemented a customized function current() for libxml2 with implementation shown below. The function works fine as expected when it evaluates Xpath expressions like:
leaf test-string {
type string;
must "current() > 0";
}
But when more complex expressions are involved like Xpath expresion "interface[name=current()/supporting-interface]/type = 'optical'", it does not seem to be working fine. Anything I am missing in the current() implementation. Given below is my implementation. Also is there a way to print the ultimate Xpath expression that gets constructed by xmlXPathEvalExpression() API?
/*
* 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");
}
回答1:
.
and current()
are not the same in YANG, though they may return the same result in certain situations. The result of .
changes with XPath path steps (the current context node, as you yourself refer to in your code), while current()
always returns the same node no matter where it is used within the same XPath expression - the initial context node.
The initial context node for an XPath expression in YANG depends on where this expression is defined with respect to YANG schema. A combination of rules in RFC7950, Section 6.4.1 and these are used to determine the initial context node:
when
statement:The XPath expression is conceptually evaluated in the following context, in addition to the definition in Section 6.4.1:
o If the "when" statement is a child of an "augment" statement, then the context node is the augment's target node in the data tree, if the target node is a data node. Otherwise, the context node is the closest ancestor node to the target node that is also a data node. If no such node exists, the context node is the root node. The accessible tree is tentatively altered during the processing of the XPath expression by removing all instances (if any) of the nodes added by the "augment" statement.
o If the "when" statement is a child of a "uses", "choice", or "case" statement, then the context node is the closest ancestor node to the node with the "when" statement that is also a data node. If no such node exists, the context node is the root node. The accessible tree is tentatively altered during the processing of the XPath expression by removing all instances (if any) of the nodes added by the "uses", "choice", or "case" statement.
o If the "when" statement is a child of any other data definition statement, the accessible tree is tentatively altered during the processing of the XPath expression by replacing all instances of the data node for which the "when" statement is defined with a single dummy node with the same name, but with no value and no children. If no such instance exists, the dummy node is tentatively created. The context node is this dummy node.
RFC7950, Section 7.21.5
must
statement:The XPath expression is conceptually evaluated in the following context, in addition to the definition in Section 6.4.1:
o If the "must" statement is a substatement of a "notification" statement, the context node is the node representing the notification in the accessible tree.
o If the "must" statement is a substatement of an "input" statement, the context node is the node representing the operation in the accessible tree.
o If the "must" statement is a substatement of an "output" statement, the context node is the node representing the operation in the accessible tree.
o Otherwise, the context node is the node in the accessible tree for which the "must" statement is defined.
RFC7950, Section 7.5.3
path
statement:The "path" XPath expression is conceptually evaluated in the following context, in addition to the definition in Section 6.4.1:
o If the "path" statement is defined within a typedef, the context node is the leaf or leaf-list node in the data tree that references the typedef.
o Otherwise, the context node is the node in the data tree for which the "path" statement is defined.
RFC7950, Section 9.9.2
All of these rules need to be implemented in order to support standard YANG XPath, current()
function included.
来源:https://stackoverflow.com/questions/56375878/xpath-current-for-yang