1

There is the following program:

package main

import (
    "encoding/xml"
    "fmt"
)

type XMLData struct {
    XMLName xml.Name `xml:"tags"`
    Tags    []Tag    `xml:"tag"`
}

type Tag struct {
    Text string `xml:"text,attr"`
}

func main() {
    var XMLFile XMLDate
    XMLFile.Tags = append(XMLFile.Tags, Tag{"Yes, I'm."})

    out, err := xml.MarshalIndent(&XMLFile, "", "  ")
    if err != nil {
        panic(err)
    }

    fmt.Println(string(out))
}

At startup, I expect to get the following output:

<tags>
  <tag text="Yes, I'm."></tag>
</tags>

But instead of the expected result, I get the following:

<tags>
  <tag text="Yes, I&#39;m."></tag>
</tags>

Instead of the apostrophe symbol, I get its code. How can this be fixed?

12iq
  • 83
  • 5
  • 2
    There is nothing to "fix" in this output. That's the proper encoding for your data. It might _look_ ugly, but it's correct and safe. – Volker Aug 10 '23 at 10:25
  • 3
    This is explicitly allowed by [the spec](https://www.w3.org/TR/xml/#syntax): «To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as `"'"`, and the double-quote character (") as `"""`.» Technically, the encoder could try to not perform this "superficial" encoding, but the implemented behavior is simpler to code. As Volker said, after _decoding_ the value of that `text` attribute with any _conforming_ decoder, the resulting string of bytes will be the same no matter whether the symbol was escaped or not. – kostix Aug 10 '23 at 10:30

0 Answers0