Dasel don't preserve initial XML content.
Discussion? While trying to use Dasel as a Go package in a personal project updatecli, I noticed that it drops XML comments, directives, and processes.
Describe the bug Dasel don't preserve initial XML content.
To Reproduce
The command dasel -f example.xml -p xml
.example.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This a commment -->
<name>
<firstname tag="xx">John</firstname>
<lastname>Doe</lastname>
</name>
.output
<name>
<firstname tag="xx">John</firstname>
<lastname>Doe</lastname>
</name>
.expectedOutput
<?xml version="1.0" encoding="UTF-8"?>
<!-- This a commment -->
<name>
<firstname tag="xx">John</firstname>
<lastname>Doe</lastname>
</name>
Another example is the command dasel put string -p xml -f example.xml .name.firstname Tom on the same example file, generated the following file:
<name>
<firstname>Tom</firstname>
<lastname>Doe</lastname>
</name>
Instead of:
.expectedOutput
<?xml version="1.0" encoding="UTF-8"?>
<!-- This a commment -->
<name>
<firstname tag="xx">John</firstname>
<lastname>Doe</lastname>
</name>
Expected behavior I would expect Dasel to preserve all original XML instructions
Screenshots /
Desktop (please complete the following information):
- OS: Linux
- Version [1.21.2]
Additional context After looking at Dasel and mxj code base, It appears that Dasel is manipulating a mxj.Map and could instead use the mxj.MapSeq which seems to preserve everything. I am still new to the two projects so I may need some guidance to contribute to this issue. It appears that some work is needed here but I may be missing something else.
That file is indeed the one that needs editing.
I have done a quick test and found that simply using mxj.NewMapXmlSeq instead of mxj.NewMapXml doesn't work.
I'll have to do a bit more reading to find out how to correctly use mxj to solve this issue. Please bear with me!
It seems that when using NewMapXmlSeq the XML version and comment information are added as map nodes at the root level causing confusion.
@TomWright Thanks for looking at this, I started looking at this comment https://github.com/clbanning/mxj/issues/17#issuecomment-947056926 but unfortunately, I run out of time so I can't help as much as I would like to.