web stats
Working solution to validate a XML against a XSD - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 07-30-2013, 06:07 AM
capzulu capzulu is offline
Mirth Newb
 
Join Date: Jun 2013
Location: Portugal
Posts: 9
capzulu is on a distinguished road
Thumbs up Working solution to validate a XML against a XSD

Hello,

After creating a HL7 v3 channel, I was on the need to successfully validate an incoming HL7 v3 XML file against their XSD schema.
So I searched and asked in this forum for a solution and one Mirth Employee refered here to a Java Package that could do XML validation. Based on that and this question on stackoverflow I implemented a solution for Mirth Connect.

First you need to download JAXP, this is a Java API for XML Processing, and import the library into Mirth Connect.

Then with this code you can validate an incoming XML message against a XSD file:

Code:
// open schema file
var schemaFile = new Packages.java.io.File("path_to_xsd_file"); // like "C:\\hl7v3\\schema.xsd"
// create a schema object
var schemaFactory = Packages.javax.xml.validation.SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
var schema = schemaFactory.newSchema(schemaFile);

// Create a Reader from the incoming XML message
var reader = new Packages.java.io.StringReader(messageObject.getRawData());

// Acts as an holder for a transformation Source in the form of a stream of XML markup.
var xmlFile = new Packages.javax.xml.transform.stream.StreamSource(reader);

// create a new validator for the schema
var validator = schema.newValidator();

try {
	// validates the message
	validator.validate(xmlFile);
	// valid message
	logger.info('valid');
} catch (err) {
	// invalid message
	logger.error(err.toString());
}
I hope this helps all of you searching for a working solution.

PS: Based that XML validation is essential to HL7 v3 messages, maybe this library could be included in future releases of Mirth Connect or create a native support to XML validation.

Last edited by capzulu; 07-31-2013 at 06:11 AM.
Reply With Quote
  #2  
Old 07-30-2013, 05:47 PM
spencerr spencerr is offline
OBX.1 Kenobi
 
Join Date: Oct 2011
Posts: 29
spencerr is on a distinguished road
Default Nice job thanks for the post

Thanks for the heads up on validating an XML.
Reply With Quote
  #3  
Old 11-06-2013, 09:33 AM
rastababy rastababy is offline
OBX.2 Kenobi
 
Join Date: Mar 2008
Posts: 72
rastababy
Default

I'm able to load the xsd and validate the xml but, how does the message processing stop if there is a validation error in the xml? Do I need to throw a specific error or call a mirth function?

I want to the message to stop processing and throw an "error" alert to the dashboard showing the message has failed.
Reply With Quote
  #4  
Old 11-06-2013, 01:47 PM
capzulu capzulu is offline
Mirth Newb
 
Join Date: Jun 2013
Location: Portugal
Posts: 9
capzulu is on a distinguished road
Default

If the validation of the XML against the XSD fails, Mirth writes the error to the log window.

This code does that
Code:
catch (err) {
	// invalid message
	logger.error(err.toString());
}
If you want the message processing to stop, you must use this when the validation fails.
Code:
return false;
So you have
Code:
catch (err) {
	// invalid message
	logger.error(err.toString());
        return false;
}
Also, in the dashboard, i don't remember if it will increment the Filter or Error numbers.

Last edited by capzulu; 11-06-2013 at 02:01 PM.
Reply With Quote
  #5  
Old 11-07-2013, 06:22 AM
rastababy rastababy is offline
OBX.2 Kenobi
 
Join Date: Mar 2008
Posts: 72
rastababy
Default

Thanks!! Looks like it increments the "filter" numbers, not error.
Reply With Quote
  #6  
Old 11-07-2013, 03:41 PM
capzulu capzulu is offline
Mirth Newb
 
Join Date: Jun 2013
Location: Portugal
Posts: 9
capzulu is on a distinguished road
Default

Ok. Thanks for the information
Reply With Quote
  #7  
Old 01-17-2014, 07:47 AM
anky2u anky2u is offline
Mirth Newb
 
Join Date: Dec 2013
Posts: 8
anky2u is on a distinguished road
Default Validating the XML against the CCD XSD

Hi ,

I have tried to validate the same way you have described in this thread but I get a error :

Failed to read schema document 'file:/C:/Program%20Files/Mirth%20Connect/UsersanshumanbDesktopCDA%20PROJECTCDA%20PROJECTsch ema.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.


Not sure why this error is there , could you please help me on this.


Thanks and Regards,

Anky
Reply With Quote
  #8  
Old 01-22-2014, 02:09 AM
capzulu capzulu is offline
Mirth Newb
 
Join Date: Jun 2013
Location: Portugal
Posts: 9
capzulu is on a distinguished road
Default

Hello Anky,

It seems to me that the path to the XSD file is incorrect.

To see if the problem is with the XSD file, use my example.
Copy your file to a directory like c:\hl7v3\schema.xsd and then update this line
Code:
var schemaFile = new Packages.java.io.File("C:\\hl7v3\\schema.xsd");
Test it and if the problems persists, then the problem is with your XSD file.
Reply With Quote
  #9  
Old 12-20-2016, 10:39 PM
varvaid varvaid is offline
What's HL7?
 
Join Date: Nov 2016
Posts: 4
varvaid is on a distinguished road
Default Error:messageObject not defined

I am getting an error of "messageObject" not defined when trying to use the above code to validate the xml against the xsd.

Any help would be appreciated.

SCRIPT SOURCE: TRANSFORMER
SOURCE CODE:
51: // create a schema object
52: var schemaFactory = Packages.javax.xml.validation.SchemaFactory.newIns tance("http://www.w3.org/2001/XMLSchema");
53: var schema = schemaFactory.newSchema(schemaFile);
54:
55: // Create a Reader from the incoming XML message
56: var reader = new Packages.java.io.StringReader(typeof messageObject.getRawData());
57:
58: // Acts as an holder for a transformation Source in the form of a stream of XML markup.
59: var xmlFile = new Packages.javax.xml.transform.stream.StreamSource(r eader);
60:
LINE NUMBER: 56
DETAILS: ReferenceError: "messageObject" is not defined.
at fb7ccbd7-64f0-4e38-9ece-3aa19742ae0a:56 (doTransform)
at fb7ccbd7-64f0-4e38-9ece-3aa19742ae0a:94 (doScript)
at fb7ccbd7-64f0-4e38-9ece-3aa19742ae0a:96
at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:154)
at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:119)
at com.mirth.connect.server.util.javascript.JavaScrip tTask.call(JavaScriptTask.java:113)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
at java.lang.Thread.run(Unknown Source)
Reply With Quote
  #10  
Old 12-21-2016, 01:40 AM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 832
siddharth is on a distinguished road
Default

This original post is from 2013. I think you are on 3.0 and higher mirth version.
messageObject is deprecated now. To get the raw message use

connectorMessage.getRawData()
Reply With Quote
Reply

Tags
java, javascript, jaxp, xml validation, xsd

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 02:42 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation