表示空 XML 元素的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/774192/
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
What is the correct way to represent null XML elements?
提问by Rob Hruska
I have seen nullelements represented in several ways:
我已经看到null以多种方式表示的元素:
The element is present with xsi:nil="true":
该元素存在于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 nullare semantically different):
该元素存在,但表示为一个空元素(我认为这是错误的,因为“空”并且null在语义上是不同的):
<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 TStamperbelow):
该元素有一个<null/>子元素(来自下面的TStamper):
<book>
<title>Beowulf</title>
<author><null/></author>
</book>
Is there a correct, or canonical way to represent such a nullvalue? Are there additional ways than the above examples?
是否有正确或规范的方式来表示这样的null值?除了上面的例子还有其他方法吗?
The XML for the examples above is contrived, so don't read too far into it. :)
上面例子的 XML 是人为设计的,所以不要读得太深。:)
采纳答案by KitsuneYMG
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.
xsi:nil 是表示值的正确方法,这样: 当发出 DOM Level 2 调用 getElementValue() 时,返回 NULL 值。xsi:nil 还用于指示没有内容的有效元素,即使该元素内容类型通常不允许空元素。
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.)
如果使用空标记,则 getElementValue() 返回空字符串 ("") 如果省略标记,则甚至不存在作者标记。这在语义上可能与将其设置为 'nil' 不同(例如,将“Series”设置为 nil 可能是这本书不属于任何系列,而省略系列可能意味着该系列是当前元素不适用的元素。)
From: The W3C
来自: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.
XML Schema:Structures 引入了一种机制,当元素没有内容时,尽管内容类型不需要或什至必须允许空内容,但该元素应该被接受为·有效·。如果元素具有属性 xsi:nil 且值为 true,则它可能是·valid· 没有内容的。如此标记的元素必须为空,但如果相应的复杂类型允许,则可以携带属性。
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:
澄清:
如果您有一个 book xml 元素并且其中一个子元素是 book:series,那么您在填写时有几个选项:
- 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.
- 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.
- 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.
- 完全删除元素 - 当您希望表明该系列不适用于本书或该书不是系列的一部分时,可以执行此操作。在这种情况下,将永远不会调用具有匹配 book:series 模板的 xsl 转换(或其他基于事件的处理器)。例如,如果您的 xsl 将 book 元素转换为表格行 (xhtml:tr),您可能会使用此方法获得不正确数量的表格单元格 (xhtml:td)。
- 将元素留空 - 这可能表示该系列是“”,或者未知,或者该书不是系列的一部分。任何匹配 book:series 的 xsl 转换(或其他基于 evernt 的解析器)都将被调用。current() 的值将是“”。使用此方法,您将获得与下一个描述的相同数量的 xhtml:td 标签。
- 使用 xsi:nil="true" - 这表示 book:series 元素为 NULL,而不仅仅是空。将调用具有匹配 book:series 的模板的 xsl 转换(或其他基于事件的解析器)。current() 的值将为空(不是空字符串)。此方法与 (2) 的主要区别在于 book:series 的模式类型不需要允许空字符串 ("") 作为有效值。这对于 series 元素没有实际意义,但对于在模式中定义为枚举类型的语言元素, xsi:nil="true" 允许元素没有数据。另一个例子是十进制类型的元素。如果您希望它们为空,您可以联合一个只允许 "" 和一个小数的枚举字符串,或者使用一个可以为空的小数。
回答by StaxMan
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:nilis the way to go. For most mappers, omitting matching element/attribute is the way to do it.
没有规范的答案,因为 XML 从根本上没有空概念。但我假设你想要 Xml/Object 映射(因为对象图有空值);所以你的答案是“无论你的工具使用什么”。如果你写处理,那意味着你喜欢什么。对于使用 XML Schema 的工具,xsi:nil是要走的路。对于大多数映射器,省略匹配元素/属性是这样做的方法。
回答by Tormod Fjeldsk?r
回答by Oakcool
The documentation in the w3 link
w3 链接中的文档
http://www.w3.org/TR/REC-xml/#sec-starttags
http://www.w3.org/TR/REC-xml/#sec-starttags
says that this are the recomended forms.
说这是推荐的形式。
<test></test>
<test/>
The attribute mentioned in the other answer is validation mechanism and not a representation of state. Please refer to the http://www.w3.org/TR/xmlschema-1/#xsi_nil
另一个答案中提到的属性是验证机制,而不是状态的表示。请参考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.
XML Schema:Structures 引入了一种机制,当元素没有内容时,尽管内容类型不需要或什至必须允许空内容,但该元素应该被接受为·有效·。如果元素具有属性 xsi:nil 且值为 true,则它可能是·valid· 没有内容的。如此标记的元素必须为空,但如果相应的复杂类型允许,则可以携带属性。
To clarify this answer: Content
澄清这个答案:内容
<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 number could be confusing and misguiding since its not present-->
</Book>
</Books>
回答by Robert Rossney
You use xsi:nilwhen 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.)
您可以使用xsi:nil,当你的模式语义指示元素有一个默认值,如果元素不存在应使用默认值。我必须假设有一些聪明人认为前一句不是一个不言而喻的可怕想法,但对我来说听起来像是九种坏事。我使用过的每种 XML 格式都通过省略元素来表示空值。(或属性,祝你好运标记一个属性xsi:nil。)
回答by Andy Dent
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:
如果您需要更复杂的信息,GML 模式添加属性 nilReason,例如:在GeoSciML 中:
xsi:nilwith a value of "true" is used to indicate that no value is availablenilReasonmay 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.
xsi:nil值为 "true" 用于表示没有可用值nilReason可用于记录缺失值的附加信息;这可能是标准 GML 原因 (missing, inapplicable, withheld, unknown) 之一,或者以 开头的文本other:,或者可能是指向更详细解释的 URI 链接。
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.
当您交换数据时,通常使用 XML 的角色、发送给一个接收者或用于给定目的的数据可能具有模糊的内容,而其他人可以使用这些内容进行支付或进行不同的身份验证。了解内容缺失的原因非常重要。
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.
科学家们还担心信息缺失的原因。例如,如果它因质量原因被删除,他们可能希望看到原始的坏数据。
回答by Jeroen Dirks
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.
在许多情况下,Null 值的目的是为应用程序的先前版本中不存在的数据值提供服务。
So say you have an xml file from your application "ReportMaster" version 1.
因此,假设您的应用程序“ReportMaster”版本 1 中有一个 xml 文件。
Now in ReportMaster version 2 a some more attributes have been added that may or not be defined.
现在在 ReportMaster 版本 2 中添加了更多属性,这些属性可能定义也可能未定义。
If you use the 'no tag means null' representation you get automatic backward compatibility for reading your ReportMaster 1 xml file.
如果您使用“无标签意味着空”表示,您将获得自动向后兼容性以读取您的 ReportMaster 1 xml 文件。

