Search code examples
xsltxslt-1.0xslt-grouping

XSLT1.0: Need to eliminate records which is having empty fields


Experts, i need to write XSLT 1.0 code to eliminate the Record in the XML which is having empty fields.

Input:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:Accounts xmlns:ns0="urn:text.com:accounts">
    <Recordset>
        <Record>
            <FIELD1>123</FIELD1>
            <FIELD2/>
            <FIELD3/>
            <FIELD4>2020</FIELD4>
            <FIELD5/>           
        </Record>       
        <Record>
            <FIELD1/>
            <FIELD2/>
            <FIELD3/>
            <FIELD4/>
            <FIELD5/>                       
        </Record>       
        <Record>
            <FIELD1>89</FIELD1>
            <FIELD2>098</FIELD2>
            <FIELD3/>
            <FIELD4>678</FIELD4>
            <FIELD5>NEW</FIELD5>
            <FIELD6/>               
        </Record>       
        <Record>
            <FIELD1/>
            <FIELD2/>
            <FIELD3/>
            <FIELD4/>
            <FIELD5/>                   
        </Record>
        <Record>
            <FIELD1/>
            <FIELD2/>
            <FIELD3/>
            <FIELD4/>
            <FIELD5/>       
        </Record>       
    </Recordset>
</ns0:Accounts>

** Desired Output:**

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:Accounts xmlns:ns0="urn:text.com:accounts">
    <Recordset>
        <Record>
            <FIELD1>123</FIELD1>
            <FIELD2/>
            <FIELD3/>
            <FIELD4>2020</FIELD4>
            <FIELD5/>           
        </Record>           
        <Record>
            <FIELD1>89</FIELD1>
            <FIELD2>098</FIELD2>
            <FIELD3/>
            <FIELD4>678</FIELD4>
            <FIELD5>NEW</FIELD5>
            <FIELD6/>               
        </Record>           
    </Recordset>
</ns0:Accounts>

** XSLT I tried:**

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
             <xsl:apply-templates 
                  select="node()[boolean(normalize-space())]
                         |@*"/>
     </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

This XSLT removing the empty fields in all the records, but my requirement is to remove the Record which is having all the empty fields. If the record contains one field with some value then we need to keep that record as it is. Please support..


Solution

  • Here is one way you could look at it:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Record[not(*/text())]"/>
    
    </xsl:stylesheet>
    

    Here's another:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/*">
        <xsl:copy>
            <Recordset>
                <xsl:copy-of select="Recordset/Record[*/text()]"/>  
            </Recordset>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>