Thursday 17 April 2014

Remove xsi:nil="true" from your XML along with the Null Tags

Suppose I am getting a XML like this..

<ebs:ShopFloorInputParameters xmlns:ebs="http://xmlns.abcd.com/EBSShopFloorSyncProvABCSImpl">
   <ValidateShopFloorRequest>
       <OrgCode xsi:nil="true">CH112A</OrgCode>
      <MFGType xsi:nil="true">FLOW12</MFGType>
      <ScheduleNum xsi:nil="true">1175007</ScheduleNum>
      <LineId xsi:nil="true"></LineId>
      <LineOP xsi:nil="true"></LineOP>

<ValidateShopFloorRequest>
</ebs:ShopFloorInputParameters>


No I want to remove all emty tags and also xsi:nil from my elements. I just have to use the below XSLT (source variable and target variable will be same the one which is carrying the XML)

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0"
                xmlns:bpws="
http://schemas.xmlsoap.org/ws/2003/03/business-process/"
                xmlns:xp20="
http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
                xmlns:mhdr="
http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
                xmlns:bpel="
http://docs.oasis-open.org/wsbpel/2.0/process/executable"
                xmlns:oraext="
http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
                xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
                xmlns:dvm="
http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
                xmlns:hwf="
http://xmlns.oracle.com/bpel/workflow/xpath"
                xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
                xmlns:med="
http://schemas.oracle.com/mediator/xpath"
                xmlns:ids="
http://xmlns.oracle.com/bpel/services/IdentityService/xpath"
                xmlns:bpm="
http://xmlns.oracle.com/bpmn20/extensions"
                xmlns:xdk="
http://schemas.oracle.com/bpel/extension/xpath/function/xdk"
                xmlns:xref="
http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
                xmlns:xsd="
http://www.w3.org/2001/XMLSchema"
                xmlns:ora="
http://schemas.oracle.com/xpath/extension"
                xmlns:socket="
http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
                xmlns:ldap="
http://schemas.oracle.com/xpath/extension/ldap"
                exclude-result-prefixes="xsi xsl bpws xp20 mhdr bpel oraext dvm hwf med ids bpm xdk xref ora socket ldap">

  <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="text()">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>



Target XML :


<ebs:ShopFloorInputParameters xmlns:ebs="http://xmlns.abcd.com/EBSShopFloorSyncProvABCSImpl">
<ValidateShopFloorRequest>
<OrgCode>CH112A</OrgCode>
<MFGType>FLOW12</MFGType>
<ScheduleNum>1175007</ScheduleNum>

<ValidateShopFloorRequest>
</ebs:ShopFloorInputParameters>