xml <a_element /> 和 <a_element xsi:nil="true" /> 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3760629/
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's the difference between <a_element /> and <a_element xsi:nil="true" />?
提问by darkly_dreaming_dexter
do you know if there's a difference between these tags on XML/XSD?
你知道这些标签在 XML/XSD 上是否有区别吗?
<a_element /> and <a_element xsi:nil="true"/>
e.g:
例如:
<SpreadCurve>
<Index>3M</Index>
<IndexNumber>4587</IndexNumber>
<BusinessArea xsi:nil="true" />
</SpreadCurve>
and
<SpreadCurve>
<Index>3M</Index>
<IndexNumber>4587</IndexNumber>
<BusinessArea />
</SpreadCurve>
Are these equivalent?
这些是等价的吗?
If I have a XSD element:
如果我有一个 XSD 元素:
<xsd:element name="BusinessArea" type="xsd:string"/>
this means that it is by default xsi:nil="false". And this means it will not accept a null value for this element.
这意味着默认情况下它是 xsi:nil="false"。这意味着它不会接受此元素的空值。
My doubt is, will it accept this one?
我的疑问是,它会接受这个吗?
<BusinessArea />
What does this really mean to the XSD?
这对 XSD 到底意味着什么?
Best regards
此致
回答by YoK
You get this as your XSD BusinessArea should be defined as nillable="true". Something like:
你得到这个是因为你的 XSD BusinessArea 应该被定义为 nillable="true"。就像是:
<xsd:element name="BusinessArea" nillable="true">
.....
</xsd:element>
What this mean is that BusinessArea element can have null value i.e. empty.
这意味着 BusinessArea 元素可以具有空值,即为空。
And if element in XML doesn't contain any value then it must have attribute xsi:nil="true":
如果 XML 中的元素不包含任何值,则它必须具有属性 xsi:nil="true":
<BusinessArea xsi:nil="true" />
This should be invalid :
这应该是无效的:
<BusinessArea />
Two examples you showed should not be equivalent.
你展示的两个例子不应该是等价的。
Check this out for understanding xsi:nil and nillable:
检查一下以了解 xsi:nil 和 nillable:
http://www.zvon.org/xxl/XMLSchemaTutorial/Output/ser_over_st0.html
http://www.zvon.org/xxl/XMLSchemaTutorial/Output/ser_over_st0.html
回答by RobertPitt
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· 没有内容的。如此标记的元素必须为空,但如果相应的复杂类型允许,则可以携带属性。
回答by Niek
<a_element />
is the equivalent of an empty stringand will render valid against xsd:string, but not against types like xsd:date, xsd:datetime, xsd:decimal etc.
与 an 等效,empty string并且将对 xsd:string 呈现有效,但不适用于 xsd:date、xsd:datetime、xsd:decimal 等类型。
<a_element xsi:nil="true"/>
is the equilalent of nulland will render valid for all elements which have nillable="true" in the schema-definition
是等价的,null并且将对架构定义中具有 nillable="true" 的所有元素呈现有效
回答by Pedro
My understanding is that they are not the same. At least if you want to validate the xml against a schema. If in your schema you define the element as nillable, let's say:
我的理解是它们不一样。至少如果您想根据架构验证 xml。如果在您的架构中您将元素定义为 nillable,让我们说:
<xsd:element name="SomeName" type="xsd:double" nillable="true"/>
You need to explicitly in your xml set that element to null, like this:
您需要在 xml 中明确将该元素设置为 null,如下所示:
<SomeName xsi:nill="true" />
If in your xml the element is like <SomeName />it will not be valid according to the schema.
如果在您的 xml 中,元素就像<SomeName />根据架构一样无效。

