英文:
Attribute with XML namespace
问题
以下结构用于封装SAML XML元素,例如OrganizationName
、OrganizationDisplayName
和OrganizationURL
。
type LocalizedName struct {
Lang string `xml:"xml lang,attr"`
Value string `xml:",chardata"`
}
Lang
属性位于xml
命名空间中。从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>
我原本期望元素不使用相对命名空间,因为它们似乎被认为是不安全的,尽管我不明白为什么:
<OrganizationName xml:lang="en">name</OrganizationName>
是什么导致创建相对命名空间?
英文:
The following struct is used to marshall SAML XML elements such as OrganizationName
, OrganizationDisplayName
& OrganizationURL
.
type LocalizedName struct {
Lang string `xml:"xml lang,attr"`
Value string `xml:",chardata"`
}
The Lang
attribute is within the xml
namespace. The generated XML from the encoding/xml package includes relative namespaces:
<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?
答案1
得分: 2
如果你想生成类似这样的内容:
<OrganizationName xml:lang="en">name</OrganizationName>
你可以使用以下代码:
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))
}
输出结果:
<Organization>
<OrganizationName xml:lang="en">Hisham Karam</OrganizationName>
</Organization>
go版本:
go version go1.10.3 darwin/amd64
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论