[英]Attribute with XML namespace
The following struct is used to marshall SAML XML elements such as OrganizationName
, OrganizationDisplayName
& OrganizationURL
. 以下结构用于编组SAML XML元素,例如
OrganizationName
, OrganizationDisplayName
和OrganizationURL
。
type LocalizedName struct {
Lang string `xml:"xml lang,attr"`
Value string `xml:",chardata"`
}
The Lang
attribute is within the xml
namespace. Lang
属性位于xml
名称空间内。 The generated XML from the encoding/xml package includes relative namespaces: 从encoding / xml包生成的XML包括相对名称空间:
<Organization>
<OrganizationName xmlns:_xml="xml" _xml:lang="en">name</OrganizationName>
<OrganizationDisplayName xmlns:_xml="xml" _xml:lang="en">name</OrganizationDisplayName>
<OrganizationURL xmlns:_xml="xml" _xml:lang="en">http://www.example.com/</OrganizationURL>
</Organization>
I was expecting the elements to not use relative namespaces as they seem to be considered to be insecure, although I do not understand why : 我期待元素不使用相对名称空间,因为它们似乎被认为是不安全的, 尽管我不明白为什么 :
<OrganizationName xml:lang="en">name</OrganizationName>
What causes the relative namespaces to be created? 是什么导致创建相对名称空间?
if you want to generate something like this: 如果要生成这样的内容:
<OrganizationName xml:lang="en">name</OrganizationName>
you can use the following: 您可以使用以下内容:
package main
import (
"encoding/xml"
"fmt"
)
//OrganizationName Name
type OrganizationName struct {
XMLName xml.Name `xml:"OrganizationName"`
Lang string `xml:"http://www.w3.org/XML/1998/namespace lang,attr,omitempty"`
Value string `xml:",chardata"`
}
//Organization Organization Entity
type Organization struct {
XMLName xml.Name `xml:"Organization"`
OrganizationName OrganizationName `xml:"OrganizationName"`
}
func main() {
v := Organization{
OrganizationName: OrganizationName{
Lang: "en",
Value: "Hisham Karam",
},
}
output, err := xml.MarshalIndent(v, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Printf("\n%s", string(output))
}
output: 输出:
<Organization>
<OrganizationName xml:lang="en">Hisham Karam</OrganizationName>
</Organization>
go version: 转到版本:
go version go1.10.3 darwin/amd64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.