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

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

https://github.com/nextgenhealthcare/connect-examples

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).

Parameters:
  • 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).

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

The code:

Code:
function stripEmptyNodes(node, stripWhitespaceNodes, onlyDeleteTrailing) {
	var nonEmptyFound = false;
	for (var childIndex = node.children().length()-1; childIndex >= 0; childIndex--) {
		var child = node.children()[childIndex];
		stripEmptyNodes(child,stripWhitespaceNodes,onlyDeleteTrailing);
		if (stripWhitespaceNodes && !child.toString().trim() || !stripWhitespaceNodes && !child.toString())
			if (!nonEmptyFound || !onlyDeleteTrailing)
				delete node.children()[childIndex];
			else
				node.children()[childIndex] = new XML('<'+child.name().toString()+'/>');
		else
			nonEmptyFound = true;
	}
}
Attached Files
File Type: xml stripEmptyNodes (new 4-24-13).xml (1.0 KB, 177 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 10:35 AM.
Reply With Quote