XML attribute with namespace

238 views
Skip to first unread message

chris.j...@gmail.com

unread,
Apr 15, 2012, 1:17:37 PM4/15/12
to golan...@googlegroups.com
Lots of XML-based formats include an xml:lang attribute on various elements. Reading encoding/xml/typeinfo.go, it looks like this is supposed to work; but I don't see any tests for this functionality, and I can't get it to work in my own code. Here's an example:

package main

import (
    "fmt"
    "encoding/xml"
)

type foo struct {
    XMLName xml.Name `xml:"ns element"`
    Attr1 string `xml:"attr1,attr"`
    Lang string `xml:"xml lang,attr"`
}

func main() {
    f := &foo{XMLName:xml.Name{Space: "ns", Local: "element"}, Attr1: "val1", Lang: "en"}
    b, err := xml.Marshal(f)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(b))
}

I'm expecting it to print something with xml:lang="en", but it prints lang="en" instead. Is this something I'm doing wrong, or a bug in encoding/xml?

Chris

Gustavo Niemeyer

unread,
Apr 15, 2012, 6:38:21 PM4/15/12
to chris.j...@gmail.com, golan...@googlegroups.com
Hey Chris,

> I'm expecting it to print something with xml:lang="en", but it prints
> lang="en" instead. Is this something I'm doing wrong, or a bug in
> encoding/xml?

It's a known bug: http://golang.org/issue/3526

Sorry about that.

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I'm not absolutely sure of anything.

chris.j...@gmail.com

unread,
Apr 15, 2012, 7:32:48 PM4/15/12
to golan...@googlegroups.com, chris.j...@gmail.com
Thanks for your prompt reply, Gustavo.
Reply all
Reply to author
Forward
0 new messages