When you leave the namespace out, it will match that tag in *any* namespace (or none).
On Mon, Jul 24, 2017 at 11:56 PM Konstantin Khomoutov <kos...@bswap.ru> wrote: > On Mon, Jul 24, 2017 at 07:44:33PM -0700, emartinez1...@gmail.com wrote: > > [...] > >>> So I'm trying to unmarshal an XML with namespaces in go but i just > can't > >>> find the right parser to do so. > [...] > >> type Root struct { > >> XMLName struct{} `xml:"urn:MNResultsResults MNResultsResults"` > >> Cpcs []float64 `xml:"urn:MNResultsResults > >> ssResultSet>ssResult>cpc"` > [...] > >> const s = `<?xml version="1.0" encoding="utf-8"?> > >> <ls:MNResultsResults xmlns:ls="urn:MNResultsResults"> > >> <ls:ssResultSet ls:firstResult="1" ls:numResults="2" ls:totalHits="2"> > >> <ls:ssResult ls:id="1"> > >> <ls:cpc>42.0</ls:cpc> > [...] > > Is there any reason to declare the namespace 2 times? > > At first we declare XMLName (which doesn't seem to be used) > > No, it is used: please read carefully the documentation on the > encoding/xml.Unmarshal (run `go doc xml.Unmarshal` or see [1]). > To cite the unmarshaling rules from there: > > | * If the XMLName field has an associated tag of the form > | "name" or "namespace-URL name", the XML element must have the > | given name (and, optionally, name space) or else Unmarshal > | returns an error. > > So the "XMLName" of a field is special and is served (among other > purposes, if needed) to tell the decoder what's the name -- possibly > namespaced -- of the XML element which is to be represented by the > data type containing that field. > > > and then we use urn:MNResultsResults again at CPC [] > > You're correct on this point: the second namespace is not needed. > Again, to cite the doc: > > | * If the XML element contains a sub-element whose name matches > | the prefix of a tag formatted as "a" or "a>b>c", unmarshal > | will descend into the XML structure looking for elements with the > | given names, and will map the innermost elements to that struct > | field. A tag starting with ">" is equivalent to one starting > | with the field name followed by ">". > > From this definition, it's not clear, whether the full (namespaced) > names of the elements are meant or local, or both are recognized -- > that is, "XML-namespace name>Another-XML-namespace another-name" is > understood, too. I have assumed the latter, but I'm now not sure. > > Seems to work both ways on playground, so I'd drop the NS from the tag of > the Cpc field. > > 1. https://golang.org/pkg/encoding/xml/#Unmarshal > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.