【问题标题】:XML - Quoting Numerical AttributesXML - 引用数字属性
【发布时间】:2018-09-13 23:00:09
【问题描述】:

XML 中的数字属性应该被引用吗?

<root>
  <node size=45 />
  <foo bar=1.2>
    <baz foo=20>
  </foo>
</root>

对比

<root>
  <node size="45" />
  <foo bar="1.2">
    <baz foo="20">
  </foo>
</root>

我的代码编辑器/浏览器似乎没有引号,但大多数在线资源似乎都说它们是必需的。

【问题讨论】:

  • 只是做一个快速的谷歌搜索,我想我会引用他们。
  • 您的代码编辑器可能处于 HTML 模式而不是 XML 模式。 XML 总是需要引号。

标签: xml syntax standards quotes


【解决方案1】:

http://www.w3schools.com/xml/xml_attributes.asp

必须引用 XML 属性

必须始终引用属性值。可以使用单引号或双引号。对于一个人的性别,person 元素可以这样写:

<person sex="female">

或者像这样:

<person sex='female'>

如果属性值本身包含双引号,您可以使用单引号,如下例所示:

<gangster name='George "Shotgun" Ziegler'>

或者你可以使用字符实体:

<gangster name="George &quot;Shotgun&quot; Ziegler">

根据最新评论进行了更新,至少在 Microsoft 世界中是这样。

XElement.Value Property

public string Value { get; set; }

你也可以参考这个question

【讨论】:

  • 经过更多挖掘,我找到了this section of the spec,指定必须引用每个属性,如this answer。
  • 如果被引用,属性是否总是被视为字符串?
  • @s.paszko 我相应地更新了答案,希望对您有所帮助。
猜你喜欢
  • 2020-03-28
  • 1970-01-01
  • 2014-04-26
  • 2012-10-01
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多