Ejemplo n.º 1
0
// This is typically done on the root element or node high up in the tree
// to avoid duplication. The declaration is not created if the namespace
// is already declared in this scope with the same prefix.
func (xmlNode *XmlNode) DeclareNamespace(prefix, href string) {
	//can only declare namespaces on elements
	if xmlNode.NodeType() != XML_ELEMENT_NODE {
		return
	}
	hrefBytes := GetCString([]byte(href))
	hrefPtr := unsafe.Pointer(&hrefBytes[0])

	//if the namespace is already declared using this prefix, just return
	_ns := C.xmlSearchNsByHref((*C.xmlDoc)(xmlNode.Document.DocPtr()), xmlNode.Ptr, (*C.xmlChar)(hrefPtr))
	if _ns != nil {
		_prefixPtr := unsafe.Pointer(_ns.prefix)
		_prefix := C.GoString((*C.char)(_prefixPtr))
		if prefix == _prefix {
			return
		}
	}

	prefixBytes := GetCString([]byte(prefix))
	prefixPtr := unsafe.Pointer(&prefixBytes[0])
	if prefix == "" {
		prefixPtr = nil
	}

	//this adds the namespace declaration to the node
	_ = C.xmlNewNs(xmlNode.Ptr, (*C.xmlChar)(hrefPtr), (*C.xmlChar)(prefixPtr))
}
Ejemplo n.º 2
0
// Set the namespace of an element.
func (xmlNode *XmlNode) SetNamespace(prefix, href string) {
	if xmlNode.NodeType() != XML_ELEMENT_NODE {
		return
	}

	prefixBytes := GetCString([]byte(prefix))
	prefixPtr := unsafe.Pointer(&prefixBytes[0])
	if prefix == "" {
		prefixPtr = nil
	}

	hrefBytes := GetCString([]byte(href))
	hrefPtr := unsafe.Pointer(&hrefBytes[0])

	// use the existing namespace declaration if there is one
	_ns := C.xmlSearchNsByHref((*C.xmlDoc)(xmlNode.Document.DocPtr()), xmlNode.Ptr, (*C.xmlChar)(hrefPtr))
	if _ns != nil {
		_prefixPtr := unsafe.Pointer(_ns.prefix)
		_prefix := C.GoString((*C.char)(_prefixPtr))
		if prefix == _prefix {
			C.xmlSetNs(xmlNode.Ptr, _ns)
			return
		}
	}

	ns := C.xmlNewNs(xmlNode.Ptr, (*C.xmlChar)(hrefPtr), (*C.xmlChar)(prefixPtr))
	C.xmlSetNs(xmlNode.Ptr, ns)
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
0
// xmlNewNs
func (node *Node) NewNs(href string, prefix string) *Namespace {
	ptrh := C.CString(href)
	defer C.free_string(ptrh)
	ptrp := C.CString(prefix)
	defer C.free_string(ptrp)
	return makeNamespace(C.xmlNewNs(node.Ptr, C.to_xmlcharptr(ptrh), C.to_xmlcharptr(ptrp)))
}
Ejemplo n.º 5
0
func (xmlNode *XmlNode) SetNamespace(prefix, href string) {
	if xmlNode.NodeType() != XML_ELEMENT_NODE {
		return
	}

	prefixBytes := GetCString([]byte(prefix))
	prefixPtr := unsafe.Pointer(&prefixBytes[0])

	hrefBytes := GetCString([]byte(href))
	hrefPtr := unsafe.Pointer(&hrefBytes[0])

	ns := C.xmlNewNs(xmlNode.Ptr, (*C.xmlChar)(hrefPtr), (*C.xmlChar)(prefixPtr))
	C.xmlSetNs(xmlNode.Ptr, ns)
}
Ejemplo n.º 6
0
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
}
Ejemplo n.º 7
0
// CreateElementNS Create an element with the given namespace
func (xp *Xp) createElementNS(prefix, element string, context *C.xmlNode, before *C.xmlNode) (newcontext *C.xmlNode) {

	ns := C.xmlNewNs(nil, namespaces[prefix].ns_uri, namespaces[prefix].prefix) // candidate for cache ...
	if ns == nil {
		panic("ns is nil")
	}
	celement := unsafe.Pointer(C.CString(element))
	newelement := C.xmlNewDocNode(xp.doc, ns, (*C.xmlChar)(celement), nil)
	C.free(celement)

	if before != nil {
		newcontext = C.xmlAddPrevSibling(before, newelement)
	} else {
		if context == nil {
			context = C.xmlDocGetRootElement(xp.doc)
		}
		newcontext = C.xmlAddChild(context, newelement)
	}
	return
}