Closed bergner closed 6 years ago
This patch shows promising results. Calling node.SetNamespace(uri, "", true)
now adds an explicit xmlns="..."
declaration.
diff --git a/clib/clib.go b/clib/clib.go
index 29c0159..478df6a 100644
--- a/clib/clib.go
+++ b/clib/clib.go
@@ -730,15 +730,18 @@ func XMLNewDocNode(doc PtrSource, ns PtrSource, local, content string) (uintptr,
}
func XMLNewNs(n PtrSource, nsuri, prefix string) (uintptr, error) {
+ var cprefix *C.xmlChar = nil
nptr, err := validNodePtr(n)
if err != nil {
return 0, err
}
cnsuri := stringToXMLChar(nsuri)
- cprefix := stringToXMLChar(prefix)
+ if prefix != "" {
+ cprefix = stringToXMLChar(prefix)
+ defer C.free(unsafe.Pointer(cprefix))
+ }
defer C.free(unsafe.Pointer(cnsuri))
- defer C.free(unsafe.Pointer(cprefix))
nsptr := C.xmlNewNs(nptr, cnsuri, cprefix)
if nsptr == nil {
diff --git a/dom/node_element.go b/dom/node_element.go
index aed7823..55c9387 100644
--- a/dom/node_element.go
+++ b/dom/node_element.go
@@ -44,9 +44,6 @@ func (n *Element) SetNamespace(uri, prefix string, activate ...bool) error {
if uri == "" {
return errors.New("missing uri for SetNamespace")
}
- if prefix == "" {
- return errors.New("missing prefix for SetNamespace")
- }
ns, err := clib.XMLNewNs(n, uri, prefix)
if err != nil {
@bergner Sorry for the non-responsiveness. For whatever reason I missed the notifications :/
Lets say I want to build the following xml:
With go-libxml2 I would do this:
But that does not produce the expected xml, instead I get the following which is missing the xmlns declaration on
<elem>
If I write the equivalent code in Perl with XML::LibXML that go-libxml2 is inspired by I get the correct result with a xmlns declaration on
<elem>