XML,这是什么:null 或空元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7238254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML, what is this: null or empty element?
提问by Paul L
Regarding to my other question: XML deserialize null elements?
关于我的另一个问题:XML 反序列化空元素?
I've got elements like these from a third-party server for API testing:
我有来自第三方服务器的这些元素用于 API 测试:
<Taxable />
<DefaultPurchasePrice />
I just realized that now I am confusing myself about whether elements like that represent null object or empty.
我刚刚意识到,现在我对这样的元素是代表空对象还是空对象感到困惑。
Talking about objects, they are same, null object normally means empty object reference right? But trying to map a XML element to a datafield/value, they could be different i.e null string is empty string, but for Decimal price or Boolean value, they are undefined which equal to empty, but will not be null, unless they are defined as nullable.
谈到对象,它们是一样的,空对象通常意味着空对象引用对吗?但是尝试将 XML 元素映射到数据字段/值,它们可能不同,即空字符串是空字符串,但对于十进制价格或布尔值,它们是未定义的,等于空,但不会为空,除非它们被定义作为可空。
Again, the problem with my XmlSerializer is that wouldn't handle transfer an empty element like that. could I easily fix that in my code. Or should I ask people provide the XML have a well defined XML? Because seems that an empty XML element like that is undefined: it is here, but could be either null or empty doesn't matter for XML element itself? But for that element my code need figure out how to deal with that, unless I set all my C# class datafield as string type. Otherwise, if my code trying to directly map an empty or null XML element to a certain datafield, it will fail for sure.
同样,我的 XmlSerializer 的问题是无法处理像那样传输空元素。我可以在我的代码中轻松解决这个问题吗?或者我应该问人们提供的 XML 是否有一个定义良好的 XML?因为似乎像这样的空 XML 元素是未定义的:它在这里,但对于 XML 元素本身来说可能是 null 还是空都无关紧要?但是对于那个元素,我的代码需要弄清楚如何处理它,除非我将所有 C# 类数据字段设置为字符串类型。否则,如果我的代码尝试将空或空 XML 元素直接映射到某个数据字段,它肯定会失败。
I have to ask this question because I encountered XML have lots of those elements and for those special elements, my .NET XMLserialization code need to mapped those field as string and if the string is not empty, I case them into correspondent datatype, otherwise I set them to null. And I end up remove those empty element before I do my deserialization because it is much easier. But I do wandering: "What am I really doing in my code? Did I just remove null elements or empty elements? Because they are clearly different!But people writing that XML think they are same, because XML itself don't have concept of 'null', and some people argue that it is my responsibility to decide whether it is null or empty. But XML do enable you to represent 'null' elements in a clearer way
我不得不问这个问题,因为我遇到 XML 有很多这些元素,对于那些特殊元素,我的 .NET XMLserialization 代码需要将这些字段映射为字符串,如果字符串不为空,我会将它们转换为相应的数据类型,否则我将它们设置为空。在我进行反序列化之前,我最终删除了那些空元素,因为它更容易。但我确实在徘徊:“我在我的代码中到底在做什么?我是否只是删除了空元素或空元素?因为它们显然不同!但是编写 XML 的人认为它们是相同的,因为 XML 本身没有'null',有些人认为决定它是 null 还是空是我的责任。但 XML 确实使您能够以更清晰的方式表示 'null' 元素
Edit:
编辑:
In the example I presented, those two elements clearly should be null rather than empty elements. XML don't really have a concept of null, but those elements could either be omitted (if they are null, don't put them into XML) or using better representation as mentioned be @svick. Or in other cases, empty elements should be used when they make sense. But not for Decimal or Boolean.
在我展示的例子中,这两个元素显然应该是 null 而不是空元素。XML 并没有真正的 null 概念,但是这些元素可以被省略(如果它们为 null,不要将它们放入 XML)或者使用更好的表示,如提到的@svick。或者在其他情况下,应该在有意义的时候使用空元素。但不适用于十进制或布尔值。
回答by Merlyn Morgan-Graham
<Taxable />
<DefaultPurchasePrice />what is this: null or empty element?
<Taxable />
<DefaultPurchasePrice />这是什么:null 或空元素?
It is empty. It is semantically the same as:
它是空的。它在语义上与以下内容相同:
<Taxable></Taxable>
<DefaultPurchasePrice></DefaultPurchasePrice>
The only "null" concept that exists in XML itself is a missing element. You can't specify an element andsay that it is null, using XML's semantics. You must have some sort of schema (an explicit schema, such as DTD, XSD, or Schematron, or an implicit/logical schema) to do this.
XML 本身存在的唯一“空”概念是缺少元素。您不能使用 XML 的语义指定一个元素并说它为空。您必须有某种模式(显式模式,例如 DTD、XSD 或 Schematron,或隐式/逻辑模式)才能执行此操作。
This does not restrict an application that interprets the XML, though, or official XML-related technologies. For example, XSD has an xsi:nil="true"attribute. Even though you can apply some of these schema attributes to the XML, those attributes aren't "pure" XML; They are an addition supplied by XSD schemas.
但是,这并不限制解释 XML 的应用程序或与 XML 相关的官方技术。例如,XSD 有一个xsi:nil="true"属性。尽管您可以将其中一些模式属性应用于 XML,但这些属性并不是“纯”XML;它们是由 XSD 模式提供的附加项。
This is a case where XSD was free to build its own semantics on top of the basic XML structure, and add an explicit nilwhere one didn't exist. This flexibility is considered an advantage of XML.
在这种情况下,XSD 可以自由地在基本 XML 结构之上构建自己的语义,并nil在不存在的地方添加显式。这种灵活性被认为是 XML 的一个优势。
回答by svick
I think this comes down to the fact that there is no default mapping between XML and the object model of your favorite language. Consider the following XML:
我认为这归结为 XML 和您喜欢的语言的对象模型之间没有默认映射的事实。考虑以下 XML:
<Person Name="John">
<Father>
<Person Name="Jim" />
</Father>
<Mother>
<Person Name="Jenny" />
</Mother>
</Person>
Does Personhave properties (or fields) Fatherand Mother? Or does Personrepresent a collection that can contains objects of types Fatherand Mother(perhaps inheriting from abstract base type FamilyMember)? This is not clear from the XML, and it's up to you how you represent this in your object model. (The presence of schema would change that somewhat, but it still leaves room for interpretation.)
是否Person有属性(或字段)Father和Mother? 或者Person代表一个可以包含类型Father和Mother(可能继承自抽象基类型FamilyMember)对象的集合?这在 XML 中并不清楚,这取决于您如何在对象模型中表示它。(模式的存在会稍微改变这种情况,但它仍然留有解释的空间。)
Similarly, the concept of nullis not present in XML. On the other hand, the concept of empty element does not map directly to object model. And they are not the same, empty element still can have attributes and it has a “type” (element name).
类似地,nullXML 中不存在的概念。另一方面,空元素的概念不直接映射到对象模型。它们不一样,空元素仍然可以有属性,并且它有一个“类型”(元素名称)。
Generally, I think good solution is to have a special XML element representing null(<Null />), but another solution might make more sense in your specific case.
通常,我认为好的解决方案是使用一个特殊的 XML 元素来表示null( <Null />),但在您的特定情况下,另一种解决方案可能更有意义。

