Skip to content
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: It does not read correctly the field tag from xml.Name when it unmarshals an XML from a string #59178

Closed
rm4n0s opened this issue Mar 22, 2023 · 6 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rm4n0s
Copy link

rm4n0s commented Mar 22, 2023

What version of Go are you using (go version)?

go version go1.20.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/job/.cache/go-build"
GOENV="/home/job/.config/go/env"
GOEXE=""
GOEXPERIMENT="arenas"
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/job/go-projects/pkg/mod"
GONOPROXY="*"
GONOSUMDB="*"
GOOS="linux"
GOPATH="/home/job/go-projects"
GOPRIVATE="*"
GOPROXY="direct"
GOROOT="/home/job/bin/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/home/job/bin/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/job/go-projects/work/smsgw3/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1489430774=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Tried to xml.Unmarshal a specific XML in a string format and it through an error.
Here is the link to the code https://go.dev/play/p/-eFryHvZ-0E

What did you expect to see?

I expected no error.

What did you see instead?

This error

expected element type <soapenv:Envelope> but have <Envelope>

It is like the xml.Unmarshal does not read the field tag of the XMLName.

@rm4n0s
Copy link
Author

rm4n0s commented Mar 22, 2023

This package github.com/nbio/xml seems to resolve the problem

Can you replicate the solution?

@heschi
Copy link
Contributor

heschi commented Mar 22, 2023

cc @rsc

@heschi heschi added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 22, 2023
@heschi heschi added this to the Backlog milestone Mar 22, 2023
@seankhliao
Copy link
Member

https://pkg.go.dev/encoding/xml#Name

A Name represents an XML name (Local) annotated with a name space identifier (Space). In tokens returned by Decoder.Token, the Space identifier is given as a canonical URL, not the short prefix used in the document being parsed.

The correct usage would be:

	XMLName                xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`

https://go.dev/play/p/WpKyJ8E0wCd

or without the namespace.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 24, 2023
@rm4n0s
Copy link
Author

rm4n0s commented Mar 24, 2023

Hi @seankhliao,
Looking at the results from https://go.dev/play/p/WpKyJ8E0wCd the EnvelopeNamespace, ShemaNamespace, and ShemaInstanceNamespace are empty.
These fields should not be empty.
Here is the same example and only replacing encoding/xml with github.com/nbio/xml https://go.dev/play/p/pUaXJ-l0ElK

@seankhliao
Copy link
Member

There are a lot of issues with namespaces, see #13400

but if you just want to unmarshal it, it's sufficient to drop the namespace
https://go.dev/play/p/9Q2ze_8WWIU

@rm4n0s
Copy link
Author

rm4n0s commented Mar 24, 2023

It seems this project https://github.com/nbio/xml solves all the problems that listed on #13400, why not copy-paste its solutions to encoding/xml?

@golang golang locked and limited conversation to collaborators Mar 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants