9

I am working on getting an xml file to validate against an XSD schema and I'm having trouble with the validations. Every time I validate I get errors saying

"Schemas validity error: Element '{http://services.website.com/ProgramResponse}Population': '' is not a valid value of the atomic type 'xs:double'."

I believe this error happens because I have a null character in that field, displayed like this:
< HarvPop>< /HarvPop>

So, to solve this I tried using the nillable="true" attribute for the elements so they will be able to be null, but still show up as empty. This seems to be the only solution, but it is not working at all. I still get the errors.

I am currently using XMLMate for my validations and I have double checked it agains several online verifiers as well. The error still persists. Any suggestions would be great.

<?xml version="1.0" encoding="UTF-8"?>

<xsd:element name="Reports" type="tns:ReportsType"/>

<xsd:complexType name="ReportsType">
    <xsd:sequence>
        <xsd:element name="Report" type="tns:ReportType" maxOccurs="unbounded" minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="ReportType">
    <xsd:sequence>
        <xsd:element name="Id" nillable="true"/>
        <xsd:element name="Brand" type="xsd:string"/>
        <xsd:element name="Address" type="xsd:string"/>
        <xsd:element name="City" type="xsd:string"/>
        <xsd:element name="State" type="xsd:string"/>
        <xsd:element name="ZipCode" type="xsd:string"/>
        <xsd:element name="Entry" type="tns:EntryType" maxOccurs="unbounded" minOccurs="1"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="EntryType">
    <xsd:sequence>
        <xsd:element name="RM" nillable="true" type="xsd:double"/>
        <xsd:element name="Pop" nillable="true" type="xsd:double"/>
        <xsd:element name="Wt" nillable="true" type="xsd:double"/>
        <xsd:element name="EntryId" type="xsd:int"/>
    </xsd:sequence>
</xsd:complexType>

sublimeike
  • 182
  • 1
  • 1
  • 10

2 Answers2

11

A node like < HarvPop>< /HarvPop> is stating that the value is there and that it's value is an empty string.

Based on the information on this w3.org page: http://www.w3.org/TR/xmlschema-0/#Nils

The nillable attribute is used like this:

Definition: <xsd:element name="shipDate" type="xsd:date" nillable="true"/>

Usage: <shipDate xsi:nil="true"></shipDate>

ie You have to specifically state that the value is null.

The other way to do it is to state minoccurs = 0, to allow the value to be missing.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • 2
    Alternatively, change the type from xs:double to a list type whose itemType is xs:double, with minLength=0 and maxLength=1. That way you can omit the value without the redundant xsi:nil attribute. – Michael Kay Jul 26 '11 at 23:45
  • do you have to have xsi:nil="true" in the xml when using minOccurs as well? – sublimeike Jul 27 '11 at 14:56
  • 2
    No. nil means that you can have the node present and empty, minOccurs=0 means the whole node can be missing. – Jon Egerton Jul 27 '11 at 15:03
4

One way that I found that also fixes the problem with minimal code was to add the default="0" attribute to the XSD. This allows you to validate as a double without having to deal with nil by making nil default to a number.

Error deserialising XML document with strongly typed XSD

Community
  • 1
  • 1
sublimeike
  • 182
  • 1
  • 1
  • 10