aadi
aadi

Reputation: 128

XSLT to Concatenate Empty Values

We have a requirement that requires us to list out all the empty values from the incoming xml. I have searched but all I could find was listing non-null values, trying to use that for our xml is not returning the required results.

Here is the xml that we will receive and I want to be able to concatenate all the null values from this xml and print. Kindly assist.

<?xml version = "1.0" encoding = "UTF-8"?>
<Output>
<Rows>
    <ns0:I2NA xmlns:ns0 = "http://www.example.com/schemas/Schema.xsd">
        <ns0:Organization>108</ns0:Organization>
        <ns0:AccountNumber>1231231231231231233 </ns0:AccountNumber>
        <ns0:Status>0</ns0:Status>
        <ns0:VipStatus>0</ns0:VipStatus>
        <ns0:TypeOfIdNo>1</ns0:TypeOfIdNo>
        <ns0:IdNo>2303111450               </ns0:IdNo>
        <ns0:HomePhone>123456              </ns0:HomePhone>
        <ns0:Employer>                                        </ns0:Employer>
        <ns0:EmployersPhone>123456              </ns0:EmployersPhone>
        <ns0:FaxPhone>123456              </ns0:FaxPhone>
        <ns0:MobileNo>0568520421          </ns0:MobileNo>
        <ns0:CountryCode>   </ns0:CountryCode>
        <ns0:PostalCode>          </ns0:PostalCode>
        <ns0:Position>    </ns0:Position>
        <ns0:MaritalStatus>0</ns0:MaritalStatus>
        <ns0:DateOfBirth>00000000</ns0:DateOfBirth>
        <ns0:EmailAddrs>                                                            </ns0:EmailAddrs>
        <ns0:UserCode1>  </ns0:UserCode1>
        <ns0:NationalityCode>   </ns0:NationalityCode>
        <ns0:NameLine1>ABC     </ns0:NameLine1>
        <ns0:NameLine2>                                        </ns0:NameLine2>
        <ns0:NameLine3>                                        </ns0:NameLine3>
        <ns0:ChDob>        </ns0:ChDob>
        <ns0:AddressLine1>USA                                   </ns0:AddressLine1>
        <ns0:AddressLine2>USA                                   </ns0:AddressLine2>
        <ns0:AddressLine3>USA                                   </ns0:AddressLine3>
        <ns0:AddressLine4>USA                                   </ns0:AddressLine4>
        <ns0:City>                              </ns0:City>
        <ns0:State>   </ns0:State>
        <ns0:GenderCode>0</ns0:GenderCode>
        <ns0:StatementNotifIndi> </ns0:StatementNotifIndi>
        <ns0:Nationality>                    </ns0:Nationality>
        <ns0:County>                              </ns0:County>
        <ns0:LastName>John                             </ns0:LastName>
        <ns0:MiddleName>                                        </ns0:MiddleName>
        <ns0:FirstName>SHAN MATHEW                             </ns0:FirstName>
        <ns0:LangPref>   </ns0:LangPref>
    </ns0:I2NA>
</Rows>
<EOF>true</EOF>

When the XSLT is applied, we would like to receive the below string as output.

Status,Employer,CountryCode,PostalCode,Position,EmailAddrs,UserCode1,NationalityCode,NameLine2,NameLine3,ChDob,City,State,StatementNotifIndi,Nationality,County,MiddleName,LangPref

Thanks!

Upvotes: 0

Views: 1038

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167516

Use <xsl:value-of select="//*[not(*) and not(normalize-space())]/local-name()" separator=","/>. But I don't understand why your sample string starts with Status while the XML has a value <ns0:Status>0</ns0:Status> for that field.

With XSLT 1.0 you need a bit of more code:

<xsl:for-each select="//*[not(*) and not(normalize-space())]">
  <xsl:if test="position() > 1"><xsl:text>.</xsl:text></xsl:if>
  <xsl:value-of select="local-name()"/>
</xsl:for-each>

Upvotes: 2

Related Questions