5 Replies Latest reply: Jul 23, 2013 1:31 AM by PuneetRekhade RSS

    How to Remove All Empty nodes from XML Payload

    988967

      Hi EveryOne,


      I have a requirement to "Remove All Empty Nodes from XML Payload " from SOA , I tried using Transformation but I couldn't get the solution. Could any one please suggest me to do this,

       

       

      Thanks,

      Narasimhulu.S

        • 1. Re: How to Remove All Empty nodes from XML Payload
          mathieu.d

          Not sure it will answer your exact need but while you edit the XSL map in jdeveloper you can make a right click on the middle section.

          One of the option shown let you configure how the XML should be generated like "do not generate unmapped node and optional node" etc...

           

          regards,

          mathieu

          • 2. Re: How to Remove All Empty nodes from XML Payload
            LFHeckler

            You can use a XSL transformation like this to copy only the elements with value:

             

            <?xml version="1.0" encoding="UTF-8"?>

            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

             

              <xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes" />

              <xsl:strip-space  elements="*"/>

             

              <xsl:template match="/">

                   <xsl:apply-templates />

              </xsl:template>

             

              <xsl:template match="*">

                   <xsl:if test="string-length(.//text())>0">

                        <xsl:element name="{name(.)}" >

                             <xsl:apply-templates />

                        </xsl:element>

                   </xsl:if>

              </xsl:template>

             

            </xsl:stylesheet>

             

            Note that this is a fast suggestion... probably can be improved in order to have a better performance in case of big XML payloads.

             

            Regards

            Luis Fernando Heckler

            • 3. Re: How to Remove All Empty nodes from XML Payload
              988967

              Hi Luis Fernando Heckler,

               

              Thanks for Your suggestions , I tried this but its not working. Any other suggestions pls share.

               

               

              Thanks,

              Narasimhulu.S

              • 4. Re: How to Remove All Empty nodes from XML Payload
                LFHeckler

                Cold you share a sample of your input xml?

                • 5. Re: How to Remove All Empty nodes from XML Payload
                  PuneetRekhade

                  This should help.. Let me know, how does it go.

                  ======================================

                  <?xml version="1.0" encoding="UTF-8" ?>

                  <xsl:stylesheet version="1.0"

                                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

                                  exclude-result-prefixes="xsl">

                    <xsl:template match="node()">

                      <xsl:if test="count(descendant::text()[string-length(normalize-space(.))>0]|@*)">

                        <xsl:copy>

                          <xsl:apply-templates select="@*|node()"/>

                        </xsl:copy>

                      </xsl:if>

                    </xsl:template>

                    <xsl:template match="@*">

                      <xsl:copy/>

                    </xsl:template>

                    <xsl:template match="text()">

                      <xsl:value-of select="normalize-space(.)"/>

                    </xsl:template>

                  </xsl:stylesheet>

                  ======================================

                  Or even simple Google search will point you to plenty of similar resources for the same. You can try and explore those options as well.

                  HTH.