Null elements for primitive data types prohibited in XSD

I encountered a parsing error with Apache CXF while processing webservice response. It is about returning an empty element:

<myValue /> 

An element definition is as follows:

 <xsd:element name="myValue" type="xsd:float" minOccurs="0"> 

Now I read on the CXF mailing list that the value is not valid using the XSD specification :

Well, there is no workaround for this as it is not a mistake. An empty element is not valid for any decimal or date type or something like that.
Thus, an exception MUST be thrown.
What do you expect from this?

Now the question is: where exactly can I find this limitation in the XML Schema specification?

+2
xsd cxf xsd-validation
source share
2 answers

Where exactly can I find this limitation in the XML Schema specification?

http://www.w3.org/TR/xmlschema-2/#float-lexical-representation

float values ​​have a lexical representation of the mantissa, optionally using the symbol "E" or "e", followed by an exponent.
...
The representations for the exponent and the mantissa must follow the lexical rules for integers and decimal numbers.
...
Of particular importance are positive and negative infinity and non-number have lexical representations INF, -INF and NaN, respectively.

So xs:float requires at least the mantissa, which is xs:decimal ...

decimal has a lexical representation consisting of a sequence with a finite length of decimal digits (# x30- # x39) separated by a period as a decimal indicator. An optional pointer is allowed.

... and an empty string is not valid xs:decimal .

If you do not have a value for this element, try not to include this element if possible. It seems your schema allows you to exclude this element, because minOccurs has a value of 0 . Another solution would be to insert a suitable replacement value, such as 0 or NaN .

+2
source share

This is not a final limitation. You can change xsd to

 <xsd:element name="myValue" type="xsd:float" minOccurs="0" default="0" /> 

And then you can provide an empty element for your float without causing rejection of your xml.

The above example means that if an element is empty, then its value is 0. Beware, the default attribute does not apply to missing elements: missing elements are simply absent, whether they have a default declared or not. http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints

if an element appears without any content, the schema processor provides the element with a value equal to the default attribute value. However, if the item does not appear in the instance document, the schema processor does not provide the item at all.

I have not used this yet, but to protect against personally reading the w3c specifications, I checked with the online validator that an xml was accepted with the empty xs: float element having a default value (at least this online validator: http: //www.freeformatter.com/xml-validator-xsd.html ).

0
source share

All Articles