func (x *XPathContext) RegisterNs(name, nsuri string) error { res := C.xmlXPathRegisterNs(x.ptr, stringToXmlChar(name), stringToXmlChar(nsuri)) if res == -1 { return errors.New("cannot register namespace") } return nil }
// 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 }
func (xpath *XPath) RegisterNamespace(prefix, href string) bool { var prefixPtr unsafe.Pointer = nil if len(prefix) > 0 { prefixBytes := AppendCStringTerminator([]byte(prefix)) prefixPtr = unsafe.Pointer(&prefixBytes[0]) } var hrefPtr unsafe.Pointer = nil if len(href) > 0 { hrefBytes := AppendCStringTerminator([]byte(href)) hrefPtr = unsafe.Pointer(&hrefBytes[0]) } result := C.xmlXPathRegisterNs(xpath.ContextPtr, (*C.xmlChar)(prefixPtr), (*C.xmlChar)(hrefPtr)) return result == 0 }