【发布时间】:2019-12-05 09:37:32
【问题描述】:
我正在尝试像这样解组一段 xml:
<message numerus="yes">
<source>%n part(s)</source>
<translation>
<numerusform>%n part</numerusform>
<numerusform>%n parts</numerusform>
</translation>
</message>
<message>
<source>Foo</source>
<translation>Bar</translation>
</message>
请注意<translation> 标签可以包含一个简单的字符串或多个<numerusform> 标签。
使用 go 的 xml 包,我解组的结构是这样的:
type Message struct {
Source string `xml:"source"`
Numerus string `xml:"numerus,attr"`
Translation string `xml:"translation"`
NumerusForms []string `xml:"translation>numerusform"`
}
问题:可以使用字段Translation 或NumerusForms。如果像这里显示的那样使用两者,则会发生错误:
Error on unmarshalling xml: main.Message field "Translation" with tag "translation" conflicts with field "NumerusForms" with tag "translation>numerusform"
非常合理,因为解组器无法决定如何处理<translation> 标签。
有什么办法可以解决这个问题吗?可以有两个不同的命名字段(一个用于纯字符串,一个用于字符串列表,如上所示的结构)。
完整的可运行代码请参考this go playground。
旁注:我正在尝试解析Qt Linguist TS file。该示例被大量剥离,以便于推理。
【问题讨论】:
标签: xml go unmarshalling