Reputation: 1405
I am having a bit of a problem with empty values failing validation on one server, but it works on all my other servers. The problem occurs on a newer PHP version (5.3.8) and works on a slightly older version (5.2.4).
Here are a couple of isolated elements which fail:
XML Input:
<clbburnfuelbias></clbburnfuelbias>
<clbburntimebias></clbburntimebias>
XSD Validation:
<xs:element name="clbburnfuelbias" type="data-fuelbias"/>
<xs:element name="clbburntimebias" type="data-timebias"/>
<xs:simpleType name="data-fuelbias">
<xs:restriction base="xs:integer">
<xs:minInclusive value="-9900"/>
<xs:maxInclusive value="9900"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="data-timebias">
<xs:restriction base="xs:integer">
<xs:minInclusive value="-59"/>
<xs:maxInclusive value="59"/>
</xs:restriction>
</xs:simpleType>
Error:
Error 1824: Element 'clbburnfuelbias': The value is not a valid value of the atomic type 'data-fuelbias'. Error 1824: Element 'clbburntimebias': The value is not a valid value of the atomic type
'data-timebias'.
Using the exact same input and XSD files on different servers running 5.2.4 I get no errors and the xml is valid.
I have tried adding minOccurs="0" to the elements, I have tried specifying nullable="true" as well but still get the errors.
It appears that null values are ignored on my servers using PHP 5.2.4, and rejected using 5.3.8. A work around that I've used which was suggested is to use a union of two definitions, one for the integer type, and one for a null value:
XSD:
<xs:element name="clbburnfuelbias" minOccurs="0">
<xs:simpleType>
<xs:union memberTypes="data-fuelbias null-string"/>
</xs:simpleType>
</xs:element>
<xs:simpleType name="data-fuelbias">
<xs:restriction base="xs:integer">
<xs:minInclusive value="-9900"/>
<xs:maxInclusive value="9900"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="null-string">
<xs:restriction base="xs:string">
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
Upvotes: 2
Views: 4261
Reputation: 163262
Looks like the old version of PHP had a bug which has since been fixed. These element instances are clearly invalid.
My preferred way of defining a simple type for elements that can contain an integer or nothing is to define a list type with itemType=integer, minLength=0, maxLength=1. The main reason I prefer that over a union type is that it works better in schema-aware XSLT and XQuery; it might also work better for some data binding products.
Upvotes: 3
Reputation: 1493
The data needs to be an integer between the values you've specified in the XSD. You are passing an empty value. At least... that is how it should be validating. I think the XML you are passing should NOT validate according to the schema.
Maybe the XML parser version is different between the PHP versions, and the older version was forgiving. Either way, I read that schema as saying: the two fields must be present exactly once, and must contain an integer between x and y (where x and y are specified in minInclusive and maxInclusive). An empty value should fail validation.
Upvotes: 2
Reputation: 8135
Making an element optional (minOccurs="0"
) doesn't make its contents a wildcard. If the element appears, it must match the declared type.
Making an element nillable (nillable="true"
) allows the element to be empty if it has the xsi:nil="true"
attribute, where xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
:
<clbburnfuelbias xsi:nil="true"></clbburnfuelbias>
<clbburntimebias xsi:nil="true"></clbburntimebias>
You may also use a union type, which only has the same meaning for validation purposes:
<xs:simpleType name="data-fuelbias">
<xs:union>
<xs:restriction base="xs:integer">
<xs:minInclusive value="-9900"/>
<xs:maxInclusive value="9900"/>
</xs:restriction>
<xs:restriction base="xs:string">
<xs:length value="0"/>
</xs:restriction>
</xs:union>
</xs:simpleType>
Upvotes: 5