Thursday, June 10, 2010

Remove duplicate nodes from an XML using XSL

HI All!

Its been a long time since my last post. I finally got something which I feel need to be posted else I will have a hard time finding it later.

Ok, one of my recent quests was how to remove duplicate nodes from an xml like below





19




















Now if we want to transform this XML into another XML without the repeating
" tag then the xsl to be used is thus :



































Transform this using XSLT

XmlDocument stripper = new XmlDocument();
stripper.Load(Server.MapPath("~/App_Data/transform.xsl")); //Compile here
StringWriter output = new StringWriter();
XslCompiledTransform emptyNodeRemover = new XslCompiledTransform();
emptyNodeRemover.Load(stripper); //Port output to string
output = new StringWriter();
XmlDocument source = new XmlDocument();

source.Load(Server.MapPath("~/App_Data/response.xml"));

emptyNodeRemover.Transform(new XmlNodeReader(source), null, output);
output.Flush(); //Reload source with emptied nodes
source.LoadXml(output.ToString());


The above xsl also specifies to restructure the xml file into





19













Now this transformed XML can be fed into a telerik RadGrid successfully as it is the format it accepts.

Cheers!

No comments: