22

do you know if there's a difference between these tags on 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: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.

My doubt is, will it accept this one?

<BusinessArea />

What does this really mean to the XSD?

Best regards

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Dark Defender
  • 273
  • 2
  • 4
  • 9
  • Related question for xml instead of xsd: [What does i:nil="true" mean?](http://stackoverflow.com/questions/463597/what-does-inil-true-mean) – Tim Abell Oct 14 '16 at 13:23

4 Answers4

13

You get this as your XSD BusinessArea should be defined as nillable="true". Something like:

<xsd:element name="BusinessArea" nillable="true">
.....
</xsd:element> 

What this mean is that BusinessArea element can have null value i.e. empty.

And if element in XML doesn't contain any value then it must have attribute 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:

http://www.zvon.org/xxl/XMLSchemaTutorial/Output/ser_over_st0.html

http://www.w3.org/TR/xmlschema-0/#Nils

YoK
  • 14,329
  • 4
  • 49
  • 67
  • Hi YoK, thanks for replying. I think I'm understanding now. Pls read my updates on the question body. – Dark Defender Sep 21 '10 at 14:37
  • @darklydreamingcoder should generate invalid document. Updated my answer with same. – YoK Sep 21 '10 at 15:05
  • Hi YoK, thank you, I've seen your update. Now I understand perfectly. I've updated my question with one more doubt (about non-null elements) – Dark Defender Sep 21 '10 at 15:42
  • Be aware when talking about "null value i.e. empty" and "doesn't contain any value". null and empty string are NOT the same! – Niek Jun 22 '15 at 06:40
4

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.

Source: http://www.w3.org/TR/xmlschema-1/#xsi_nil

Taoufik Mohdit
  • 1,910
  • 3
  • 26
  • 39
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
2
<a_element />  

is the equivalent of an empty string and will render valid against xsd:string, but not against types like xsd:date, xsd:datetime, xsd:decimal etc.

<a_element xsi:nil="true"/>

is the equilalent of null and will render valid for all elements which have nillable="true" in the schema-definition

Niek
  • 989
  • 2
  • 11
  • 23
0

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:

<xsd:element name="SomeName" type="xsd:double" nillable="true"/>

You need to explicitly in your xml set that element to null, like this:

<SomeName xsi:nill="true" />

If in your xml the element is like <SomeName /> it will not be valid according to the schema.

Pedro
  • 388
  • 1
  • 10
  • The question is not talking about xsd:double but about xsd:string. Empty string "" will render invalid against xsd:double, but will render valid against xsd:string – Niek Jun 22 '15 at 06:31