[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: append values to a String

Subject: Re: append values to a String
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 12 Feb 2003 16:30:26 +0000
xsl for each append
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.