Exemple #1
0
func (d *Document) CreateAttributeNS(nsuri, k, v string) (*Attribute, error) {
	if nsuri == "" {
		return d.CreateAttribute(k, v)
	}

	kx := stringToXmlChar(k)
	if C.MY_test_node_name(kx) == 0 {
		return nil, ErrInvalidNodeName
	}

	root := d.DocumentElement()
	if root == nil {
		return nil, errors.New("attribute with namespaces require a root node")
	}

	prefix, local := splitPrefixLocal(k)

	ns := C.xmlSearchNsByHref(d.ptr, (*C.xmlNode)(root.pointer()), stringToXmlChar(nsuri))
	if ns == nil {
		ns = C.xmlNewNs((*C.xmlNode)(root.pointer()), stringToXmlChar(nsuri), stringToXmlChar(prefix))
		if ns == nil {
			return nil, errors.New("failed to create namespace")
		}
	}

	vx := stringToXmlChar(v)
	buf := C.xmlEncodeEntitiesReentrant(d.ptr, vx)
	newAttr := C.xmlNewDocProp(d.ptr, stringToXmlChar(local), buf)
	C.xmlSetNs((*C.xmlNode)(unsafe.Pointer(newAttr)), ns)

	return wrapAttribute((*C.xmlAttr)(unsafe.Pointer(newAttr))), nil
}
Exemple #2
0
// xmlNewDocProp
func (doc *Document) NewProp(name string, value string) *Attribute {
	ptrn := C.CString(name)
	defer C.free_string(ptrn)
	ptrv := C.CString(value)
	defer C.free_string(ptrv)
	cattr := C.xmlNewDocProp(doc.Ptr, C.to_xmlcharptr(ptrn), C.to_xmlcharptr(ptrv))
	return makeAttribute(cattr)
}
Exemple #3
0
func (d *Document) CreateAttribute(k, v string) (*Attribute, error) {
	kx := stringToXmlChar(k)
	vx := stringToXmlChar(v)
	if C.MY_test_node_name(kx) == 0 {
		return nil, ErrInvalidNodeName
	}

	buf := C.xmlEncodeEntitiesReentrant(d.ptr, vx)
	newAttr := C.xmlNewDocProp(d.ptr, kx, buf)

	return wrapAttribute((*C.xmlAttr)(unsafe.Pointer(newAttr))), nil
}