表示 null XML 元素的正确方法是什么?
我见过 null
元素以多种方式表示:
该元素以 xsi:nil="true"
形式出现:
<book>
<title>Beowulf</title>
<author xsi:nil="true"/>
</book>
该元素是存在,但表示为空元素(我认为这是错误的,因为“空”和 null
在语义上不同):
<book>
<title>Beowulf</title>
<author/>
</book>
<!-- or: -->
<book>
<title>Beowulf</title>
<author></author>
</book>
该元素在返回的标记中根本不存在:
<book>
<title>Beowulf</title>
</book>
该元素有一个
子元素(来自TStamper 下面):
<book>
<title>Beowulf</title>
<author><null/></author>
</book>
是否有正确或规范的方式来表示这样的 null< /代码>值? 除了上述示例之外,还有其他方法吗?
上面示例的 XML 是人为设计的,因此不要对其进行过多解读。 :)
I have seen null
elements represented in several ways:
The element is present with xsi:nil="true"
:
<book>
<title>Beowulf</title>
<author xsi:nil="true"/>
</book>
The element is present, but represented as an empty element (which I believe is wrong since 'empty' and null
are semantically different):
<book>
<title>Beowulf</title>
<author/>
</book>
<!-- or: -->
<book>
<title>Beowulf</title>
<author></author>
</book>
The element is not present at all in the returned markup:
<book>
<title>Beowulf</title>
</book>
The element has a <null/>
child element (from TStamper below):
<book>
<title>Beowulf</title>
<author><null/></author>
</book>
Is there a correct, or canonical way to represent such a null
value? Are there additional ways than the above examples?
The XML for the examples above is contrived, so don't read too far into it. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
xsi:nil 是表示以下值的正确方法:
当发出 DOM Level 2 调用 getElementValue() 时,将返回 NULL 值。
xsi:nil 还用于指示没有内容的有效元素,即使该元素内容类型通常不允许空元素。
如果使用空标签,getElementValue() 返回空字符串 ("")
如果省略标签,则甚至不存在作者标签。 语义上可能与将其设置为“nil”不同(例如,将“Series”设置为 nil 可能表示该书不属于任何系列,而省略系列可能意味着该系列是对当前元素不适用的元素。)
这在 W3C
澄清:
如果您有一个 book xml 元素,并且其中一个子元素是 book:series,则在填写时您有多种选择:
xsi:nil is the correct way to represent a value such that:
When the DOM Level 2 call getElementValue() is issued, the NULL value is returned.
xsi:nil is also used to indicate a valid element with no content even if that elements content type normally doesn't allow empty elements.
If an empty tag is used, getElementValue() returns the empty string ("")
If the tag is omitted, then no author tag is even present. This may be semantically different than setting it to 'nil' (Ex. Setting "Series" to nil may be that the book belongs to no series, while omitting series could mean that series is an inapplicable element to the current element.)
From: The W3C
A clarification:
If you have a book xml element and one of the child elements is book:series you have several options when filling it out:
没有规范的答案,因为 XML 根本上没有 null 概念。
但我假设您需要 Xml/对象映射(因为对象图有空值); 所以你的答案是“无论你的工具使用什么”。 如果你写处理,那就意味着你喜欢什么。 对于使用 XML 架构的工具,
xsi:nil
是最佳选择。 对于大多数映射器来说,省略匹配元素/属性是实现这一点的方法。There is no canonical answer, since XML fundamentally has no null concept.
But I assume you want Xml/Object mapping (since object graphs have nulls); so the answer for you is "whatever your tool uses". If you write handling, that means whatever you prefer. For tools that use XML Schema,
xsi:nil
is the way to go. For most mappers, omitting matching element/attribute is the way to do it.这取决于您如何验证 XML。 如果您使用 XML 架构验证,则表示
null
值的正确方法是使用xsi:nil
属性。[来源]
It depends on how you validate your XML. If you use XML Schema validation, the correct way of representing
null
values is with thexsi:nil
attribute.[Source]
w3 链接中的文档:
http://www.w3.org/ TR/REC-xml/#sec-starttags
表示这些是推荐的形式:
另一个答案中提到的属性是验证机制,而不是状态的表示。 请参考:http://www.w3.org/TR/xmlschema-1 /#xsi_nil
为了澄清这个答案:
The documentation in the w3 link:
http://www.w3.org/TR/REC-xml/#sec-starttags
says that these are the recommended forms:
The attribute mentioned in the other answer is a validation mechanism and not a representation of state. Please refer to: http://www.w3.org/TR/xmlschema-1/#xsi_nil
To clarify this answer:
当您的架构语义指示某个元素具有默认值并且如果该元素不存在时应使用默认值时,您可以使用 xsi:nil。 我必须假设,对于一些聪明人来说,前面这句话并不是一个不言而喻的可怕想法,但对我来说,这听起来像是九种不好的想法。 我使用过的每种 XML 格式都通过省略元素来表示空值。 (或者属性,祝你好运用
xsi:nil
标记属性。)You use
xsi:nil
when your schema semantics indicate that an element has a default value, and that the default value should be used if the element isn't present. I have to assume that there are smart people to whom the preceding sentence is not a self-evidently terrible idea, but it sounds like nine kinds of bad to me. Every XML format I've ever worked with represents null values by omitting the element. (Or attribute, and good luck marking an attribute withxsi:nil
.)在不太正式的数据中,简单地省略属性或元素效果很好。
如果您需要更复杂的信息,GML 模式添加属性 nilReason,例如:在 GeoSciML:
xsi:nil
值为“true”,用于指示没有可用的值nilReason
可用于记录附加信息缺失值; 这可能是标准 GML 原因之一(缺失、不适用、保留、未知
),或以other:
开头的文本,也可能是指向更详细信息的 URI 链接解释。当您交换数据时,XML 常用的角色、发送给一个接收者或出于给定目的的数据可能会隐藏一些内容,而这些内容对于付费或具有不同身份验证的其他人来说是可用的。 了解内容丢失的原因非常重要。
科学家们还担心信息缺失的原因。 例如,如果由于质量原因而被丢弃,他们可能想查看原始的不良数据。
Simply omitting the attribute or element works well in less formal data.
If you need more sophisticated information, the GML schemas add the attribute nilReason, eg: in GeoSciML:
xsi:nil
with a value of "true" is used to indicate that no value is availablenilReason
may be used to record additional information for missing values; this may be one of the standard GML reasons (missing, inapplicable, withheld, unknown
), or text prepended byother:
, or may be a URI link to a more detailed explanation.When you are exchanging data, the role for which XML is commonly used, data sent to one recipient or for a given purpose may have content obscured that would be available to someone else who paid or had different authentication. Knowing the reason why content was missing can be very important.
Scientists also are concerned with why information is missing. For example, if it was dropped for quality reasons, they may want to see the original bad data.
在许多情况下,Null 值的目的是提供应用程序先前版本中不存在的数据值。
假设您的应用程序“ReportMaster”版本 1 有一个 xml 文件。
现在在 ReportMaster 版本 2 中添加了一些可能已定义或未定义的属性。
如果您使用“无标记意味着空”表示,您将获得自动向后兼容性来读取 ReportMaster 1 xml 文件。
In many cases the purpose of a Null value is to serve for a data value that was not present in a previous version of your application.
So say you have an xml file from your application "ReportMaster" version 1.
Now in ReportMaster version 2 a some more attributes have been added that may or not be defined.
If you use the 'no tag means null' representation you get automatic backward compatibility for reading your ReportMaster 1 xml file.