// Evaluate an XPath. The returned result is stored in the struct. Call ReturnType to // discover the type of result, and call one of the ResultAs* functions to return a // copy of the result as a particular type. func (xpath *XPath) Evaluate(nodePtr unsafe.Pointer, xpathExpr *Expression) (err error) { if nodePtr == nil { //evaluating xpath on a nil node returns no result. return } oldXPContextDoc := xpath.ContextPtr.doc oldXPContextNode := xpath.ContextPtr.node oldXPProximityPosition := xpath.ContextPtr.proximityPosition oldXPContextSize := xpath.ContextPtr.contextSize oldXPNsNr := xpath.ContextPtr.nsNr oldXPNamespaces := xpath.ContextPtr.namespaces xpath.ContextPtr.node = (*C.xmlNode)(nodePtr) if xpath.ResultPtr != nil { C.xmlXPathFreeObject(xpath.ResultPtr) } xpath.ResultPtr = C.xmlXPathCompiledEval(xpathExpr.Ptr, xpath.ContextPtr) xpath.ContextPtr.doc = oldXPContextDoc xpath.ContextPtr.node = oldXPContextNode xpath.ContextPtr.proximityPosition = oldXPProximityPosition xpath.ContextPtr.contextSize = oldXPContextSize xpath.ContextPtr.nsNr = oldXPNsNr xpath.ContextPtr.namespaces = oldXPNamespaces if xpath.ResultPtr == nil { err = errors.New("err in evaluating xpath: " + xpathExpr.String()) return } return }
//need to add an error as a return value b/c xpath evaluation can return error func (xpath *XPath) Evaluate(nodePtr unsafe.Pointer, xpathExpr *Expression) (nodes []unsafe.Pointer, err error) { if nodePtr == nil { //evaluating xpath on a nil node returns no result. return } xpath.ContextPtr.node = (*C.xmlNode)(nodePtr) if xpath.ResultPtr != nil { C.xmlXPathFreeObject(xpath.ResultPtr) } xpath.ResultPtr = C.xmlXPathCompiledEval(xpathExpr.Ptr, xpath.ContextPtr) if xpath.ResultPtr == nil { err = errors.New("err in evaluating xpath: " + xpathExpr.String()) return } if nodesetPtr := xpath.ResultPtr.nodesetval; nodesetPtr != nil { if nodesetSize := int(nodesetPtr.nodeNr); nodesetSize > 0 { nodes = make([]unsafe.Pointer, nodesetSize) for i := 0; i < nodesetSize; i++ { nodes[i] = unsafe.Pointer(C.fetchNode(nodesetPtr, C.int(i))) } } } return }
func (x *XPathContext) evalXPath(expr *XPathExpression) (*XPathObject, error) { if expr == nil { return nil, errors.New("empty XPathExpression") } // If there is no document associated with this context, // then xmlXPathCompiledEval() just fails to match ctx := x.ptr if ctx.node != nil && ctx.node.doc != nil { ctx.doc = ctx.node.doc } if ctx.doc == nil { ctx.doc = C.xmlNewDoc(stringToXmlChar("1.0")) defer C.xmlFreeDoc(ctx.doc) } res := C.xmlXPathCompiledEval(expr.ptr, ctx) if res == nil { return nil, errors.New("empty result") } return &XPathObject{ptr: res}, nil }
func (xpath *XPath) Evaluate(nodePtr unsafe.Pointer, xpathExpr *Expression) (nodes []unsafe.Pointer) { if nodePtr == nil { return } xpath.ContextPtr.node = (*C.xmlNode)(nodePtr) if xpath.ResultPtr != nil { C.xmlXPathFreeObject(xpath.ResultPtr) } xpath.ResultPtr = C.xmlXPathCompiledEval(xpathExpr.Ptr, xpath.ContextPtr) if nodesetPtr := xpath.ResultPtr.nodesetval; nodesetPtr != nil { if nodesetSize := int(nodesetPtr.nodeNr); nodesetSize > 0 { nodes = make([]unsafe.Pointer, nodesetSize) for i := 0; i < nodesetSize; i++ { nodes[i] = unsafe.Pointer(C.fetchNode(nodesetPtr, C.int(i))) } } } return }