Ejemplo n.º 1
0
// QueryNumber evaluates an xpath expressions that returns a number
func (xp *Xp) QueryNumber(context *C.xmlNode, path string) (val int) {
	var xmlXPathObject *C.xmlXPathObject
	if xmlXPathObject = xp.xmlXPathEvalExpression(context, path); xmlXPathObject == nil {
		return 0
	}
	defer C.xmlXPathFreeNodeSetList(xmlXPathObject)

	if xmlXPathObject._type == C.XPATH_NUMBER {
		val = int(xmlXPathObject.floatval)
	}
	return
}
Ejemplo n.º 2
0
// Query Do a xpath query with the given context
// returns a slice of nodes
func (xp *Xp) Query(context *C.xmlNode, path string) (nodes []*C.xmlNode) {
	var xmlXPathObject *C.xmlXPathObject
	if xmlXPathObject = xp.xmlXPathEvalExpression(context, path); xmlXPathObject == nil {
		return nil
	}

	defer C.xmlXPathFreeNodeSetList(xmlXPathObject)

	// curtesy of https://github.com/moovweb/gokogiri/blob/master/xpath/xpath.go#L164

	if nodesetPtr := xmlXPathObject.nodesetval; nodesetPtr != nil {
		if nodesetSize := int(nodesetPtr.nodeNr); nodesetSize > 0 {
			nodes = make([]*C.xmlNode, nodesetSize)
			for i := 0; i < nodesetSize; i++ {
				nodes[i] = C.fetchNode(nodesetPtr, C.int(i))
			}
		}
	}
	return
}
Ejemplo n.º 3
0
// xmlXPathFreeNodeSetList
func (obj *XPathObject) FreeList() {
	C.xmlXPathFreeNodeSetList(obj.Ptr)
	obj.Ptr = nil
}