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

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

问题描述

我已经看到null元素以几种方式表示:

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

 <book>
     <title>Beowulf</title>
     <author xsi:nil="true"/>
 </book>

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

 <book>
     <title>Beowulf</title>
     <author/>
 </book>

 <!-- or: -->
 <book>
     <title>Beowulf</title>
     <author></author>
 </book>

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

 <book>
     <title>Beowulf</title>
 </book>

元素具有<null/>子元素(来自解决方案

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

如果使用空标记,则getElementValue()返回空字符串(") 如果省略标签,那么甚至没有作者标签.这可能与将其设置为"nil"在语义上有所不同(例如,将系列"设置为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"允许该元素不包含数据.另一个示例是小数类型的元素.如果希望它们为空,则可以合并仅允许使用"和小数的枚举字符串,也可以使用可为空的小数.

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天全站免登陆