[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: Newbie Question: Building Variables Based on Multiple Elements


Subject: Re: Newbie Question: Building Variables Based on Multiple Elements
From: Steve Tinney <stinney@xxxxxxxxxxxxx>
Date: Mon, 31 Jan 2000 23:16:55 -0500

jeffrod@xxxxxxxxxx wrote:
> Is there a way for me to build upon a variable string based on
> multiple elements of the same name?

Sure.  An xsl:variable can be assigned the result of a 'select' or can
be used as a container for template instantiation.  In the latter case,
you can roam around, generating any kind of output you want, and instead
of going directly into the result tree ("output") it goes into the
variable, which is then of type Result Tree Fragment (RTF).  An RTF can
be used like a string, which is adequate for your purposes.

So, you can just do the following:

test.xml:
<test>
  <elem>This</elem>
  <elem>is</elem>
  <elem>a</elem>
  <elem>test</elem>
</test>

concat.xsl:
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="elements">
  <xsl:text>Value of $elements: </xsl:text>
  <xsl:for-each select="/test/elem">
    <xsl:value-of select="."/>
    <xsl:if test="position() &lt; last()">
      <xsl:text> </xsl:text>
    </xsl:if>
  </xsl:for-each>
  <xsl:text>.</xsl:text>
</xsl:variable>

<xsl:template match="/">
  <xsl:value-of select="$elements"/>
</xsl:template>

</xsl:stylesheet>

---

Note that the elements don't have to have the same name here; you could
use the results of any 'select' that is legal within xsl:for-each.

 Steve


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



Current Thread