如何使用Golang在xml中输入重复属性?

时间:2019-05-22 06:38:28

标签: xml go

我想导出一个看起来像下面结果的xml文件,但是属性值是重复的,并且告诉我重复的属性值,在下面如何实现此结果?

属性部分不止一次,golang抱怨它重复了。 .................................................... ..........................................

预期的XML结构:

<cdon_shopping_mall_import import_id='1' source_id='1' version='2.0' import_previous_id='1' import_type='FULL' xmlns='http://schemas.cdon.com/product/2.0/shopping-mall.xsd' import_date='2019-03-22T08:39:38'>
    <documents>
        <brands>
            <brand id='1'>
                <values>
                    <attribute id='BrandName'>
                        <value>
                            CarpetVista
                        </value>
                    </attribute>
                </values>
            </brand>
        </brands>
        <products>
            <product id='CVID229005'>
                <class id='PatchworkCarpets'/>
                <brand id='1'/>
                <values>
                    <attribute id='Title'>
                        <value>Davina 160x230 cm, maskinknuten</value>
                    </attribute>
                    <attribute id='Description'>
                        <value>
                            <![CDATA[
                                <p>Beskrivning om min fina matta</p>
                            ]]>
                        </value>
                    </attribute>
                    <attribute id='FrontImage'>
                        <value>http://test.se/bild.jpg</value>
                    </attribute>
                </values>
                <productStatus>
                    <status>ONLINE</status>
                    <exposeStatus>BUYABLE</exposeStatus>
                    <inStock>111</inStock>
                </productStatus>
                <salesChannels>
                    <channel iso='se'>
                        <price current='111111' ordinary='11111' vat='25' currency='SEK'/>
                        <sellable>true</sellable>
                        <deliveryTime min='1' max='3'/>
                        <freightClass>A</freightClass>
                    </channel>
                </salesChannels>
            </product>
        </products>
    </documents>
</cdon_shopping_mall_import>

我的代码是:

type price struct {
    current  string `xml:"current,attr"`
    ordinary string `xml:"ordinary,attr"`
    vat      string `xml:"current,attr"`
    currency string `xml:"current,attr"`
}

type deliveryTime struct {
    min string `xml:"min,attr"`
    max string `xml:"min,attr"`
}

type channel struct {
    iso string `xml:"iso,attr"`
    price
    sellable string `xml:"sellable"`
    deliveryTime
    freightClass string `xml:"freightClass"`
}

type attribute struct {
    id    string `xml:"id,attr"`
    value string `xml:"value"`
}

type values struct {
    attribute
}

type brands struct {
    brand
}

type brand struct {
    id string `xml:"id,attr"`
    values
}

type productStatus struct {
    status       string `xml:"status"`
    exposeStatus string `xml:"exposeStatus"`
    inStock      int    `xml:"inStock"`
}

type salesChannels struct {
    channel
}

type class struct {
    id string `xml:"id,attr"`
}

type products struct {
    id string `xml:"id,attr"`
    class
    brand string `xml:"brand"`
    values
    productStatus
    salesChannels
}

type documents struct {
    brands
    products
}

type cdon_shopping_mall_import struct {
    documents
}


v := &cdon_shopping_mall_import{
            documents{
                brands: brands{
                    brand: brand{
                        id: "1",
                        values: values{
                            attribute{
                                id:    "BrandName",
                                value: p.Brand,
                            },
                        },
                    },
                },
                products: products{
                    id: p.SKU(),
                    class: class{
                        id: p.CategoryN(lang, 0),
                    },
                    brand: "1",
                    values: values{
                        attribute: attribute{
                            id:    "Title",
                            value: p.Title[lang],
                        },
                    },
                    productStatus: productStatus{
                        status:       "ONLINE",
                        exposeStatus: "BUYABLE",
                        inStock:      p.Available,
                    },
                    salesChannels: salesChannels{
                        channel: channel{
                            iso: "se",
                            price: price{
                                current:  p.SalePriceStr("SEK"),
                                ordinary: p.RegularPriceStr("SEK"),
                                vat:      "25",
                                currency: "SEK",
                            },
                            sellable: "true",
                            deliveryTime: deliveryTime{
                                min: "1",
                                max: "3",
                            },
                            freightClass: "A",
                        },
                    },
                },
            },
        }

0 个答案:

没有答案