Announcement

Collapse
No announcement yet.

Remove XML Tag if attributes value is empty

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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([email protected] == "")
    				{
    					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.

  • #2
    Probably like this:
    Code:
    for each (var node in xml.descendants()) {	
    	for each (var a in node.attributes()) {
    		if (!a.toString()) {
    			delete [email protected][a.name()];
    		}
    	}
    }

    Comment

    Working...
    X