I have a field defined as non-mandatory, but also has some details of the
type of data it should contain if it exists:

[schema snippet]
<element name="FilmReference" minOccurs="0">
        <simpleType>
                <restriction base="string">
                        <minLength value="1"/>
                        <maxLength value="11"/>
                        <pattern value="[*0-9 A-Z]*"/>
                </restriction>
        </simpleType>
</element>

I used Castor 1.1.1 to generate the classes from the schema, which I then
use for unmarshalling (including descriptors).

If the XML I receive contains no 'FilmReference' tag it unmarshalls and
validates successfully, but if it contains an empty tag, it fails with the
following error:

The following exception occured while validating field: _possibleMatchList
of class: com.btc.ba.fileinterface.possiblematches.PossibleMatches: The
following exception occured while validating field: _usage of class:
com.btc.ba.fileinterface.possiblematches.PossibleMatch: The following
exception occured while validating field: _filmReference of class:
com.btc.ba.fileinterface.possiblematches.Usage: Strings of this type must
have a minimum length of 1 characters{File: [not available]; line: 41998;
column: 22}
        at
org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(Unm
arshaller.java:775)
        at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:741)
        at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:621)
        at
com.btc.ba.fileinterface.DataLoader.processPossibleMatches(DataLoader.java:2
23)
        at
com.btc.ba.fileinterface.DataLoader.processFile(DataLoader.java:181)
        at com.btc.ba.fileinterface.DataLoader.run(DataLoader.java:128)
        at
com.btc.ba.fileinterface.ProcessController.doProcessing(ProcessController.ja
va:127)
        at
com.btc.ba.fileinterface.ProcessController.run(ProcessController.java:68)
Caused by: ValidationException: The following exception occured while
validating field: _possibleMatchList of class:
com.btc.ba.fileinterface.possiblematches.PossibleMatches: The following
exception occured while validating field: _usage of class:
com.btc.ba.fileinterface.possiblematches.PossibleMatch: The following
exception occured while validating field: _filmReference of class:
com.btc.ba.fileinterface.possiblematches.Usage: Strings of this type must
have a minimum length of 1 characters;
   - location of error: XPATH: /PossibleMatches
The following exception occured while validating field: _usage of class:
com.btc.ba.fileinterface.possiblematches.PossibleMatch: The following
exception occured while validating field: _filmReference of class:
com.btc.ba.fileinterface.possiblematches.Usage: Strings of this type must
have a minimum length of 1 characters
        at
org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:270)
        at
org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescripto
rImpl.java:1048)
        at org.exolab.castor.xml.Validator.validate(Validator.java:127)
        at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:921)
        at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1119
)
        at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatc
her.dispatch(Unknown Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:729)
        ... 6 more
Caused by: ValidationException: The following exception occured while
validating field: _usage of class:
com.btc.ba.fileinterface.possiblematches.PossibleMatch: The following
exception occured while validating field: _filmReference of class:
com.btc.ba.fileinterface.possiblematches.Usage: Strings of this type must
have a minimum length of 1 characters;
   - location of error: XPATH: /PossibleMatch
The following exception occured while validating field: _filmReference of
class: com.btc.ba.fileinterface.possiblematches.Usage: Strings of this type
must have a minimum length of 1 characters
        at
org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:270)
        at
org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescripto
rImpl.java:1048)
        at org.exolab.castor.xml.Validator.validate(Validator.java:127)
        at
org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:237)
        ... 19 more
Caused by: ValidationException: The following exception occured while
validating field: _filmReference of class:
com.btc.ba.fileinterface.possiblematches.Usage: Strings of this type must
have a minimum length of 1 characters;
   - location of error: XPATH: /Usage
Strings of this type must have a minimum length of 1 characters
        at
org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:270)
        at
org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(XMLClassDescripto
rImpl.java:1048)
        at org.exolab.castor.xml.Validator.validate(Validator.java:127)
        at
org.exolab.castor.xml.FieldValidator.validateInstance(FieldValidator.java:31
8)
        at
org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:263)
        ... 22 more
Caused by: ValidationException: Strings of this type must have a minimum
length of 1 characters
        at
org.exolab.castor.xml.validators.StringValidator.validate(StringValidator.ja
va:292)
        at
org.exolab.castor.xml.validators.StringValidator.validate(StringValidator.ja
va:340)
        at
org.exolab.castor.xml.FieldValidator.validateInstance(FieldValidator.java:31
6)
        at
org.exolab.castor.xml.FieldValidator.validate(FieldValidator.java:263)
        ... 26 more
[truncated]

I would have expected empty tags for non-mandatory fields to be ignored, but
this does not seem to be the case. Is there anything I can do about this
(other than asking the customer not to send empty tags)?

regards,

Alison


$$
BancTec Ltd
Reg Office: Jarman House, Mathisen Way, Poyle Road, Colnbrook, Berkshire SL3 
0HF. Incorporated in England & Wales - 1283512. VAT GB 228 4783 38.
______________________________________________________________________

Reply via email to