web stats
Remove XML Tag if attributes value is empty - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-08-2018, 11:51 PM
nkn786 nkn786 is offline
What's HL7?
 
Join Date: Mar 2014
Posts: 1
nkn786 is on a distinguished road
Default Remove XML Tag if attributes value is empty

Hi All,

I am trying to remove the XML tag if the @attributes having an empty value after that i have to set it back to my original XML
Below is my code
In My Below example
Code:
<bookid value=""/>
this value is empty like this i have to remove for all and set it back to xml.
Code:
var xml = new XML(next.text);
var appndtag="",arr1=[],arr2=[],i=0,j=0;
for each (var childnm in xml.children())   // check number of sub tag
{
	
	for each (alltag in childnm.descendants())  //check number of node under subtag
	{
		
			arr1[i]=alltag.name();   //store allsubnode name
			i=i+1
			arr2[j]=alltag;  //store all subnode and its value
			if(alltag.hasSimpleContent())
			{
				if(alltag.@value == "")
				{
					delete alltag[arr2[j].name()])
					
				}
			}
		
		j=j+1
	}
	
}
Input :
Code:
<Test>
	<id value="123"/>
	<Book>
		<source>
			<English>
				<bookid value=""/>
				<version>
					<type>
						<place>
							<author value="Test123"/>
							<index value="10"/>
							<display value=""/>
						</place>
					</type>
				</version>
			</English>
		</source>
	</Book>
	<Book>
		<source>
			<German>
				<bookid value=""/>
				<version>
					<type>
						<place>
							<author value="Test143"/>
							<index value=""/>
							<display value="Helo"/>
						</place>
					</type>
				</version>
			</German>
		</source>
	</Book>
</Test>
Expected Output:
Code:
<Test>
	<id value="123"/>
	<Book>
		<source>
			<English>
				<version>
					<type>
						<place>
							<author value="Test123"/>
							<index value="10"/>
						</place>
					</type>
				</version>
			</English>
		</source>
	</Book>
	<Book>
		<source>
			<German>
				<version>
					<type>
						<place>
							<author value="Test143"/>
							<display value="Helo"/>
						</place>
					</type>
				</version>
			</German>
		</source>
	</Book>
</Test>
Please help me out.
Reply With Quote
  #2  
Old 05-09-2018, 11:06 AM
Shamil Shamil is offline
OBX.3 Kenobi
 
Join Date: May 2013
Posts: 154
Shamil is on a distinguished road
Default

Probably like this:
Code:
for each (var node in xml.descendants()) {	
	for each (var a in node.attributes()) {
		if (!a.toString()) {
			delete node.@[a.name()];
		}
	}
}
Reply With Quote
Reply

Tags
attributes, javascript, xml child segments, xml tags

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 07:23 PM.


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