web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
Old 04-24-2013, 04:47 PM
narupley's Avatar
narupley narupley is online now
Mirth Employee
Join Date: Oct 2010
Posts: 7,124
narupley is on a distinguished road

UPDATE: I've created a public GitHub repository to track these example channels, code templates, scripts, or whatever else!


To start with I only added the ones I wrote, because I didn't want to presume and add code from others without their explicit permission. Pull requests welcome!
Pursuant to a recent thread asking for this, I've expanded the stripEmptyNodes function to do more cool stuff.

stripEmptyNodes: Recursively iterates through all descendant nodes of an E4X XML object and removes those that are empty (or those whose children are all empty).

  • node: The node to iterate through. If node itself is empty, it will not be removed from its parent.
  • stripWhitespaceNodes: If true, nodes that are not empty but only contain whitespace will also be removed.
  • onlyDeleteTrailing: If true, only trailing nodes will actually be deleted. For example if a child set has five nodes at the end which are all empty, all five will be removed. However if the same five nodes are followed by a node which isn't empty, then those five nodes will not be removed. Instead, the nodes will simply have their inner text elements removed (if necessary).

  • Get rid of all empty nodes in the inbound XML structure (msg):
  • Get rid of all blank (empty and whitespace-only) nodes in the inbound XML structure:
  • Delete any trailing blank nodes, and replace any non-trailing whitespace-only nodes with empty nodes:

The code:

function stripEmptyNodes(node, stripWhitespaceNodes, onlyDeleteTrailing) {
	var nonEmptyFound = false;
	for (var childIndex = node.children().length()-1; childIndex >= 0; childIndex--) {
		var child = node.children()[childIndex];
		if (stripWhitespaceNodes && !child.toString().trim() || !stripWhitespaceNodes && !child.toString())
			if (!nonEmptyFound || !onlyDeleteTrailing)
				delete node.children()[childIndex];
				node.children()[childIndex] = new XML('<'+child.name().toString()+'/>');
			nonEmptyFound = true;
Attached Files
File Type: xml stripEmptyNodes (new 4-24-13).xml (1.0 KB, 178 views)
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.

- How do I foo?
- You just bar.

Last edited by narupley; 06-08-2018 at 11:35 AM.
Reply With Quote