Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT Looping through a variable set 2

Status
Not open for further replies.
Jun 24, 2005
109
0
0
GB
Hi all,

I'm fairly new to XSLT, and have 2 questions. Firstly is my specifc problem: Is there a way to declare a set of variables and then iterate through them.

My example is I have several status' for some task items in my flat xml file. I want to specify which status' get displayed in my xslt, and also a custom order. What I want to do is set an array of variables at the top of the xslt, and then iterate through those variables applying my template on each iteration using the variable. Let me know if you want some code samples.

Secondly, can anyone point me in the direction of a good xslt resource (i use w3schools quite a lot).

cheers guys

Matt
 
[1] In xslt 2.0, you can do this with sequence construction.
[tt]
<xsl:variable name="z1" select="'A'" />
<xsl:variable name="z2" select="'B'" />
<xsl:variable name="z3" select="'C'" />
<xsl:variable name="z4" select="'D'" />
<xsl:variable name="z" select="$z1,$z2,$z3,$z4" />
[/tt]
[2] And somewhere you call upon them.
[tt]
<xsl:for-each select="$z">
<xsl:value-of select="." />
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
[/tt]
[3] It will output
[tt] A,B,C,D[/tt]
 
if you cannot use an XSLT2 processor, the following works for all XSLT1.0 processors that I am aware of. The example is based on tsuji's example
Code:
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0"
               xmlns:foo="[URL unfurl="true"]http://foo.com"[/URL] exclude-result-prefixes="foo">

 <xsl:output method="text" encoding="utf-8"/>

 <foo:vars>
    <foo:var name="z1">A</foo:var>
    <foo:var name="z2">B</foo:var>
    <foo:var name="z3">C</foo:var>
    <foo:var name="z4">D</foo:var>
 </foo:vars>

 <xsl:template match="/">
    <xsl:for-each select="document('')/xsl:stylesheet/foo:vars/foo:var" >
      <xsl:value-of select="." />
      <xsl:if test="position() != last()">
          <xsl:text>,</xsl:text>
      </xsl:if>
    </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top