表示 null XML 元素的正确方法是什么?

发布于 2024-07-17 01:01:02 字数 1171 浏览 10 评论 0原文

我见过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦开始←不甜 2024-07-24 01:01:02

xsi:nil 是表示以下值的正确方法:
当发出 DOM Level 2 调用 getElementValue() 时,将返回 NULL 值。
xsi:nil 还用于指示没有内容的有效元素,即使该元素内容类型通常不允许空元素。

如果使用空标签,getElementValue() 返回空字符串 ("")
如果省略标签,则甚至不存在作者标签。 语义上可能与将其设置为“nil”不同(例如,将“Series”设置为 nil 可能表示该书不属于任何系列,而省略系列可能意味着该系列是对当前元素不适用的元素。)

这在 W3C

XML 架构:结构引入了
发出信号的机制
元素应被视为·有效·
当它没有内容时
不需要或的内容类型
甚至必须允许空内容。
一个元素可能是“有效的”,无需
内容(如果具有该属性)
xsi:nil 值为 true。 一个
如此标记的元素必须为空,但是
如果允许的话可以携带属性
相应的复杂类型。

澄清:
如果您有一个 book xml 元素,并且其中一个子元素是 book:series,则在填写时您有多种选择:

  1. 完全删除该元素 - 当您希望表明该系列不适用于这本书或该书时,可以执行此操作本书不是系列的一部分。 在这种情况下,具有与 book:series 匹配的模板的 xsl 转换(或其他基于事件的处理器)将永远不会被调用。 例如,如果您的 xsl 将 book 元素转换为表格行 (xhtml:tr),则使用此方法可能会得到不正确的表格单元格数量 (xhtml:td)。
  2. 将元素留空 - 这可能表明该系列是“”,或者未知,或者该书不是系列的一部分。 任何与 book:series 匹配的 xsl 转换(或其他基于 Evernt 的解析器)都将被调用。 current() 的值将为“”。 使用此方法您将获得与下一个描述的方法相同数量的 xhtml:td 标记。
  3. 使用 xsi:nil="true" - 这表示 book:series 元素为 NULL,而不仅仅是空。 将调用具有模板匹配 book:series 的 xsl 转换(或其他基于事件的解析器)。 current() 的值将为空(不是空字符串)。 此方法与 (2) 之间的主要区别在于 book:series 的架构类型不需要允许空字符串 ("") 作为有效值。 这对于系列元素没有任何实际意义,但对于在架构中定义为枚举类型的语言元素,xsi:nil="true" 允许元素没有数据。 另一个例子是十进制类型的元素。 如果您希望它们为空,您可以联合一个仅允许“”和小数的枚举字符串,或者使用可以为空的小数。

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

XML Schema: Structures introduces a
mechanism for signaling that an
element should be accepted as ·valid·
when it has no content despite a
content type which does not require or
even necessarily allow empty content.
An element may be ·valid· without
content if it has the attribute
xsi:nil with the value true. An
element so labeled must be empty, but
can carry attributes if permitted by
the corresponding complex type.

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:

  1. Removing the element entirely - This can be done when you wish to indicate that series does not apply to this book or that book is not part of a series. In this case xsl transforms (or other event based processors) that have a template that matches book:series will never be called. For example, if your xsl turns the book element into table row (xhtml:tr) you may get the incorrect number of table cells (xhtml:td) using this method.
  2. Leaving the element empty - This could indicate that the series is "", or is unknown, or that the book is not part of a series. Any xsl transform (or other evernt based parser) that matches book:series will be called. The value of current() will be "". You will get the same number of xhtml:td tags using this method as with the next described one.
  3. Using xsi:nil="true" - This signifies that the book:series element is NULL, not just empty. Your xsl transform (or other event based parser) that have a template matching book:series will be called. The value of current() will be empty (not empty string). The main difference between this method and (2) is that the schema type of book:series does not need to allow the empty string ("") as a valid value. This makes no real sense for a series element, but for a language element that is defined as an enumerated type in the schema, xsi:nil="true" allows the element to have no data. Another example would be elements of type decimal. If you want them to be empty you can union an enumerated string that only allows "" and a decimal, or use a decimal that is nillable.
晨曦慕雪 2024-07-24 01:01:02

没有规范的答案,因为 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.

何必那么矫情 2024-07-24 01:01:02

这取决于您如何验证 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 the xsi:nil attribute.

[Source]

澜川若宁 2024-07-24 01:01:02

w3 链接中的文档:

http://www.w3.org/ TR/REC-xml/#sec-starttags

表示这些是推荐的形式:

<test></test>
<test/>

另一个答案中提到的属性是验证机制,而不是状态的表示。 请参考:http://www.w3.org/TR/xmlschema-1 /#xsi_nil

XML 模式:结构引入了一种机制,用于表示
当元素没有内容(尽管有)时,应将其视为“有效”

不需要甚至不一定允许为空的内容类型
内容。 如果一个元素具有以下条件,那么它在没有内容的情况下可能是“有效”的:
属性 xsi:nil ,值为 true。 这样标记的元素必须是
,但如果相应的允许,可以携带属性
复杂类型。

为了澄清这个答案:

<?xml version="1.0" encoding="utf-8" ?>
<Books>
  <Book>
    <!--This element should alway be empty-->
    <BuildAttributes HardCover="true" Glued="true" xsi:nil="true"/>
    <Index></Index>
    <pages>
      <page pageNumber="1">Content</page>
    </pages>
    <!--Valid representation of a null or empty ISBN-->
    <ISBN></ISBN>
  </Book>

  <Book>
    <!--Invalid construct since the element attribute xsi:nil="true" signal that the element must be empty-->
    <BuildAttributes HardCover="true" Glued="true" xsi:nil="true">
      <anotherAttribute name="Color">Blue</anotherAttribute>
    </BuildAttributes>
    <Index></Index>
    <pages>
      <page pageNumber="1">Content</page>            
    </pages>
    <!--Missing ISBN could be confusing and misguiding since its not present-->
  </Book>
</Books>

The documentation in the w3 link:

http://www.w3.org/TR/REC-xml/#sec-starttags

says that these are the recommended forms:

<test></test>
<test/>

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

XML Schema: Structures introduces a mechanism for signaling that an
element should be accepted as ·valid·
when it has no content despite a
content type which does not require or even necessarily allow empty
content. An element may be ·valid· without content if it has the
attribute xsi:nil with the value true. An element so labeled must be
empty
, but can carry attributes if permitted by the corresponding
complex type.

To clarify this answer:

<?xml version="1.0" encoding="utf-8" ?>
<Books>
  <Book>
    <!--This element should alway be empty-->
    <BuildAttributes HardCover="true" Glued="true" xsi:nil="true"/>
    <Index></Index>
    <pages>
      <page pageNumber="1">Content</page>
    </pages>
    <!--Valid representation of a null or empty ISBN-->
    <ISBN></ISBN>
  </Book>

  <Book>
    <!--Invalid construct since the element attribute xsi:nil="true" signal that the element must be empty-->
    <BuildAttributes HardCover="true" Glued="true" xsi:nil="true">
      <anotherAttribute name="Color">Blue</anotherAttribute>
    </BuildAttributes>
    <Index></Index>
    <pages>
      <page pageNumber="1">Content</page>            
    </pages>
    <!--Missing ISBN could be confusing and misguiding since its not present-->
  </Book>
</Books>
離殇 2024-07-24 01:01:02

当您的架构语义指示某个元素具有默认值并且如果该元素不存在时应使用默认值时,您可以使用 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 with xsi:nil.)

粉红×色少女 2024-07-24 01:01:02

在不太正式的数据中,简单地省略属性或元素效果很好。

如果您需要更复杂的信息,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 available
  • nilReason 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 by other:, 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.

半寸时光 2024-07-24 01:01:02

在许多情况下,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文