-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
encoding/xml: cannot marshal self-closing tag #21399
Comments
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. |
Change https://golang.org/cl/59830 mentions this issue: |
CC @rsc XML suggestion. |
The proposed patch only works for marshaling structs. What if we're using the |
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. |
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. |
Is there a benefit to only doing this for some elements and not for others? |
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. |
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 |
fair. I'll change the patches. Should/can I tag you for review? |
I don't have +2 permissions, but I'm happy to run trybots and provide feedback (also, setting this on the Encoder is my suggestion, but I don't actually have any authority here, so grain of salt until rsc or someone replies :) ) |
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, |
Sorry, but we're not going to get to this for Go 1.10. |
The current behaviour of documented tag "omitempty" allows the following workaround. |
@iwdgo how does this solve the original question? The desire is to produce either |
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>. |
@robzon see the milestone; this issue is marked for Go 1.13. EDIT: I should have also said: "please file an issue against those clients when you find them; they are broken". |
It's been modified several times, so I'm not sure how reliable this is. Don't want to come across as complaining, I'm just curious on what can be realistically expected as I'm sure it's not a high priority thing. |
This comment was marked as spam.
This comment was marked as spam.
1 similar comment
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
is anyone aware of a custom marshaller example that can achieve self-closing aka empty tags? |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I'm just running into this problem myself. I'd be interested in a fix too. Side-note: This should probably be
To be clear, this token would only be accepted by An alternative might be to add an Personally, I consider it more important to support the low-level (i.e. |
Yeah. |
I'm curious what "clean" solution / workaround people have come up with to write self-closing elements with |
@jybp we use this func forceSelfClosingTags(b []byte) []byte {
emptyTagIdxs := regexp.MustCompile(`<(\w+)></\w+>`).FindAllSubmatchIndex(b, -1)
var nb []byte
for _, idx := range emptyTagIdxs {
// get everything in b up till the first of the submatch indexes (this is the start of an
// "empty" <thing></thing> tag), then get the name of the tag and put it in a self-closing
// tag.
nb = append(b[0:idx[0]], fmt.Sprintf("<%s/>", b[idx[2]:idx[3]])...) //nolint: gocritic
// finally, append everything *after* the submatch indexes
nb = append(nb, b[len(b)-(len(b)-idx[1]):]...)
}
return nb
} |
I have been looking for this since I started writing Go and my original issue #6896 and still running into this. In my particular use case a self-closing is being used (probably improperly by the API) to be a bool. If it exists then we get a "true" if it's missing we get a "false". There is a second use case where self-closed tags are used as variables, but there are ok workarounds for that. It seems a custom type would be needed to follow the proposals "allowempty" proposal to be used a extant bool, but there are at least workarounds. Given a "RawXML" token type we could have a proper workaround. #26756 |
Change https://go.dev/cl/469495 mentions this issue: |
Do this import (
"regexp"
)
func SelfClosing(xml []byte) ([]byte, error) {
regex, err := regexp.Compile(`></[A-Za-z0-9_]+>`)
return regex.ReplaceAll(xml, []byte("/>")), err
} |
@StEvUgnIn Pretty much eliminates stream mode and has overhead with regexp matching. |
It's also not correct - it would, for example, also apply the replacement in comments. |
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: |
the following case not work:
the |
the repo seems fine: https://github.com/ECUST-XX/xml/tree/master |
@ttys3 thanks! |
See #69273 for a concrete proposal. |
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
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
The text was updated successfully, but these errors were encountered: