xsl-list
[Top] [All Lists]

Re: append values to a String

2003-02-12 09:30:26
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



<Prev in Thread] Current Thread [Next in Thread>