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: struct tag "a>b>c" not valid on XMLName fields #35676

Open
bemasher opened this issue Nov 18, 2019 · 2 comments

Comments

@bemasher
Copy link

@bemasher bemasher commented Nov 18, 2019

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

$ go version
go version go1.13.4 windows/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GOOS=windows
GOARCH=amd64

What did you do?

package main

import (
	"encoding/xml"
	"fmt"
)

type NestedField struct {
	Field string `xml:"a>b>c"`
}

type NestedNameStructTag struct {
	XMLName xml.Name `xml:"a>b>c"`
}

type NestedNameValue struct {
	XMLName xml.Name
}

func main() {
	nf := NestedField{}
	xmlBytes, _ := xml.MarshalIndent(nf, "", "    ")
	fmt.Println(string(xmlBytes))

	fmt.Println()

	nnst := NestedNameStructTag{}
	xmlBytes, _ = xml.MarshalIndent(nnst, "", "    ")
	fmt.Println(string(xmlBytes))

	fmt.Println()

	nnv := NestedNameValue{XMLName: xml.Name{Local: "a>b>c"}}
	xmlBytes, _ = xml.MarshalIndent(nnv, "", "    ")
	fmt.Println(string(xmlBytes))
}

Output:

<NestedField>
    <a>
        <b>
            <c></c>
        </b>
    </a>
</NestedField>

<a>b>c></a>b>c>

<a>b>c></a>b>c>

What did you expect to see?

Nested struct tags on XMLName fields should produce nested elements as shown in the "NestedField" example above.

Unless I've missed an exception in the documentation, this is what I expected.

What did you see instead?

The element name used the provided struct tag verbatim without escaping invalid characters or nesting elements as expected.

I think it's probably reasonable for marshal to behave this way, but the documentation should be updated to match.

Related issue:

#2119 encoding/xml: Marshal doesn't understand "parent>child" struct tags

@toothrot

This comment has been minimized.

Copy link
Contributor

@toothrot toothrot commented Nov 18, 2019

I agree that the behavior seems reasonable, and updating the documentation to mention that Name.Local is used verbatim seems helpful.

/cc @rsc who owns encoding/xml

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Nov 20, 2019

Change https://golang.org/cl/207850 mentions this issue: encoding/xml: clarify doc on XMLName and field tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.