正确编组和解组包含 xmlns:wcm 和 xmlns:xsi 的 XML


我正在尝试处理 Windows Autounattend.xml 文件的读写,以便创建和修改它们。我无法获取xmlns:wcmxmlns:xsi正确编组和解组属性。

我正在尝试处理 Windows Autounattend.xml 文件的读写,以便创建和修改它们。我无法获取xmlns:wcm和xmlns:xsi正确编组和解组属性。


炎炎设计
浏览 153回答 1
1回答

眼眸繁星

xmlns:wcm而xmlns:xsi不是真正的属性,它们是经过特殊处理的。您的 XML 既没有元素也没有来自wcm和xsi模式的属性,它们将被删除。为什么你的代码中需要它们?不过,如果您真的非常需要它们,您可以将以下字段添加到结构中Component:&nbsp;&nbsp;&nbsp;&nbsp;Attr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[]xml.Attr&nbsp;`xml:",any,attr"`xml.Unmarshal文件说:如果 XML 元素具有先前规则未处理的属性,并且结构具有包含“,any,attr”的关联标记的字段,则 Unmarshal 会在第一个此类字段中记录属性值。这是一个例子:https://go.dev/play/p/tGDh5Ay1kZWfunc main() {&nbsp; &nbsp; var u Unattend&nbsp; &nbsp; err := xml.Unmarshal([]byte(document), &u)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; for _, a := range u.Settings[0].Components[0].Attr {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Unmatched attributes: %s:%s = %s\n", a.Name.Space, a.Name.Local, a.Value)&nbsp; &nbsp; }}输出:Unmatched attributes: xmlns:wcm = http://schemas.microsoft.com/WMIConfig/2002/StateUnmatched attributes: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance注意 1. 这个解决方案不太好的地方:它只处理额外的属性<component/>。但是xmlns:XXX属性可以出现在任何元素中。在一般情况下,您应该Attr在数据模型的所有结构中添加数组。注2. 属性xmlns:wcm和xmlns:xsi不承担任何业务逻辑。他们没有提供有关设置和组件的信息。你真的需要它们吗?注 3. 绝对没有要求命名这些属性xmlns:wcm和xmlns:xsi.&nbsp;这只是一种常见的做法。XML 文档可以命名它们xmlns:foo并且xmlns:bar- 它是完全有效的。连价值观"http://schemas.microsoft.com/WMIConfig/2002/State"都没"http://www.w3.org/2001/XMLSchema-instance"有意义。它们可以是任何字符串,唯一的要求是匹配URI 格式。例如urn:microsoft:wmi-config:2002:state与http://schemas.microsoft.com/WMIConfig/2002/State解析这些属性Component非常具体,可能会在其他有效的 XML 文档上失败,这些文档在其他元素中或使用其他后缀或值声明这些属性。更新编组工作不如预期。xml.Marshal专门处理xmlns名称空间并转换Attr为名称空间xmlns:wcm并转换xmlns:xsi为名称空间_xmlns<component name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:_xmlns="xmlns" _xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" _xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">...</component>解决方法是为属性使用特殊类型:type XMLNSAttr struct {&nbsp; &nbsp; Name&nbsp; xml.Name&nbsp; &nbsp; Value string}func (a XMLNSAttr) Label() string {&nbsp; &nbsp; if a.Name.Space == "" {&nbsp; &nbsp; &nbsp; &nbsp; return a.Name.Local&nbsp; &nbsp; }&nbsp; &nbsp; if a.Name.Local == "" {&nbsp; &nbsp; &nbsp; &nbsp; return a.Name.Space&nbsp; &nbsp; }&nbsp; &nbsp; return a.Name.Space + ":" + a.Name.Local}func (a *XMLNSAttr) UnmarshalXMLAttr(attr xml.Attr) error {&nbsp; &nbsp; a.Name = attr.Name&nbsp; &nbsp; a.Value = attr.Value&nbsp; &nbsp; return nil}func (a XMLNSAttr) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {&nbsp; &nbsp; return xml.Attr{&nbsp; &nbsp; &nbsp; &nbsp; Name: xml.Name{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Space: "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Local: a.Label(),&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; Value: a.Value,&nbsp; &nbsp; }, nil这种类型通过构造一个将命名空间嵌入到本地名称中的属性来达到目的。场&nbsp;&nbsp;&nbsp;&nbsp;Attr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[]xml.Attr&nbsp;`xml:",any,attr"`正确解码/编码xmlns:xxx属性示例: https:&nbsp;//go.dev/play/p/VDofREl5nf1输出:Unmatched attributes: xmlns:wcm = http://schemas.microsoft.com/WMIConfig/2002/StateUnmatched attributes: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance<unattend>&nbsp; <settings pass="windowsPE">&nbsp; &nbsp; <component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; </component>&nbsp; &nbsp; ...&nbsp; </settings></unattend>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go