Exemplo n.º 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)
}
Exemplo n.º 2
0
func (xmlNode *XmlNode) RecursivelyRemoveNamespaces() (err error) {
	nodePtr := xmlNode.Ptr
	C.xmlSetNs(nodePtr, nil)

	for child := xmlNode.FirstChild(); child != nil; {
		child.RecursivelyRemoveNamespaces()
		child = child.NextSibling()
	}

	nodeType := xmlNode.NodeType()

	if ((nodeType == XML_ELEMENT_NODE) ||
		(nodeType == XML_XINCLUDE_START) ||
		(nodeType == XML_XINCLUDE_END)) &&
		(nodePtr.nsDef != nil) {
		C.xmlFreeNsList((*C.xmlNs)(nodePtr.nsDef))
		nodePtr.nsDef = nil
	}

	if nodeType == XML_ELEMENT_NODE && nodePtr.properties != nil {
		property := nodePtr.properties
		for property != nil {
			if property.ns != nil {
				property.ns = nil
			}
			property = property.next
		}
	}
	return
}
Exemplo 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
}
Exemplo n.º 4
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)
}