home
links
tips

code
users
tools
tutorials
projects
web
help
design

mudabone
suletzki
trans48
nutrition reality

MarthaAttire!

code Clean XML


XSLT Style Sheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
     version="1.0" 
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" version="1.0" encoding="UTF-8"/>

   <!-- Whenever you match any node or any attribute -->
   <xsl:template match="node()|@*">
      <!-- Copy the current node -->
     <xsl:if test="normalize-space(.) != '' or normalize-space(./@*) != '' ">
          <xsl:copy>
              <!-- Including any attributes it has and any child nodes -->
               <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
     </xsl:if>
   </xsl:template>

</xsl:stylesheet>


Sample Code implementation

          public static string RemoveEmptyTags(string sXML)
          {
               System.Text.StringBuilder sb = new StringBuilder();
               
               sb.Append("<?xml version="1.0" encoding="UTF-8"?>");
               sb.Append("<xsl:stylesheet ");
               sb.Append("     version="1.0" ");
               sb.Append("     xmlns:msxsl="urn:schemas-microsoft-com:xslt"");
               sb.Append("     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">");
               sb.Append("     <xsl:output method="xml" version="1.0" encoding="UTF-8"/>");
               sb.Append("   <!-- Whenever you match any node or any attribute -->");
               sb.Append("   <xsl:template match="node()|@*">");
               sb.Append("      <!-- Copy the current node -->");
               sb.Append("     <xsl:if test="normalize-space(.) != '' or normalize-space(./@*) != '' ">");
               sb.Append("          <xsl:copy>");
               sb.Append("              <!-- Including any attributes it has and any child nodes -->");
               sb.Append("               <xsl:apply-templates select="@*|node()"/>");
               sb.Append("          </xsl:copy>");
               sb.Append("     </xsl:if>");
               sb.Append("   </xsl:template>");
               sb.Append("</xsl:stylesheet>");
               return utils.transXMLStringThroughXSLTString(sXML,sb.ToString());
          }

          private static string transXMLStringThroughXSLTString(string sXML, string sXSLT)
          {
               //This is the logic of the application.
               XslTransform objTransform=new XslTransform();
               XmlDocument objDocument=new XmlDocument();
               StringWriter objStream=new StringWriter();

               //objDocument.Load(strAppPath+"\BookFair.xml");
               objDocument.LoadXml(sXML);

               StringReader stream = new StringReader(sXSLT);
               XmlReader xmlR = new XmlTextReader(stream);

               objTransform.Load(xmlR,null,null);

               objTransform.Transform(objDocument,null,
                    objStream,null);
               return objStream.ToString().Replace(@"encoding=""utf-16""?>",@"encoding=""utf-8""?>");
          }


Last Modified 5/10/04 3:36 PM

Hide Tools