Link to home
Start Free TrialLog in
Avatar of PrecisionDotNet
PrecisionDotNetFlag for United States of America

asked on

Selecting new variable from list for each iteration of for-each loop

Hi All,

I'm trying to include in my XSLT file a variable containing multiple elements which I need to use during a for-each loop. The variable could be as below in theory:

<xsl:variable name="colorPalette">
   <color value="red"/>
   <color value="blue"/>
   <color value="green"/>
</xsl:variable>

During a for-each loop, I need to use the values - a different one for each iteration. Here's an example of how I thought it might work based on a similar solution I found:

<xsl:for-each select="...">
   <set>
      <xsl:attribute name="color">
         <xsl:for-each select="$colorPalette/color[not(@value = preceding::*/@value)]">
            <xsl:value-of select="@value"/>
         </xsl:for-each>
      </xsl:attribute>
   </set>
</xsl:for-each>

What would be the best way to accomplish this? The main for-each loop needs to remain a loop, but the variable process could use templates or some other approach. Thanks in advance!
Avatar of sweetfa2
sweetfa2
Flag of Australia image

<set color="redbluegreen"/>
<set color="redbluegreen"/>

or


<set color="red"/>
<set color="blue"/>
<set color="green"/>
ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
      <xsl:for-each select=".">
      	<xsl:for-each select="$colorPalette/color">
	      <xsl:variable name="index" select="position()"/>
         <set>
	         	<xsl:attribute name="pos">
	         		<xsl:value-of select="$index"/>
	         		</xsl:attribute>
            <xsl:attribute name="color">
	            <xsl:value-of select="$colorPalette/color[$index]/@value"/>
            </xsl:attribute>
         </set>
         </xsl:for-each>
      </xsl:for-each> 

Open in new window

This will only work in XSLT2.0 I am afraid.
Or you need to use an extension
a lot of processors support exslt:nodeset()

If you put some XML tags in a variable,
you can't simply use that as a lookup using XPath
because the XML elements are a Result Tree Fragment,
and XSLT1 does not cast a RFT to a node-set() automatically
(XSLT2 does)

In order for me to show you a solution that would possibly allow this,
I need to know which processor you are using.
Is this .net? let me know

Other options are
- having the values for the variable in a seperate XML document and use document() to get it
- put the values in a string with a seperator and make a recursive function to loop over the string (bit awkward, but it works)
- start using XSLT2 (there is a saxon.net version)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PrecisionDotNet

ASKER

Thanks for the responses! Sorry I didn't give more details in my question. This would be run on a SharePoint server (specifically Microsoft's SharePoint Online servers), the XSLT file stored in a document library and read in using JavaScript.
Then you are using msxml and zc2's solution should work
It works like a charm! Thanks for the answers and explanations.
you're welcome