Reputation: 436
This question is asking about the correct way to represent NULL in XML: What is the correct way to represent null XML elements?
The accepted answer seems to be that providing the attribute xsi:nil="true" is the best way to explicitly provide empty content.
Another question refers to this tutorial regarding how to declare a nillable element in your XSD, and to provide one:
http://www.zvon.org/xxl/XMLSchemaTutorial/Output/ser_over_st0.html
Given that information, I beleive I am following the guidelines, but my XSD validation is failing. Here's a super-simple piece of XML giving nil for the second child element:
<?xml version="1.0"?>
<plant xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
<genus>GenusValue</genus>
<species xsi:nil="true" />
</plant>
And here is my XSD that I think should validate that:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="plant">
<xs:complexType>
<xs:sequence>
<xs:element name="genus">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="60"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="species" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="60"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The validation function within ColdFusion2016 (XmlValidate()) fails (with a not very helpful error message). Notepad++ (with installed XML validation tools) will also fail this validation, giving the error message:
Validation of current file using XML schema:
ERROR: Element 'species', attribute '{https://www.w3.org/2001/XMLSchema-instance}nil': The attribute '{https://www.w3.org/2001/XMLSchema-instance}nil' is not allowed.
Code generating the XML is not under my control. I've figured out how to validate:
<species></species>
but that's a different issue - it's not the way the XML is currently being provided. (So, solutions about unions with empty strings, minOccurs, etc. are not helpful unless they validate a tag as is given above.)
Maybe I am just missing or misunderstanding something about the XSD? (this also seems to follow the documentation given here: https://www.w3schools.com/xml/el_element.asp , though they are not actually showing XML which gives the xsi:nil="true" value.)
The zvon example is setting a tag which has a child as nillable, whereas the tag I am trying to set nillable is a child of something else and has no children itself, but it still seems like I am following the functional parts of what the zvon example is demonstrating?
Help?!? ;)
Thanks.
Upvotes: 1
Views: 4589
Reputation: 163302
There are at least three common ways to represent absent values in XML:
(a) Omit the element entirely (b) Include the element, but leave its content empty (c) Include the element, leave its content empty, and add the attribute xsi:nil="true"
None of these is universally better than the others. Personally, I usually find that (a) is the most convenient, but others might disagree. Some would argue that you need to distinguish different kinds of null value: (a) a person is known to have no middle name vs. (b) a person's middle name is unknown.
The error in your example has nothing to do with these design considerations. You have simply got the namespace wrong. https://www.w3.org/2001/XMLSchema-instance
should be http://www.w3.org/2001/XMLSchema-instance
(that is http not https).
Upvotes: 2