Giter VIP home page Giter VIP logo

Comments (10)

lestrrat avatar lestrrat commented on August 17, 2024 1

meh. Okay, I will be offline for a few days, so I'll work on it when I come back.

from libxml2.

lestrrat avatar lestrrat commented on August 17, 2024

@guitarpawat Can you please check #103?

from libxml2.

guitarpawat avatar guitarpawat commented on August 17, 2024

@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.

lestrrat avatar lestrrat commented on August 17, 2024

@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.

guitarpawat avatar guitarpawat commented on August 17, 2024

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.

lestrrat avatar lestrrat commented on August 17, 2024

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.

guitarpawat avatar guitarpawat commented on August 17, 2024

@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.

guitarpawat avatar guitarpawat commented on August 17, 2024

Got it, thank you!

from libxml2.

github-actions avatar github-actions commented on August 17, 2024

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.

github-actions avatar github-actions commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.