Exemple #1
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)
}
Exemple #2
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))
}
Exemple #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
}
Exemple #4
0
// The namespace should already be declared and in-scope when SetNsAttr is called.
// This restriction will be lifted in a future version.
func (xmlNode *XmlNode) SetNsAttr(href, name, value string) (val string) {
	val = value
	if xmlNode.NodeType() != XML_ELEMENT_NODE {
		return
	}
	nameBytes := GetCString([]byte(name))
	namePtr := unsafe.Pointer(&nameBytes[0])

	valueBytes := GetCString([]byte(value))
	valuePtr := unsafe.Pointer(&valueBytes[0])

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

	ns := C.xmlSearchNsByHref((*C.xmlDoc)(xmlNode.Document.DocPtr()), xmlNode.Ptr, (*C.xmlChar)(hrefPtr))
	if ns == nil {
		return
	}

	C.xmlSetNsProp(xmlNode.Ptr, ns, (*C.xmlChar)(namePtr), (*C.xmlChar)(valuePtr))
	return
}