I have a Input XML which looks like this and want the desired output as shown below using XSL transformation. I've been looking through the blog but couldn't find any relevance on how to remove empty tags matching the root element only not the child nodes.
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com">
<Revenue__c/>
<Revenue__c/>
<Revenue__c/>
<Revenue__c>
<Sales_Org_ID__c>IV</Sales_Org_ID__c>
<Branch_ID__c>1</Branch_ID__c>
<Branch_Name__c>TEST</Branch_Name__c>
<Therapy_Code__c>TEST</Therapy_Code__c>
<Therapy_Name__c>TEST</Therapy_Name__c>
<Therapy_Class_Code__c>TEST</Therapy_Class_Code__c>
<Therapy_Class_Name__c>TEST</Therapy_Class_Name__c>
<Payor_Type_Name__c>TEST</Payor_Type_Name__c>
<Calendar_Year_Number__c>2011</Calendar_Year_Number__c>
<Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c>
<Payor_ID__c>TEST</Payor_ID__c>
<Payor_Name__c/>
<Payor_Type_Code__c>TEST</Payor_Type_Code__c>
<MDM_Account_EID__c>66600001</MDM_Account_EID__c>
<MDM_Physician_EID__c>99900001</MDM_Physician_EID__c>
<Account__c>001a000001APU5OAAX</Account__c>
<Contact__c>003a000001RL1EFAA1</Contact__c>
<Revenue_ID__c>41</Revenue_ID__c>
<Calendar_Year_Month__c>01</Calendar_Year_Month__c>
</Revenue__c>
</objects>
This is exactly the out that I want:
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com">
<Revenue__c>
<Sales_Org_ID__c>IV</Sales_Org_ID__c>
<Branch_ID__c>1</Branch_ID__c>
<Branch_Name__c>TEST</Branch_Name__c>
<Therapy_Code__c>TEST</Therapy_Code__c>
<Therapy_Name__c>TEST</Therapy_Name__c>
<Therapy_Class_Code__c>TEST</Therapy_Class_Code__c>
<Therapy_Class_Name__c>TEST</Therapy_Class_Name__c>
<Payor_Type_Name__c>TEST</Payor_Type_Name__c>
<Calendar_Year_Number__c>2011</Calendar_Year_Number__c>
<Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c>
<Payor_ID__c>TEST</Payor_ID__c>
<Payor_Name__c/>
<Payor_Type_Code__c>TEST</Payor_Type_Code__c>
<MDM_Account_EID__c>66600001</MDM_Account_EID__c>
<MDM_Physician_EID__c>99900001</MDM_Physician_EID__c>
<Account__c>001a000001APU5OAAX</Account__c>
<Contact__c>003a000001RL1EFAA1</Contact__c>
<Revenue_ID__c>41</Revenue_ID__c>
<Calendar_Year_Month__c>01</Calendar_Year_Month__c>
</Revenue__c>
</objects>
Any suggestion would be greatly appreciated.
This should do it - it uses apply-templates
only to Revenue nodes with children, and then copy-of
to copy the non empty Revenue trees.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="urn:sobject.partner.soap.sforce.com"
exclude-result-prefixes="x">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/x:objects">
<objects xmlns="urn:sobject.partner.soap.sforce.com">
<xsl:apply-templates select="x:Revenue__c[*]" />
</objects>
</xsl:template>
<xsl:template match="x:Revenue__c">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com">
<Revenue__c>
<Sales_Org_ID__c>IV</Sales_Org_ID__c>
<Branch_ID__c>1</Branch_ID__c>
<Branch_Name__c>TEST</Branch_Name__c>
<Therapy_Code__c>TEST</Therapy_Code__c>
<Therapy_Name__c>TEST</Therapy_Name__c>
<Therapy_Class_Code__c>TEST</Therapy_Class_Code__c>
<Therapy_Class_Name__c>TEST</Therapy_Class_Name__c>
<Payor_Type_Name__c>TEST</Payor_Type_Name__c>
<Calendar_Year_Number__c>2011</Calendar_Year_Number__c>
<Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c>
<Payor_ID__c>TEST</Payor_ID__c>
<Payor_Name__c />
<Payor_Type_Code__c>TEST</Payor_Type_Code__c>
<MDM_Account_EID__c>66600001</MDM_Account_EID__c>
<MDM_Physician_EID__c>99900001</MDM_Physician_EID__c>
<Account__c>001a000001APU5OAAX</Account__c>
<Contact__c>003a000001RL1EFAA1</Contact__c>
<Revenue_ID__c>41</Revenue_ID__c>
<Calendar_Year_Month__c>01</Calendar_Year_Month__c>
</Revenue__c>
</objects>
Edit - It can be simplified:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="urn:sobject.partner.soap.sforce.com"
exclude-result-prefixes="x">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/x:objects">
<objects xmlns="urn:sobject.partner.soap.sforce.com">
<xsl:copy-of select="x:Revenue__c[*]" />
</objects>
</xsl:template>
</xsl:stylesheet>