Comments (10)
meh. Okay, I will be offline for a few days, so I'll work on it when I come back.
from libxml2.
@guitarpawat Can you please check #103?
from libxml2.
@lestrrat Thanks for a quick response, it seems like your commit doesn't set parent element to the namespace
<?xml version="1.0" encoding="utf-8"?>
<Document>
<ex:Test xmlns:ex="https://example.com">test</ex:Test>
</Document>
And it still have duplicate xmlns
if we set the namespace for parent after child
func main() {
root := dom.CreateDocument()
doc, _ := root.CreateElement("Document")
//_ = doc.SetNamespace("https://example.com", "ex") // delete
_ = root.SetDocumentElement(doc)
test, _ := root.CreateElement("Test")
_ = test.SetNamespace("https://example.com", "ex")
_ = doc.SetNamespace("https://example.com", "ex") // move to here
_ = doc.AddChild(test)
test.SetNodeValue("test")
fmt.Println(root.Dump(true))
}
<ex:Document xmlns:ex="https://example.com">
<ex:Test xmlns:ex="https://example.com">test</ex:Test>
</ex:Document>
from libxml2.
@guitarpawat Oh, I found your problem. xmlSearchNs
can only find duplicate namespaces if the node in question is already part of a document. You are setting the namespace and then adding the node, that's why it failed to find your namespace.
t.Run("child ns after parent ns", func(t *testing.T) {
root := CreateDocument()
doc, _ := root.CreateElement("Document")
_ = doc.SetNamespace("https://example.com", "ex")
test, _ := root.CreateElement("Test")
_ = doc.AddChild(test) // <---- THIS needs to go BEFORE SetNamespace
_ = root.SetDocumentElement(doc)
_ = test.SetNamespace("https://example.com", "ex")
test.SetNodeValue("test")
// root.Dump(true) should only contain one namespace declaration
// for the 'ex' prefix
dump := root.Dump(true)
count := strings.Count(dump, `xmlns:ex="https://example.com"`)
t.Logf("%s", dump)
require.Equal(t, 1, count, "expected only one namespace declaration for 'ex' prefix")
})
Also, it is NOT possible to find duplicates on children. So if you add a namespace on your child node and then add to a parent, that's not going to be fixed.
I added a minor fix to #103 including fixes to the test, and I believe it's working as intended now.
from libxml2.
Hi @lestrrat, I pulled your latest version from #103 (commit b1f63e9) and it is still not working for me
The xmlns is gone in child nodes but it also doesn't have a namespace prefix
package main
import (
"fmt"
"github.com/lestrrat-go/libxml2/dom"
)
func main() {
root := dom.CreateDocument()
doc, _ := root.CreateElement("Document")
_ = root.SetDocumentElement(doc)
_ = doc.SetNamespace("https://example.com", "ex")
test, _ := root.CreateElement("Test")
_ = doc.AddChild(test)
_ = test.SetNamespace("https://example.com", "ex")
test2, _ := root.CreateElement("Test2")
_ = test.AddChild(test2)
_ = test2.SetNamespace("https://example.com", "ex")
fmt.Println(root.Dump(true))
}
<?xml version="1.0" encoding="utf-8"?>
<ex:Document xmlns:ex="https://example.com">
<Test>
<Test2/>
</Test>
</ex: Document>
For clarification, I expect my message to look like this spec.
<ex:Document xmlns:ex="https://example.com">
<ex:Test>
<ex:Test2/>
</ex:Test>
</ex:Document>
I managed to find a workaround by using CreateElementNS
instead but it still has another issue with default namespace (will open another issue):
root := dom.CreateDocument()
doc, _ := root.CreateElementNS("https://example.com", "ex:Document")
_ = root.SetDocumentElement(doc)
test, _ := root.CreateElementNS("https://example.com", "ex:Test")
_ = doc.AddChild(test)
test2, _ := root.CreateElementNS("https://example.com", "ex:Test2")
_ = test.AddChild(test2)
fmt.Println(root.Dump(true))
from libxml2.
Oh, oops?
~/dev/src/github.com/lestrrat-go/libxml2$ git push origin gh-102
To github.com:lestrrat-go/libxml2.git
! [rejected] gh-102 -> gh-102 (fetch first)
error: failed to push some refs to 'github.com:lestrrat-go/libxml2.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
How about now?
from libxml2.
@lestrrat I pulled commit 38fc65c
Child nodes still don't have prefix.
package main
import (
"fmt"
"github.com/lestrrat-go/libxml2/dom"
)
func main() {
root := dom.CreateDocument()
doc, _ := root.CreateElement("Document")
_ = doc.SetNamespace("https://example.com", "ex")
test, _ := root.CreateElement("Test")
_ = doc.AddChild(test) // <---- THIS needs to go BEFORE SetNamespace
_ = root.SetDocumentElement(doc)
_ = test.SetNamespace("https://example.com", "ex")
test.SetNodeValue("test")
fmt.Println(root.Dump(true))
}
<?xml version="1.0" encoding="utf-8"?>
<ex:Document xmlns:ex="https://example.com">
<Test>test</Test> <- this should be ex:Test
</ex:Document>
from libxml2.
Got it, thank you!
from libxml2.
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.
from libxml2.
This issue was closed because it has been stalled for 7 days with no activity. This does not mean your issue is rejected, but rather it is done to hide it from the view of the maintains for the time being. Feel free to reopen if you have new comments
from libxml2.
Related Issues (20)
- ERROR pkg-config --cflags -- libxml-2.0 HOT 4
- About HTMLParser encoding HOT 8
- How to remove node HOT 1
- How to remove a node? HOT 11
- docker build found clib undefined HOT 3
- Support for RelaxNG schemas HOT 5
- [Regression] Default values from XSD causes validation to fail HOT 3
- Cross-compiling from linux to windows HOT 8
- Memory leak when parsing XSDs HOT 2
- Unsafe pointer usage HOT 2
- parser error : internal error: Huge input lookup HOT 3
- RemoveChild() for AttributeNode HOT 3
- Trying to compile project using libxml2 produces errors in the module itself. HOT 1
- Newer versions of libxml doesn't have stdlib.h include HOT 1
- [WIP] future: Maintenance and releases
- https://github.com/lestrrat-go/libxml2/blob/master/clib/clib.go Package Build is failing with "could not determine kind of name for C.free" HOT 9
- Incorrect element name when creating element using `CreateElementNS` without namespace prefix HOT 2
- Node.String()/Node.ToString indentation not working as expected HOT 1
- Error details leaking
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libxml2.