| Author | Message | 
		
		  | pcelari | 
			  
				|  Posted: Wed Nov 04, 2009 5:40 am    Post subject: how to remove empty nodes in XML? |   |  | 
		
		  | Chevalier
 
 
 Joined: 31 Mar 2006Posts: 411
 Location: New York
 
 | 
			  
				| Hi, 
 the service I called returns a XML tree that contains large number of empty fields.
 
 Is there a simple way to remove all those empty fields?
 
 thanks for any insight!
 
  _________________
 pcelari
 -----------------------------------------
 - a master of always being a newbie
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | mqjeff | 
			  
				|  Posted: Wed Nov 04, 2009 6:43 am    Post subject: |   |  | 
		
		  | Grand Master
 
 
 Joined: 25 Jun 2008Posts: 17447
 
 
 | 
			  
				| Yes. 
 Use the XMLNSC domain.
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | pcelari | 
			  
				|  Posted: Wed Nov 04, 2009 7:22 am    Post subject: |   |  | 
		
		  | Chevalier
 
 
 Joined: 31 Mar 2006Posts: 411
 Location: New York
 
 | 
			  
				| Sorry, I wasn't clear enough. 
 Yes, I'm already using XMLNSC.
 
 There might be a better way of doing this than recursively removing empty fields in the tree.
 
 
  _________________
 pcelari
 -----------------------------------------
 - a master of always being a newbie
 
 Last edited by pcelari on Wed Nov 04, 2009 7:51 am; edited 1 time in total
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | Vitor | 
			  
				|  Posted: Wed Nov 04, 2009 7:31 am    Post subject: |   |  | 
		
		  |  Grand High Poobah
 
 
 Joined: 11 Nov 2005Posts: 26093
 Location: Texas, USA
 
 | 
			  
				| I'm not aware of a "remove empty fields" switch (but that's not to say there isn't one!) but a simple bit of ESQL (I agree recursive) should be able to do the trick. _________________
 Honesty is the best policy.
 Insanity is the best defence.
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | mqjeff | 
			  
				|  Posted: Wed Nov 04, 2009 7:42 am    Post subject: |   |  | 
		
		  | Grand Master
 
 
 Joined: 25 Jun 2008Posts: 17447
 
 
 | 
			  
				| What exactly do you mean by an empty field? |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | pcelari | 
			  
				|  Posted: Wed Nov 04, 2009 7:50 am    Post subject: |   |  | 
		
		  | Chevalier
 
 
 Joined: 31 Mar 2006Posts: 411
 Location: New York
 
 | 
			  
				| I mean nodes like: 
 <ThisTagContainsNothing/>
 _________________
 pcelari
 -----------------------------------------
 - a master of always being a newbie
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | Vitor | 
			  
				|  Posted: Wed Nov 04, 2009 8:06 am    Post subject: |   |  | 
		
		  |  Grand High Poobah
 
 
 Joined: 11 Nov 2005Posts: 26093
 Location: Texas, USA
 
 | 
			  
				| 
   
	| pcelari wrote: |  
	| I mean nodes like: 
 <ThisTagContainsNothing/>
 |  
 I did think that's what you meant.
 
 The issue here is that those tags are valid XML so no parser is going to strip them out. I still don't believe there's a switch in WMB to automatically remove them from the message tree, and if you don't want them (given that they possibly convey something) then it's a matter for your code.
 _________________
 Honesty is the best policy.
 Insanity is the best defence.
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | mqjeff | 
			  
				|  Posted: Wed Nov 04, 2009 8:10 am    Post subject: |   |  | 
		
		  | Grand Master
 
 
 Joined: 25 Jun 2008Posts: 17447
 
 
 | 
			  
				| I think that I'm going to await further comment from kimbert. |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | Vitor | 
			  
				|  Posted: Wed Nov 04, 2009 8:23 am    Post subject: |   |  | 
		
		  |  Grand High Poobah
 
 
 Joined: 11 Nov 2005Posts: 26093
 Location: Texas, USA
 
 | 
			  
				| 
   
	| mqjeff wrote: |  
	| I think that I'm going to await further comment from kimbert. |  
 Who's the person most likely to
 
 a) have an informed opinion on this matter
 b) know of the existence (or not) of a "remove empty fields" switch, possibly having developed it!!
 _________________
 Honesty is the best policy.
 Insanity is the best defence.
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | kimbert | 
			  
				|  Posted: Wed Nov 04, 2009 11:30 am    Post subject: |   |  | 
		
		  |  Jedi Council
 
 
 Joined: 29 Jul 2003Posts: 5543
 Location: Southampton
 
 | 
			  
				| The XMLNSC parser will remove non-significant white space, comments and processing instructions if you want it to. However, there is no switch ( and none planned ) to remove empty tags. Those are genuinely a part of the data, as Vitor correctly pointed out above. 
 I suggests a recursive walk of the message tree, removing the tags that you don't want. Or an ESQL function which you call when mapping each tag, to detect the empty ones and *not* map those.
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | pcelari | 
			  
				|  Posted: Wed Nov 04, 2009 12:28 pm    Post subject: |   |  | 
		
		  | Chevalier
 
 
 Joined: 31 Mar 2006Posts: 411
 Location: New York
 
 | 
			  
				| thanks for all the insights, here's the code modified from the original StripNamespaces. I've yet to test it, though. 
 CREATE PROCEDURE RemoveNullNodes(IN StartRefPtr REFERENCE)
 BEGIN
 DECLARE FieldRefPtr REFERENCE TO StartRefPtr;
 MOVE FieldRefPtr FIRSTCHILD;
 IF LASTMOVE(FieldRefPtr) THEN
 IF FieldRefPtr = NULL THEN
 delete field FieldRefPtr;
 END IF;
 END IF;
 
 WHILE LASTMOVE(FieldRefPtr) DO
 CALL RemoveNullNodes(FieldRefPtr);
 IF FieldRefPtr = NULL THEN
 delete field FieldRefPtr;
 END IF;
 MOVE FieldRefPtr NEXTSIBLING;
 END WHILE;
 END;
 _________________
 pcelari
 -----------------------------------------
 - a master of always being a newbie
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | Gemz | 
			  
				|  Posted: Wed Nov 04, 2009 10:15 pm    Post subject: |   |  | 
		
		  |  Centurion
 
 
 Joined: 14 Jan 2008Posts: 124
 
 
 | 
			  
				| Hi, 
 You can try passing this message through mapping node. I guess it will remove those empty tags.(Though this is not the actual purpose of mapping node.
 
 -GemZ
 _________________
 GemZ
 "An expert is one who knows more and more about less and less until he knows absolutely everything about nothing...."
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  | goffinf | 
			  
				|  Posted: Sat Nov 14, 2009 5:20 pm    Post subject: |   |  | 
		
		  | Chevalier
 
 
 Joined: 05 Nov 2005Posts: 401
 
 
 | 
			  
				| You can do it as a simple XSLT transform :- 
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:template match="*">
 <xsl:copy>
 <xsl:copy-of select="@*"/>
 <xsl:apply-templates/>
 </xsl:copy>
 </xsl:template>
 <xsl:template match="*[not(node())]"/>
 </xsl:stylesheet>
 |  | 
		
		  | Back to top |  | 
		
		  |  | 
		
		  |  |