具有XML命名空间的属性

huangapple go评论76阅读模式
英文:

Attribute with XML namespace

问题

以下结构用于封装SAML XML元素,例如OrganizationNameOrganizationDisplayNameOrganizationURL

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:&quot;xml lang,attr&quot;`
	Value string `xml:&quot;,chardata&quot;`
}

The Lang attribute is within the xml namespace. The generated XML from the encoding/xml package includes relative namespaces:

&lt;Organization&gt;
  &lt;OrganizationName xmlns:_xml=&quot;xml&quot; _xml:lang=&quot;en&quot;&gt;name&lt;/OrganizationName&gt;
  &lt;OrganizationDisplayName xmlns:_xml=&quot;xml&quot; _xml:lang=&quot;en&quot;&gt;name&lt;/OrganizationDisplayName&gt;
  &lt;OrganizationURL xmlns:_xml=&quot;xml&quot; _xml:lang=&quot;en&quot;&gt;http://www.example.com/&lt;/OrganizationURL&gt;
&lt;/Organization&gt;

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:

&lt;OrganizationName xml:lang=&quot;en&quot;&gt;name&lt;/OrganizationName&gt;

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:

&lt;OrganizationName xml:lang=&quot;en&quot;&gt;name&lt;/OrganizationName&gt;

you can use the following:

package main

import (
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
)

//OrganizationName Name
type OrganizationName struct {
	XMLName xml.Name `xml:&quot;OrganizationName&quot;`
	Lang    string   `xml:&quot;http://www.w3.org/XML/1998/namespace lang,attr,omitempty&quot;`
	Value   string   `xml:&quot;,chardata&quot;`
}

//Organization Organization Entity
type Organization struct {
	XMLName          xml.Name         `xml:&quot;Organization&quot;`
	OrganizationName OrganizationName `xml:&quot;OrganizationName&quot;`
}

func main() {
	v := Organization{
		OrganizationName: OrganizationName{
			Lang:  &quot;en&quot;,
			Value: &quot;Hisham Karam&quot;,
		},
	}
	output, err := xml.MarshalIndent(v, &quot;  &quot;, &quot;    &quot;)
	if err != nil {
		fmt.Printf(&quot;error: %v\n&quot;, err)
	}
	fmt.Printf(&quot;\n%s&quot;, string(output))

}

output:

  &lt;Organization&gt;
      &lt;OrganizationName xml:lang=&quot;en&quot;&gt;Hisham Karam&lt;/OrganizationName&gt;
  &lt;/Organization&gt;

go version:

go version go1.10.3 darwin/amd64

huangapple
  • 本文由 发表于 2017年8月19日 18:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/45770413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定