1

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.

user1441004
  • 386
  • 1
  • 5
  • 13
  • The fact that an answer to a question like this is accepted means that the originator gave it a tick, it doesn't make it definitive. When it comes to recommendations on the best way of doing X, there will generally be many opinions. – Michael Kay Jul 06 '18 at 22:45

1 Answers1

2

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

Matthew
  • 1,630
  • 1
  • 14
  • 19
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • OK, maybe I should have just skipped the info about best way to provide non-content. The tag comes to me with xsi:nil. Thank you for spotting the https problem! – user1441004 Jul 06 '18 at 23:37