1

I am new to XSLT and I am trying the following:

I have the following:

<TR> <TD> Name: </TD> <TD><xsl:value-of select="ZNAME"/> </TD> </TR>

which returns

ADAM,BRIAN,CHARLIE,DAVID

How I can make this return:

ADAM
BRIAN
CHARLIE
DAVID

Please suggest.

Portal Admin
  • 11
  • 1
  • 2
  • 1
    If you have this input `ADAM,BRIAN,CHARLIE,DAVID`, you are looking for tokenization. That would be a duplicate of http://stackoverflow.com/questions/136500/does-xslt-have-a-split-function –  Feb 25 '11 at 19:16
  • @Alejandro - You should add that as an answer so you can get a proper upvote. @Portal Admin - Like Alejandro said, tokenization is the way to go. However, if you need to add a literal break in your output in some other circumstance, try using something like ` `. – Daniel Haley Feb 25 '11 at 20:54
  • @DevNull: Do note that `translate(ZNAME,',',' ')` could be also a proper answer to this question **as is**. –  Feb 25 '11 at 21:21
  • Good question, +1. See my answer for a complete, short and easy solution. :) – Dimitre Novatchev Feb 26 '11 at 03:33

2 Answers2

0

This transformation:

<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="ZNAME" name="tokenize">
  <xsl:param name="pText" select="concat(., ',')"/>

  <xsl:if test="string-length($pText)>0">
   <xsl:value-of select="substring-before($pText, ',')"/>
   <br />
   <xsl:call-template name="tokenize">
    <xsl:with-param name="pText"
     select="substring-after($pText, ',')"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<ZNAME>ADAM,BRIAN,CHARLIE,DAVID</ZNAME>

produces the wanted, correct result:

ADAM<br/>BRIAN<br/>CHARLIE<br/>DAVID<br/>

which when viewed in a browser is displayed like:

ADAM
BRIAN
CHARLIE
DAVID

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

If you need a solution that doesn't output a trailing <br/> element, try this:

<TR> <TD> Name: </TD> <TD><xsl:apply-templates select="ZNAME"/> </TD> </TR>

<xsl:template match="ZNAME" name="convertcommas">
  <xsl:param name="text" select="."/>
  <xsl:value-of select="substring-before(concat($text,','),',')" />
  <xsl:if test="contains($text,',')">
    <br />
    <xsl:call-template name="convertcommas">
      <xsl:with-param name="text" select="substring-after($text,',')" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

The concat($text,',') makes sure the substring-before sees at least one comma, thereby outputting the string on it's own if there were no commas originally.

Flynn1179
  • 11,925
  • 6
  • 38
  • 74