I'm working on adding an XSD validator feature to a tool I created at work. The tool actually generates an XML with very strict formatting. The format of the XML is defined in a mapping document and there is also an XSD schema.
What I want is to add the ability for a user to quickly validate an XML they create in the tool. I have added all of the code for this validation to occur. Unfortunately, I am having a couple of unexpected problems.
Specifically in one section of the XML are about 20 tags. These tags will not always appear in an XML file, many are optional. I need the validation to ignore the optional tags if they are not present - Instead the validator returns an error EVEN IF I set the "Minoccurs" value to 0.
Here is my validator code:
package misc;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
public class XMLValidation {
public static JFileChooser uploadFile;
public static boolean validateXMLSchema(String xsdPath, String xmlPath){
try{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlPath)));
}catch(IOException | SAXException e){
JOptionPane.showMessageDialog(null, e.getMessage());
return false;
}
JOptionPane.showMessageDialog(null, "No errors detected!");
return true;
}
public static void uploadFile(){
boolean uploadApproval = false;
while(uploadApproval==false){//While upload approval has not been given..
JFileChooser chooser = new JFileChooser();//Creates a new object of the JFileChooser class.
uploadFile = chooser;//Saves the upload file variable as the chooser response.
FileNameExtensionFilter filter = new FileNameExtensionFilter("XML Files", "xml");
//Sets the allowed file formats for upload.
chooser.setFileFilter(filter);//Activates the created file filter.
chooser.setDialogTitle("Select a Camt54 file to validate");//Sets the title bar text.
//Completes once the user clicks ok.
int returnVal = chooser.showOpenDialog(chooser);//
if(returnVal == JFileChooser.APPROVE_OPTION){
uploadApproval=true;
}else{
System.exit(0);
}
}
}
}
I can't share my entire XSD schema because of security. I attempted to take out a portion that would illustrate what I mean:
<xs:complexType name="CntsRecord1">
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" name="test1" type="ISODateTime"/>
<xs:element maxOccurs="1" minOccurs="0" name="test2" type="ISODateTime"/>
<xs:element maxOccurs="1" minOccurs="0" name="test3" type="ISODateTime"/>
<xs:element maxOccurs="1" minOccurs="0" name="test4" type="type4"/>
<xs:element maxOccurs="1" minOccurs="0" name="test5" type="Max35Text"/>
<xs:element maxOccurs="1" minOccurs="0" name="test6" type="Max140Text"/>
<xs:element maxOccurs="1" minOccurs="0" name="test7" type="type5"/>
<xs:element maxOccurs="1" minOccurs="0" name="Test" type="Max140Text"/>
</xs:sequence>
</xs:complexType>
As you can see, the "minOccurs" for all of these values is set at 0, so they should not return an error.
Once I run this code however I will get back an error that the element "Test" is expected. (Assuming all the others are present).
Is there some difficulty in running the validation with the xsd and using minimum values of 0?
Here is the error I get back from the validator:
cvc-complex-type.2.4.a:Invalid content was found starting with element 'Test10'. One of {"urn:iso:std:iso:20022:tech:xsd:camt:054:001:04:"Test}' is expected.
Here is the example from my XML file:
-<Cnts>
<Test1>2016-08-18T09:51:41</Test1>
<Test2>2016-08-18T09:51:41</Test2>
<Test3>2016-08-18T09:51:41</Test3>
<Test4>PAPER</Test4>
<Test5>2016-08-18T09:51:41</Test1>
<Test6>2016-08-18T09:51:41</Test2>
<Test7>2016-08-18T09:51:41</Test3>
<Test10>PAPER</Test4>
Any help would be greatly appreciated.