Search code examples
xsltconcatenationnotnull

How to Concatenate strings with "," ( Comma ) Delimiter only when the string values are not null using xslt2.0


For Eg: Street, BuildingId, FloorId, UnitId needs to be separated with , only when the values are not null. If any of the fields is null do not separate with a comma.

 <ADDRESS nil="true"><xsl:value-of select="//street"/><xsl:text>,</xsl:text><xsl:value-of select="//buildingId"/><xsl:text>,</xsl:text><xsl:value-of select="//floorId"/><xsl:text>,</xsl:text><xsl:value-of select="//unitId"/></ADDRESS>

Solution

  • If you're using XSLT 2.0, try:

    <xsl:value-of select="(street, buildingId, floorId, unitId)[string()]" separator=","/>
    

    Demo: http://xsltransform.hikmatu.com/gWmuiHS/1


    Added:

    Is it possible to add a space between the attributes if any of the field is blank?

    Try:

    <xsl:value-of select="for $i in (street, buildingId, floorId, unitId) return if (string($i)) then $i else ' ' " separator=","/>
    

    Demo: http://xsltransform.hikmatu.com/gWmuiHS/3