1 回答

TA貢獻1873條經(jīng)驗 獲得超9個贊
xmlns:wcm
而xmlns:xsi
不是真正的屬性,它們是經(jīng)過特殊處理的。您的 XML 既沒有元素也沒有來自wcm
和xsi
模式的屬性,它們將被刪除。為什么你的代碼中需要它們?
不過,如果您真的非常需要它們,您可以將以下字段添加到結(jié)構(gòu)中Component
:
Attr []xml.Attr `xml:",any,attr"`
如果 XML 元素具有先前規(guī)則未處理的屬性,并且結(jié)構(gòu)具有包含“,any,attr”的關(guān)聯(lián)標記的字段,則 Unmarshal 會在第一個此類字段中記錄屬性值。
這是一個例子:https://go.dev/play/p/tGDh5Ay1kZW
func main() {
var u Unattend
err := xml.Unmarshal([]byte(document), &u)
if err != nil {
log.Fatal(err)
}
for _, a := range u.Settings[0].Components[0].Attr {
fmt.Printf("Unmatched attributes: %s:%s = %s\n", a.Name.Space, a.Name.Local, a.Value)
}
}
輸出:
Unmatched attributes: xmlns:wcm = http://schemas.microsoft.com/WMIConfig/2002/State
Unmatched attributes: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
注意 1. 這個解決方案不太好的地方:它只處理額外的屬性<component/>
。但是xmlns:XXX
屬性可以出現(xiàn)在任何元素中。在一般情況下,您應(yīng)該Attr
在數(shù)據(jù)模型的所有結(jié)構(gòu)中添加數(shù)組。
注2. 屬性xmlns:wcm
和xmlns:xsi
不承擔(dān)任何業(yè)務(wù)邏輯。他們沒有提供有關(guān)設(shè)置和組件的信息。你真的需要它們嗎?
注 3. 絕對沒有要求命名這些屬性xmlns:wcm
和xmlns:xsi
. 這只是一種常見的做法。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 文檔上失敗,這些文檔在其他元素中或使用其他后綴或值聲明這些屬性。
更新
編組工作不如預(yù)期。xml.Marshal
專門處理xmlns
名稱空間并轉(zhuǎn)換Attr
為名稱空間xmlns:wcm
并轉(zhuǎn)換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 {
Name xml.Name
Value string
}
func (a XMLNSAttr) Label() string {
if a.Name.Space == "" {
return a.Name.Local
}
if a.Name.Local == "" {
return a.Name.Space
}
return a.Name.Space + ":" + a.Name.Local
}
func (a *XMLNSAttr) UnmarshalXMLAttr(attr xml.Attr) error {
a.Name = attr.Name
a.Value = attr.Value
return nil
}
func (a XMLNSAttr) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{
Name: xml.Name{
Space: "",
Local: a.Label(),
},
Value: a.Value,
}, nil
這種類型通過構(gòu)造一個將命名空間嵌入到本地名稱中的屬性來達到目的。場
Attr []xml.Attr `xml:",any,attr"`
正確解碼/編碼xmlns:xxx
屬性
示例: https: //go.dev/play/p/VDofREl5nf1
輸出:
Unmatched attributes: xmlns:wcm = http://schemas.microsoft.com/WMIConfig/2002/State
Unmatched attributes: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
<unattend>
<settings pass="windowsPE">
<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">
...
</component>
...
</settings>
</unattend>
- 1 回答
- 0 關(guān)注
- 210 瀏覽
添加回答
舉報