func NewXPath(docPtr unsafe.Pointer) (xpath *XPath) { if docPtr == nil { return } xpath = &XPath{ContextPtr: C.xmlXPathNewContext((*C.xmlDoc)(docPtr)), ResultPtr: nil} runtime.SetFinalizer(xpath, (*XPath).Free) return }
func (d *XmlDoc) XPath(xpathExpr string) *XPathResult { context := C.xmlXPathNewContext(d.Ptr) defer C.xmlXPathFreeContext(context) result := C.xmlXPathEvalExpression(stringToXmlChar(xpathExpr), context) fmt.Println("XPath: ", xpathExpr, " Type: ", result._type, " Result: ", result) return &XPathResult{ptr: result} }
// Note that although we are specifying `n... Node` for the argument, // only the first, node is considered for the context node func NewXPathContext(n ...Node) (*XPathContext, error) { ctx := C.xmlXPathNewContext(nil) ctx.namespaces = nil obj := &XPathContext{ptr: ctx} if len(n) > 0 { obj.SetContextNode(n[0]) } return obj, nil }
func (n *XmlNode) XPath(xpathExpr string) *XPathResult { context := C.xmlXPathNewContext((C.xmlDocPtr)(unsafe.Pointer(n.Ptr.doc))) context.node = n.Ptr defer C.xmlXPathFreeContext(context) result := C.xmlXPathEvalExpression(stringToXmlChar(xpathExpr), context) fmt.Println("XPath: ", xpathExpr, " Type: ", result._type, " Result: ", result) return &XPathResult{ptr: result} }
// Parse html object with doc - used in testing for "forwarding" samlresponses from html to http // Disables error reporting - libxml2 complains about html5 elements func NewHtmlXp(html []byte) *Xp { x := new(Xp) //x.doc = C.htmlParseDoc((*C.xmlChar)(unsafe.Pointer(&html[0])), nil) ctxt := C.htmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&html[0])), C.int(len(html))) C.htmlCtxtUseOptions(ctxt, C.HTML_PARSE_NOERROR) C.htmlParseDocument(ctxt) x.doc = ctxt.myDoc C.htmlFreeParserCtxt(ctxt) x.xpathCtx = C.xmlXPathNewContext(x.doc) runtime.SetFinalizer(x, (*Xp).free) return x }
// Make a copy of the Xp object - shares the document with the source, but allocates a new xmlXPathContext because // they are not thread/gorutine safe as the context is set for each query call // Only the document "owning" Xp releases the C level document and it needs be around as long as any copies - ie. do // not let the original document be garbage collected or havoc will be wreaked func (src *Xp) CpXp() (xp *Xp) { xp = new(Xp) xp.doc = src.doc xp.master = src //xp.doc = C.xmlCopyDoc(src.doc, 1) xp.xpathCtx = C.xmlXPathNewContext(xp.doc) runtime.SetFinalizer(xp, (*Xp).freexpathCtx) for _, ns := range namespaces { C.xmlXPathRegisterNs(xp.xpathCtx, ns.prefix, ns.ns_uri) } return }
// Parse SAML xml to Xp object with doc and xpath with relevant namespaces registered func NewXp(xml []byte) *Xp { x := new(Xp) if len(xml) == 0 { x.doc = C.xmlNewDoc((*C.xmlChar)(unsafe.Pointer(C.CString("1.0")))) } else { x.doc = C.xmlParseMemory((*C.char)(unsafe.Pointer(&xml[0])), C.int(len(xml))) } x.xpathCtx = C.xmlXPathNewContext(x.doc) runtime.SetFinalizer(x, (*Xp).free) for _, ns := range namespaces { C.xmlXPathRegisterNs(x.xpathCtx, ns.prefix, ns.ns_uri) } return x }
// xmlXPathNewContext func NewXPathContext(doc *Document) *XPathContext { return &XPathContext{C.xmlXPathNewContext(doc.Ptr)} }