Skip to content

encoding/xml: struct tag "a>b>c" not valid on XMLName fields #35676

Open
@bemasher

Description

@bemasher

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions