How do I enable schema validation so that JAXB rejects an empty element?

I ran into a JAXB parsing problem. I am using JAXB RI 2.x. I have enabled schema validation using "unmarshaller.setSchema (schema)". However, if XML contains an empty element, JAXB does not throw any validation errors. Thus, clients happily pass empty string values!

This is how an element is declared in a schema:

(see my comments below)

Here's how it appears in an XML instance:

(see my comments below)

Although this is a required field, it is successfully validated by JAXB. How to enable rejection of such empty elements?

thank

+1
source share
2 answers

XML-, xsd:, minLength.

<xs:element name="ChargeCode" type="stringMinSize1"/>
<xs:simpleType name="stringMinSize1">
    <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
    </xs:restriction>
</xs:simpleType>

, pattern, , . , :

<xs:element name="PostalCode" type="postalCode"/>
<xs:simpleType name="postalCode">
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Z]{1}[0-9]{1}[A-Z]{1} [0-9]{1}[A-Z]{1}[0-9]{1}"/>
    </xs:restriction>
</xs:simpleType>

, .

:

+3

minOccurs

0

All Articles