XML,这是什么:null 或空元素? [英] XML, what is this: null or empty element?

查看:20
本文介绍了XML,这是什么:null 或空元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我的另一个问题:XML 反序列化空元素?

我从第三方服务器获得了类似的元素用于 API 测试:

I've got elements like these from a third-party server for API testing:

<Taxable />
<DefaultPurchasePrice />

我刚刚意识到,现在我对这样的元素是代表空对象还是空对象感到困惑.

I just realized that now I am confusing myself about whether elements like that represent null object or empty.

说到对象,它们是一样的,空对象通常意味着空对象引用对吧?但是尝试将 XML 元素映射到数据字段/值,它们可能不同,即空字符串是空字符串,但对于十进制价格或布尔值,它们是未定义的,等于空,但不会为空,除非它们被定义作为可以为空的.

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.

同样,我的 XmlSerializer 的问题是它无法处理像这样的空元素的传输.我可以在我的代码中轻松解决这个问题吗?或者我应该问人们提供的 XML 是否有明确定义的 XML?因为似乎像这样的空 XML 元素是未定义的:它就在这里,但是对于 XML 元素本身来说,它可以是 null 还是空的都无关紧要吗?但是对于那个元素,我的代码需要弄清楚如何处理它,除非我将所有 C# 类数据字段设置为字符串类型.否则,如果我的代码试图直接将一个空或 null 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.

我不得不问这个问题,因为我遇到过 XML 有很多这些元素对于那些特殊元素,我的 .NET XMLserialization 代码需要将这些字段映射为字符串,如果字符串不为空,我会将它们设置为对应的数据类型,否则我将它们设置为 null.我最终在反序列化之前删除了那些空元素,因为它更容易.但我确实在徘徊:我在我的代码中真正做了什么?我只是删除了 null 元素还是空元素?因为它们明显不同!但是编写 XML 的人认为它们是相同的,因为 XML本身并没有'null'的概念,有人认为我有责任决定它是空还是空.但是XML确实使您能够以更清晰的方式表示null"元素

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

在我展示的示例中,这两个元素显然应该是 null 而不是空元素.XML 并没有真正的 null 概念,但这些元素可以被省略(如果它们为 null,则不要将它们放入 XML)或使用@svick 提到的更好的表示形式.或者在其他情况下,应该在有意义时使用空元素.但不适用于十进制或布尔值.

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.

推荐答案

<应税/>
<DefaultPurchasePrice/>

<Taxable />
<DefaultPurchasePrice />

这是什么:null 或空元素?

what is this: null or empty element?

它是空的.它在语义上与:

It is empty. It is semantically the same as:

<Taxable></Taxable>
<DefaultPurchasePrice></DefaultPurchasePrice>

XML 本身中唯一存在的空"概念是缺少的元素.你不能指定一个元素 并且 说它是空的,使用 XML 的语义.您必须有某种模式(显式模式,例如 DTD、XSD 或 Schematron,或隐式/逻辑模式)才能执行此操作.

The only "null" concept that exists in XML itself is a missing element. You can't specify an element and say 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 相关技术的应用程序.例如,XSD 有一个 xsi:nil="true" 属性.即使您可以将其中一些模式属性应用到 XML,这些属性也不是纯"XML;它们是 XSD 模式提供的附加功能.

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.

在这种情况下,XSD 可以在基本 XML 结构之上自由构建自己的语义,并在不存在的地方添加显式 nil.这种灵活性被认为是 XML 的优势.

This is a case where XSD was free to build its own semantics on top of the basic XML structure, and add an explicit nil where one didn't exist. This flexibility is considered an advantage of XML.

这篇关于XML,这是什么:null 或空元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆