解组时如何跳过元素?

How can I remove <Text Language="de">...</Text> (as shown below) when unmarshaling XML?

I tried to skip the element with UnmarshalXML method, however, it's skipping the whole element when I do that.

Example in Play Golang Link

package main

import (
    "encoding/xml"
    "fmt"
)

type Root struct {
    Translation []Text `xml:"Texts>Text>Text"`
}

type Text struct {
    Language string `xml:"Language,attr"`
    Value    string `xml:"Value"`
}

func main() {
    foo := `
        <Root>
            <Texts>
                <Text>
                    <Text Language="EN">
                        <Value>One</Value>
                    </Text>
                    <Text Language="de">
                        <Value>Eins</Value>
                    </Text>
                </Text>
            </Texts>
        </Root>
        `

    var root Root
    e := xml.Unmarshal([]byte(foo), &root)
    if e != nil {
        panic(e)
    }

    fmt.Printf("%+v
", root)
}

func (t *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    tx := struct{ Language, Value string }{}

    if start.Attr[0].Value == "EN" {
        d.DecodeElement(&tx, &start)

        // I couldn't got Language attr here
        *t = Text{tx.Language, tx.Value}
        // fmt.Printf("hey: %+v %s
", tx, start.Attr[0].Value)
    } else {
        // It outputs DE element with empty fields
        d.Skip()
    }

    return nil
}

Current Output:

{Translation:[{Language: Value:One} {Language: Value:}]}

What I Want:

{Translation:[{Language:EN Value:One}]}

You're operating too low by unmarshalling at the Text Level - you're still Unmarshalling 2 Texts elements which is why you are seeing an empty second element. You could try something like this:

package main

import (
    "encoding/xml"
    "fmt"
)

type Root struct {
    Translation Text `xml:"Texts>Text>Text"`
}

type Text []struct {
    Language string `xml:"Language,attr"`
    Value    string `xml:"Value"`
}

func main() {
    foo := `
        <Root>
            <Texts>
                <Text>
                    <Text Language="EN">
                        <Value>One</Value>
                    </Text>
                    <Text Language="de">
                        <Value>Eins</Value>
                    </Text>
                </Text>
            </Texts>
        </Root>
        `

    var root Root
    e := xml.Unmarshal([]byte(foo), &root)
    if e != nil {
        panic(e)
    }

    fmt.Printf("%+v
", root)
}

func (t *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    tx := []struct{
        Language string `xml:"Language,attr"`
        Value    string `xml:"Value"`
    }{}
    d.DecodeElement(&tx, &start)


    tSl := *t
    for _, elem := range tx {
        switch elem.Language {
        case "EN":
            tSl = append(tSl, struct{
                Language string `xml:"Language,attr"`
                Value    string `xml:"Value"`}{elem.Language, elem.Value})
        default:
            d.Skip()
        }
    }
    *t = tSl
    return nil
}

Output:

{Translation:[{Language:EN Value:One}]}