I am working on a Mirth channel where I am trying to validate an XML using the XSD file. In order to perform that I wrote the below JavaScript which was serving my purpose. I am using JAXP API library to perform this action.
My next step is: Any validation error should route to the Destination 1. Whereas, valid message should go to the Destination 2.
I would really appreciate if someone can guide me how to tweak this Javascript or write a filter logic to perform this action.
Thanks in advance !!!
var schemaFile = new Packages.java.io.File("C:\\hl7v3\\Test.xsd");
var schemaFactory = Packages.javax.xml.validation.SchemaFactory.newIns tance("http://www.w3.org/2001/XMLSchema");
var schema = schemaFactory.newSchema(schemaFile);
var reader = new Packages.java.io.StringReader(connectorMessage.get RawData());
var xmlFile = new Packages.javax.xml.transform.stream.StreamSource(r eader);
var validator = schema.newValidator();
try {
// validates the message
validator.validate(xmlFile);
// valid message
logger.info('valid');
} catch (err) {
// invalid message
logger.error('An Error Occurred:'+err.toString());
return false;
}
For above validation example we can use Mirth destinationSet filtering function as below,
we should have two destination connectors, one for valid messages transmission and other for error-ed messages
try {
// validates the message
validator.validate(xmlFile);
// valid message
logger.info('valid');
destinationSet.removeAllExcept(<sucess-destination-id>);
} catch (err) {
// invalid message
logger.error('An Error Occurred:'+err.toString());
destinationSet.removeAllExcept(<error-destination-id>);
return false;
}
//Replace destination ID based on your destination IDs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.