I'm using this code to validate a XML against a XSD:
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(xmlSchema);
Validator validator = schema.newValidator();
Source source = new StreamSource(myXmlFile);
try {
validator.validate(source);
return null;
}catch (SAXException ex) {
String validationMessage = ex.getMessage();
return validationMessage;
}
But when the XML is not valid, the message is always something like this:
cvc-minLength-valid: Value '-' with length = '1' is not facet-valid with respect to minLength '2' for type '#AnonType_xLgrTEndereco'.
Is there a way to return a user friendly message in my language, without having to translate the message programatically?
UPDATE This is piece of code in the XSD for the field in which the message occured:
<xs:element name="xLgr">
<xs:annotation>
<xs:documentation>Logradouro</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="TString">
<xs:maxLength value="60"/>
<xs:minLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
As you can see it even has a description to return the "name" of the field (Logradouro) but the schema validator seems not to recognize it.