-1

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>
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Rajesh Kumar
  • 11
  • 1
  • 6
  • you have to indent tags to make them visible in question – barbsan Nov 30 '18 at 08:06
  • So what is a `null` value? If you are using XSLT 2 then you can use the `separator` attribute of `xsl:value-of` e.g. `` instead of explicitly outputting a separator with `xsl:text`. – Martin Honnen Nov 30 '18 at 08:20
  • @MartinHonnen I don't think it's that simple: http://xsltransform.hikmatu.com/gWmuiHS – michael.hor257k Nov 30 '18 at 08:32
  • It will depend on what a null value is (my guess was https://xsltfiddle.liberty-development.net/nc4NzRu) but in any case using `separator` instead explicit `,` seems more compact in any case for XSLT 2 or 3. – Martin Honnen Nov 30 '18 at 08:38
  • @MartinHonnen You are right that `null` is ambiguous, but in this case I think it's safe to bet the elements ("fields" in OP words) will always be there (apparently this is a DB export). – michael.hor257k Nov 30 '18 at 08:42

1 Answers1

0

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

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51