Re: [xsl] append values to a String

Subject: Re: [xsl] append values to a String
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 12 Feb 2003 16:30:26 +0000
Hi Dongzhi,

> I'd like to loop through all the children nodes under <Sample>,

Looping is easiest with <xsl:for-each>:

  <xsl:for-each select="Sample/*">
    ...
  </xsl:for-each>

> find those have non-empty value,

Filter the elements that you've selected using a predicate that tests
whether they have a string value:

  <xsl:for-each select="Sample/*[string()]">
    ...
  </xsl:for-each>

> and construct a String with all those values append to each other
> with a "," delimiter in between them, i.e. the result String should
> look like: "something,something else,".

The <xsl:value-of> gets the string value of the element; add the comma
using <xsl:text>.

  <xsl:for-each select="Sample/*[string()]">
    <xsl:value-of select="." />
    <xsl:text>,</xsl:text>
  </xsl:for-each>

> And I need to assign this String to a variable since I need to use
> it elsewhere.

Just wrap the above in an <xsl:variable> element as follows:

  <xsl:variable name="String">
    <xsl:for-each select="Sample/*[string()]">
      <xsl:value-of select="." />
      <xsl:text>,</xsl:text>
    </xsl:for-each>
  </xsl:variable>

Technically, the $String variable is set to a result tree fragment
containing a single text node whose string value is the value that
you're after, but this will make no difference when you use the
variable as you would a string.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread