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 }
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 }
func (d *Document) CreateElement(name string) (*Element, error) { if C.MY_test_node_name(stringToXmlChar(name)) == 0 { return nil, ErrInvalidNodeName } newNode := C.xmlNewNode(nil, stringToXmlChar(name)) if newNode == nil { return nil, errors.New("element creation failed") } // XXX hmmm... newNode.doc = d.ptr return wrapElement((*C.xmlElement)(unsafe.Pointer(newNode))), nil }
func (d *Document) CreateElementNS(nsuri, name string) (*Element, error) { if C.MY_test_node_name(stringToXmlChar(name)) == 0 { return nil, ErrInvalidNodeName } i := strings.IndexByte(name, ':') nsuriDup := stringToXmlChar(nsuri) prefix := stringToXmlChar(name[:i]) localname := stringToXmlChar(name[i+1:]) ns := C.xmlNewNs(nil, nsuriDup, prefix) newNode := C.xmlNewDocNode(d.ptr, ns, localname, nil) newNode.nsDef = ns return wrapElement((*C.xmlElement)(unsafe.Pointer(newNode))), nil }