원시적 인 데이터 유형에 대한 빈 요소는 XSD에서 금지되었습니다
-
15-11-2019 - |
문제
웹 서비스 응답을 처리하는 동안 Apache CXF에서는 파싱 오류가 발생했습니다.그것이 오는 것은 빈 요소가 반환되는 빈 요소입니다 :
<myValue />
.
요소 정의는 다음과 같습니다.
<xsd:element name="myValue" type="xsd:float" minOccurs="0">
.
이제 빈 값을 읽은 CXF 메일 링리스트를 읽었습니다.xsd-spec /a> : 에서 허용되지 않습니다.
잘 해결 방법은 없습니다. 이것이 버그가 아닙니다.빈 요소는 소수점에 유효하지 않습니다 또는 날짜 유형 또는 그런 것.
따라서 예외를 던져야합니다.
당신은 무엇을 할 것인가?이제 여기에 다음 질문이 있습니다. XML 스키마 사양 에서이 제약 조건을 찾을 수 있습니까?
해결책
Where exactly can I find this constraint in the XML Schema specification?
http://www.w3.org/TR/xmlschema-2/#float-lexical-representation
float values have a lexical representation consisting of a mantissa followed, optionally, by the character "E" or "e", followed by an exponent.
...
The representations for exponent and mantissa must follow the lexical rules for integer and decimal.
...
The special values positive and negative infinity and not-a-number have lexical representations INF, -INF and NaN, respectively.
So xs:float
requires at least a mantissa that is a xs:decimal
...
decimal has a lexical representation consisting of a finite-length sequence of decimal digits (#x30-#x39) separated by a period as a decimal indicator. An optional leading sign is allowed.
...and an empty string is not a valid xs:decimal
.
If you don't have a value for this element, you should try not including this element, if possible. Your schema seems to allow omitting this element because minOccurs
has value 0
. Other solution would be to insert a suitable replacement value, like 0
or NaN
.
다른 팁
This is not a definitive constraint. You should be able to change your xsd to
<xsd:element name="myValue" type="xsd:float" minOccurs="0" default="0" />
And then be able to supply an empty element for your float without causing your xml to be invalid.
The above example means that if the element is empty, then its value is 0. Beware, default attribute does not apply on missing elements: missing elements are just missing, whether they have a declared default or not. http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints
if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all.
I have not used this till now, but to guard against a personal miss-reading of w3c specs, I have check with an online validator that an xml with an empty xs:float element having a default was accepted (at least by this online validator: http://www.freeformatter.com/xml-validator-xsd.html ).