go: encoding/xml: cannot marshal self-closing tag
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version
)?
go version go1.8.3 darwin/amd64
What operating system and processor architecture are you using (go env
)?
GOARCH=“amd64” GOBIN=“” GOEXE=“” GOHOSTARCH=“amd64” GOHOSTOS=“darwin” GOOS=“darwin” GOPATH=“/Users/xxx/.go:/Users/xxx/workspace/goproject” GORACE=“” GOROOT=“/usr/local/Cellar/go/1.8.3/libexec” GOTOOLDIR=“/usr/local/Cellar/go/1.8.3/libexec/pkg/tool/darwin_amd64” GCCGO=“gccgo” CC=“clang” GOGCCFLAGS=“-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/53/r61bh2fj1hg9n0w03fs__7kc0000gn/T/go-build440341166=/tmp/go-build -gno-record-gcc-switches -fno-common” CXX=“clang++” CGO_ENABLED=“1” PKG_CONFIG=“pkg-config” CGO_CFLAGS=“-g -O2” CGO_CPPFLAGS=“” CGO_CXXFLAGS=“-g -O2” CGO_FFLAGS=“-g -O2” CGO_LDFLAGS=“-g -O2”
What did you do?
I want to marshal a xml struct which has a self-closing tag field. After looking up in golang doc, there isn’t an official support. In google group they use string.Replace, it’s ugly. It seems we could write marshal func for the custom struct, by i’m willing to see an official support like
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
Balabala string `xml:"balabala,allowempty"` // i want something like this
}
In xml standard, a self-closing tag is permitted. https://www.w3.org/TR/REC-xml/#dt-empty
And empty-element tag should be used, and should only be used, for elements which are declared EMPTY. https://www.w3.org/TR/REC-xml/#d0e2480
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 58
- Comments: 51 (30 by maintainers)
Any news on this?
Any news?
Any news?
This is a good idea since I’m running into the same issue. Is this idea still active or is it “dead”?
Do this
Any chances of this being addressed? Stumbled upon this issue while working with the XMPP protocol. Some clients will only accept <required/>, but not <required></required>.
Change https://go.dev/cl/469495 mentions this issue:
encoding/xml: Add EmptyElement token.
We still need the ability to output non-selfclosing pairs of tags. The idea is to give the user the option to make certain fields self closing, not to do that for all fields.
just created a mod with patch in https://go.dev/cl/469495 applied.
and fixed a indent issue. in case someone need a workaround, here is the repo:
https://github.com/ttys3/go-xml#usage
is anyone aware of a custom marshaller example that can achieve self-closing aka empty tags?
@iWdGo how does this solve the original question?
The desire is to produce either
<tag />
or<tag>notempty</tag>
. Not<tag></tag>
.For the same reason this bug exists in the first place; not all XML parsers handle this correctly and I wanted maximum flexibility.
I’m fine with doing it the other way though.
Change https://golang.org/cl/59830 mentions this issue:
encoding/xml: add 'allowempty' tag for self-closing XML tags
I like this idea; though i suspect the community will have to agree on it (and also the naming of the tag)
However, I whipped up a quick implementation at https://go-review.googlesource.com/59830 .
I’ll polish it up and add tests if this proposal actually goes through.
It’s also not correct - it would, for example, also apply the replacement in comments.
@jybp we use this
There is, we simply support this for marshalling structs – the primary use case – and not for using EncodeToken. (which is what I’ve been suggesting)
What we can also do is allow
Token
to have aSelfClosingElement
variant so that logic for choosing when to emit empty tags lives in the callerThanks, I’ll mark this as a release blocker for 1.13 so that we decide on that CL.
Go is an open source project. Want to send a patch?
We hope to do a general overhaul of the JSON and XML patches and feature requests in the 1.13 timeframe, but we hoped to do it for 1.12 also and didn’t make it.
Can we expect this to be implemented in Go 1.10?
Currently we have a Go library, that needs to communicate with a remote API endpoint, which can only understand empty XML elements with self-closing tags.
With the current implementation of XML package we are not able to use Go in order to communicate with that API endpoint. I realize that this is an issue of the remote side by not supporting both ways of specifying empty elements, but having the option to define how empty elements are defined in Go structs would be very useful and would allow us to overcome such edge cases.
Thanks, Marin
If a parser did not handle this you couldn’t mix the two anyways, you’d just have to turn it on or off entirely to make sure that parser would work.
In case there’s confusion, I’m not suggesting that we always do self-closing tags or non-self-closing tags, but that an individual
*Encoder
should be configurable to do one or the other.Oh, I see, you’re suggesting this be global. Hm. I don’t really like the idea of making this a global change, but we could also make it an option on the marshaler.