forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Adding support for evaluating xpath expression (leafref, must, when expression) using customized open source xpath, xmlquery and jsonquery.
- Loading branch information
1 parent
5e2466b
commit 6f9535f
Showing
7 changed files
with
1,452 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
diff --git a/node.go b/node.go | ||
index e86c0c3..028867c 100644 | ||
--- a/node.go | ||
+++ b/node.go | ||
@@ -48,7 +48,7 @@ type Node struct { | ||
|
||
// InnerText returns the text between the start and end tags of the object. | ||
func (n *Node) InnerText() string { | ||
- var output func(*bytes.Buffer, *Node) | ||
+ /*var output func(*bytes.Buffer, *Node) | ||
output = func(buf *bytes.Buffer, n *Node) { | ||
switch n.Type { | ||
case TextNode: | ||
@@ -64,7 +64,18 @@ func (n *Node) InnerText() string { | ||
|
||
var buf bytes.Buffer | ||
output(&buf, n) | ||
- return buf.String() | ||
+ return buf.String()*/ | ||
+ | ||
+ if (n.Type == TextNode) { | ||
+ return n.Data | ||
+ } else if (n.Type == ElementNode) && | ||
+ (n.FirstChild != nil) && | ||
+ (n.FirstChild.Type == TextNode) { | ||
+ return n.FirstChild.Data | ||
+ } | ||
+ | ||
+ | ||
+ return "" | ||
} | ||
|
||
func (n *Node) sanitizedData(preserveSpaces bool) string { | ||
diff --git a/query.go b/query.go | ||
index 146c2a4..f21b61b 100644 | ||
--- a/query.go | ||
+++ b/query.go | ||
@@ -49,6 +49,29 @@ func CreateXPathNavigator(top *Node) *NodeNavigator { | ||
return &NodeNavigator{curr: top, root: top, attr: -1} | ||
} | ||
|
||
+//Evaluate XPath expression, the expression should evaluate to true or false | ||
+func Eval(top, ctx *Node, exp *xpath.Expr) bool { | ||
+ if exp == nil { | ||
+ return false | ||
+ } | ||
+ | ||
+ v := exp.Evaluate(&NodeNavigator{curr: ctx, ctxt: ctx, root: top, attr: -1}) | ||
+ | ||
+ switch val := v.(type) { | ||
+ case bool: | ||
+ return val | ||
+ case string: | ||
+ return (val != "") | ||
+ case float64: | ||
+ return (val != 0) | ||
+ case *xpath.NodeIterator: | ||
+ return (val != nil) | ||
+ } | ||
+ | ||
+ //return v.(bool) | ||
+ return false | ||
+} | ||
+ | ||
func getCurrentNode(it *xpath.NodeIterator) *Node { | ||
n := it.Current().(*NodeNavigator) | ||
if n.NodeType() == xpath.AttributeNode { | ||
@@ -145,7 +168,7 @@ func FindEachWithBreak(top *Node, expr string, cb func(int, *Node) bool) { | ||
} | ||
|
||
type NodeNavigator struct { | ||
- root, curr *Node | ||
+ root, curr, ctxt *Node | ||
attr int | ||
} | ||
|
||
@@ -212,6 +235,17 @@ func (x *NodeNavigator) MoveToRoot() { | ||
x.curr = x.root | ||
} | ||
|
||
+func (x *NodeNavigator) MoveToContext() { | ||
+ x.curr = x.ctxt | ||
+} | ||
+ | ||
+func (x *NodeNavigator) CurrentPrefix() string { | ||
+ if (x.ctxt != nil) { | ||
+ return x.ctxt.Prefix | ||
+ } | ||
+ return "" | ||
+} | ||
+ | ||
func (x *NodeNavigator) MoveToParent() bool { | ||
if x.attr != -1 { | ||
x.attr = -1 |
Oops, something went wrong.