表示空 XML 元素的正确方法是什么? [英] What is the correct way to represent null XML elements?

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

问题描述

我见过以多种方式表示的 null 元素:

元素与 xsi:nil="true" 一起出现:

 <title>贝奥武夫</title><author xsi:nil="true"/>

元素存在,但表示为一个空元素(我认为这是错误的,因为 'empty' 和 null 在语义上是不同的):

 <title>贝奥武夫</title><作者/><!-- 或:--><书><title>贝奥武夫</title><作者></作者>

返回的标记中根本不存在该元素:

 <title>贝奥武夫</title>

该元素有一个 子元素(来自 TStamper 下面):

 <title>贝奥武夫</title><author><null/></author>

是否有正确或规范的方式来表示这样的 null 值?除了上面的例子还有其他方法吗?

上述示例的 XML 是人为设计的,因此不要深入研究.:)

解决方案

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

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

来自:W3C

<块引用>

XML Schema:Structures 引入了一个发出信号的机制元素应被接受为·有效·当它没有内容时,尽管有不需要或的内容类型甚至必须允许空内容.一个元素可能·有效·没有内容,如果它有属性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" 允许元素没有数据.另一个例子是十进制类型的元素.如果您希望它们为空,您可以联合一个只允许 "" 和一个小数的枚举字符串,或者使用一个可以为空的小数.

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. :)

解决方案

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.

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

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