设想:
我有一个正在尝试解析的 XML 结构,我不知道如何设置一个结构,其中 xml 属性的值包含文本和更多嵌套值。所有其他属性都已正确设置,我不确定是否需要获取源的值并创建一个单独的解析器来检索元素的值。
<trans-unit id="some.message">
<source>hello %<ph id="first_name">{0}</ph> %<ph id="last_name">{1}</ph>
</source>
<target/>
</trans-unit>
type TransUnit struct {
Id string `xml:"id,attr"`
Source string `xml:"source"`
SourceVars MixedVars `xml:"source>ph"`
Target string `xml:"target"`
}
type MixedVars []MixedVar
type MixedVar struct {
VarName string `xml:"id,attr"`
}
EDIT:我正在尝试将源解析为遵循以下形式的字符串:
你好 %{first_name} %{last_name}
使用当前结构解组 xml 字符串会返回一个空结构
@plato 使用innerxml 将源设置为:
<source>Are you sure you want to reset the reservation for %<ph id="first_name">{0}</ph> %<ph id="last_name">{1}</ph>
这使我处于类似的情况,我仍然在源值中插入嵌套的 xml 标签
可以将源 xml 节点同时解组为原始 xml 和变量切片,例如:
type TransUnit struct {
ID string `xml:"id,attr"`
Source Source `xml:"source"`
Target string `xml:"target"`
}
type Source struct {
Raw string `xml:",innerxml"`
Text string `xml:",chardata"`
Vars []Var `xml:"ph"`
}
type Var struct {
ID string `xml:"id,attr"`
Value string `xml:",innerxml"`
}
See a 运行示例 https://play.golang.org/p/7I1bxgxCCO。你应该很高兴从那里开始。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)